如何在ASP中使用log4net异步登录。NET MVC Web应用程序没有使用所有可用的线程?

时间:2021-10-29 03:42:18

Is AsyncForwardingAppender of the Log4Net.Async package safe to use in an ASP.NET MVC Web Application? I'm worried that it will clog up the available threads.

是Log4Net的AsyncForwardingAppender。在ASP中使用异步包是安全的。净MVC Web应用程序?我担心它会阻塞可用的线程。

It comes down to me wanting to make a call to an async method to send the logs to an HTTP API. I could use an async void method like the way this guy did it:

最后,我希望调用异步方法,将日志发送到HTTP API。我可以使用异步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
}

He later updated his code:

他后来更新了他的代码:

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);
    }            
}

But I'm too scared to actually try it because of the dangers involved: "First off, let me point out that "fire and forget" is almost always a mistake in ASP.NET applications".

但是我太害怕去尝试它了,因为它涉及到的危险:“首先,让我指出,在ASP中“火与遗忘”几乎总是一个错误。网络应用”。

Any suggestions?

有什么建议吗?

1 个解决方案

#1


2  

Looking at the source you can see that the AsyncForwardingAppender uses only one thread to dequeue the events. Using it won't kill your MVC app since only one thread will be used.

查看源文件,您可以看到AsyncForwardingAppender只使用一个线程来对事件进行去队列处理。使用它不会杀死你的MVC应用,因为只会使用一个线程。

Regarding "Fire and forget" as a bad pattern in web apps, you have to add a grain of salt to the statement since the answer talks about the danger of letting a functional operation go unsupervised, not a logging one. Logging should be able to fail without your application ceasing working (which is why log4net never says anything when configuration or logging fails)

在web应用程序中,“Fire and forget”是一种糟糕的模式,因此您必须在声明中添加一点盐,因为答案中提到了让功能操作不受监视(而不是日志记录操作)的危险。日志记录应该能够在应用程序停止工作的情况下失败(这就是为什么log4net在配置或日志失败时不会说任何话)

#1


2  

Looking at the source you can see that the AsyncForwardingAppender uses only one thread to dequeue the events. Using it won't kill your MVC app since only one thread will be used.

查看源文件,您可以看到AsyncForwardingAppender只使用一个线程来对事件进行去队列处理。使用它不会杀死你的MVC应用,因为只会使用一个线程。

Regarding "Fire and forget" as a bad pattern in web apps, you have to add a grain of salt to the statement since the answer talks about the danger of letting a functional operation go unsupervised, not a logging one. Logging should be able to fail without your application ceasing working (which is why log4net never says anything when configuration or logging fails)

在web应用程序中,“Fire and forget”是一种糟糕的模式,因此您必须在声明中添加一点盐,因为答案中提到了让功能操作不受监视(而不是日志记录操作)的危险。日志记录应该能够在应用程序停止工作的情况下失败(这就是为什么log4net在配置或日志失败时不会说任何话)