Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net core 访问发送到自定义记录器的参数。NETCore 3.1_.net Core_Ilogger - Fatal编程技术网

.net core 访问发送到自定义记录器的参数。NETCore 3.1

.net core 访问发送到自定义记录器的参数。NETCore 3.1,.net-core,ilogger,.net Core,Ilogger,我正在尝试实现自定义ILogger。NetCore 3.1 CustomLogger类实现ILogger。需要实施的方法之一是: public class AuditLogLogger: ILogger { public IDisposable BeginScope<TState>(TState state) { return null; } public bool IsEnabled(LogLevel logLevel)

我正在尝试实现自定义ILogger。NetCore 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?
}
}
公共类AuditLogLogger:ILogger
{
公共IDisposable BeginScope(州)
{
返回null;
}
公共布尔值已启用(日志级别日志级别)
{
返回true;
}
公共无效日志(日志级别、日志级别、事件ID、事件ID、TState状态、异常、函数格式化程序)
{
//如何访问从控制器发送的参数?
}
}
从我的控制器中,我触发了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);
List udf=new List();
添加(新的KeyValuePair(“测试”);
_logger.LogInformation(“这是一个测试”,udf);

我的代码可以写入日志,但我需要根据传入的参数执行一些逻辑。如何访问传递的参数?

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

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);
string message=“这是一个测试”;

List udf=新列表不清楚您有什么问题。如果您正在实现自己的CustomLogger,那么您可以实现自己的日志和访问参数there@NikitaChayka谢谢你的回复。是的,我实现了自己的自定义记录器。当我触发一个日志函数时,需要访问发送的所有参数。更新了描述。现在更清楚了吗?\n不,不清楚,为什么不从那里实现LoginInformation和访问参数?@NikitaChayka这些参数需要传递到LoginInformation方法中是的,每个都是,那么为什么需要在日志中使用它们呢?为什么不直接在LoginInformation中访问它们并执行逻辑呢