0

我正在尝试实现自定义 ILogger 。网核 3.1

CustomLogger 类实现了 ILogger。需要实施的方法之一是:

 public class AuditLogLogger: ILogger
 {
    public IDisposable BeginScope<TState>(TState state)
    {
        return null;
    }

    public bool IsEnabled(LogLevel logLevel)
    {
        return true;
    }
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
{
   // How can I access the parameters I sent it from the controller?
}
}

从我的控制器中,我触发了 LogInformation 方法并传递了一个字符串和一个 KeyValuePair 列表,如下所示:

List<KeyValuePair<string, string>> udf = new List<KeyValuePair<string, string>>();
udf.Add(new KeyValuePair<string, string>("Test", "Test"));
_logger.LogInformation("This is a test", udf);

我的代码能够到达 Log<TState> 但我需要根据传入的参数执行一些逻辑。如何访问传递的参数?

4

1 回答 1

0

我最终做了一个肮脏的解决方案基本上,让我的控制器发送一个包含列表和消息的 json 字符串,然后让 Log 函数对其进行反序列化,例如

string message = "this is a test";
List<KeyValuePair<string, string>> udf = new List<KeyValuePair<string, string();
udf.Add(new KeyValuePair<string, string>("Test", "Test"));
JObject obj = new JObject();
obj["Message"] = message;
obj["MyList"] = JArray.FromObject(udf);

日志消息需要反序列化

我确信有一个更清洁的解决方案

于 2020-11-03T19:17:59.700 回答