![](/img/trans.png)
[英]Using Log4Net to log all the functions called in ASP.NET and C# application
[英]How to log asynchronously with log4net in an ASP.NET MVC Web Application without using up all available threads?
是AsyncForwardingAppender
的的Log4Net.Async
包安全在ASP.NET MVC Web應用程序使用? 我擔心它會堵塞可用的線程。
它歸結為我想要調用async
方法將日志發送到HTTP API。 我可以使用async void
方法,就像這個人做的那樣 :
protected override async void Append(log4net.Core.LoggingEvent loggingEvent)
{
var message = new SplunkMessage(loggingEvent.Level, loggingEvent.ExceptionObject);
var success = await _splunkLogger.Report(message);
//do something with the success status, not really relevant
}
他后來更新了他的代碼 :
public void DoAppend(log4net.Core.LoggingEvent loggingEvent)
{
var clientIp = _clientIpRetriever.GetClientIp();
var message = new SplunkMessage(loggingEvent.Level, loggingEvent.ExceptionObject, clientIp);
SendMessageToSplunk(message);
}
private async void SendMessageToSplunk(SplunkMessage message)
{
try
{
var success = await _splunkLogger.Report(message);
//do something unimportant
}
catch(Exception x)
{
LogLog.Error(GetType(), "Error in SplunkAppender.", x);
}
}
但是我太害怕實際嘗試它,因為涉及的危險 :“首先,讓我指出”火與忘記“幾乎總是在ASP.NET應用程序中的錯誤”。
有什么建議么?
查看源代碼,您可以看到AsyncForwardingAppender
僅使用一個線程來使事件出列。 使用它不會殺死你的MVC應用程序,因為只使用一個線程。
關於“火與忘記”作為網絡應用程序中的錯誤模式,你必須在聲明中添加一些鹽,因為答案談到讓功能操作無人監督而不是記錄操作的危險。 在沒有應用程序停止工作的情況下,日志記錄應該能夠失敗(這就是為什么當配置或日志記錄失敗時log4net從不說任何內容)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.