本文介绍了使用Func()参数的ASP.NET Core日志记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在将ASP.NET Core与NLog一起使用,并使用NLog.Web.AspNetCore块软件包将其替换为原始ASP.NET Core记录器.

I am using ASP.NET core with NLog, using it as a replacement for the original ASP.NET Core logger with the NLog.Web.AspNetCore nugget package.

NLog包含有用的Func()委托签名,该签名仅在启用相应的日志记录级别时才允许执行参数评估:

NLog contains a useful Func() delegate signature that allows to performs arguments evaluation only if the corresponding logging level is enabled:

static readonly Logger log = LogManager.GetCurrentClassLogger();
log.Trace(() => request.JsonSerializer.Serialize(body));

我正在将ASP.NET与NLog一起使用,但听起来此功能不可用:

I am using ASP.NET with NLog, but it sounds like this feature is not available:

private ILogger<MyController> log;
log.Trace(() => request.JsonSerializer.Serialize(body));

在开始为自己编写一个方法之前,我想知道是否错过了什么,但我还没有发现使用带有NLog的ASP.NET Core的带有委托参数的此类日志记录方法的任何信息.

Before undertaking to write myself a method, I would like to know if I missed something, I have not find anything about such logging methods with a delegate argument using ASP.NET Core with NLog.

推荐答案

Microsoft.Extensions.Logging抽象中没有这样的东西,而且它的构建方式也不容易.虽然您可以轻松地向其中添加扩展方法,并且实际上所有日志调用都是 扩展方法,但基本的Log方法是确定是否记录某些内容的方法,因为它是唯一实际拥有的内容访问已配置的日志级别.

There is no such thing in the Microsoft.Extensions.Logging abstractions, and the way it is built, it isn’t exactly easy to do such a thing. While you can easily add extension methods to it, and actually all log calls are extension methods, the base Log method is what determines whether or not to log somethings since it is the only thing that actually has access to the configured log level.

话虽这么说,日志记录抽象使用的东西可能使做类似的事情成为可能.为此,请考虑ILogger.Log方法的签名:

That being said, the logging abstractions to use something that may make it possible to do something similar to this. For that, consider the signature of the ILogger.Log method:

void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)

如您所见,实际上没有传递给它的字符串,只有一个stateformatter.在默认的扩展方法中,状态是FormattedLogValues对象,而格式化程序只是在状态上调用ToString()的方法,即FormattedLogValues对象.

As you can see, there isn’t actually a string being passed to it, but just a state and a formatter. In the default extension methods, the state is a FormattedLogValues object and the formatter is just a method that calls ToString() on the state, i.e. the FormattedLogValues object.

FormattedLogValues实际上是构建格式化字符串的地方,也是进行结构化日志记录的地方.因此,序列化日志消息中的某些对象实际上是一个坏主意;您可以直接将其传递给记录器.

The FormattedLogValues is what actually builds the formatted string, and that’s also where the structured logging is happening. So it is actually a bad idea to serialize some object in your log message; you can just pass that directly to the logger.

但是您可以在这里给Log提供您自己的重载,该重载使用一个函数代替,然后将其包装到某个状态对象中,该对象在调用ToString()时执行该功能.

But what you could do here is provide your own overloads to Log that take a function instead which is then wrapped into some state object that executes the function when ToString() is being called.

这篇关于使用Func()参数的ASP.NET Core日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 23:27