ILogger 在函数 Log<TState>(LogLevel logLevel [英] ILogger accessing params args in the function Log<TState>(LogLevel logLevel
问题描述
我正在 .Net Core 3.1 中实现自定义 ILogger.该类需要实现以下契约
I am implementing a custom ILogger in .Net Core 3.1. The class needs to implement the following contract
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
我正在触发 LogDebug 函数并传递一个元组(键、值)参数列表,如下所示:
I am triggering the LogDebug function and passing a list of tuple (key, value) arguments like this:
logger.LogDebug("MyDebugMessage", ("arg1Key", "arg2Value"), ("arg2Key", "arg2Value"), ("arg3Key", "arg3Value"));
ILogger 日志函数被触发,第一个参数是消息MyDebugMessage".传递给状态参数.但是,ILogger 函数中的参数列表没有变量.如何访问参数列表?
The ILogger Log function is triggered and the first parameter the message "MyDebugMessage" is passed into the state parameter. However there is no variable for the list of arguments in the ILogger function. How can I access the list of arguments?
推荐答案
传递给TState state"的默认对象是Microsoft.Extensions.Logging.格式化日志值
The default object passed to "TState state" is Microsoft.Extensions.Logging.FormattedLogValues
派生自类型 IReadOnlyList
which is derived from type IReadOnlyList<KeyValuePair<string, object>>
您可以从中获取参数的 kvp 列表
you can get the kvp list of arguments from it
这篇关于ILogger 在函数 Log<TState>(LogLevel logLevel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!