简体   繁体   English

记录器提供程序:如何实现自定义格式化程序

[英]Logger provider: How can I implement a custom formatter

I implemented a custom logger provider. 我实现了自定义记录器提供程序。 My custom logger has this signature: 我的自定义记录器具有以下签名:

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

How is formatter being passed? formatter如何通过?

And how can I implement a custom formatter ? 我如何实现自定义formatter Say, if I want it to be formatting everything in JSON 比方说,如果我希望它在JSON中格式化所有内容

I'm new to Net Core and I don't fully understand how this works. 我是Net Core的新手,我不完全了解它是如何工作的。

The Func<TState, Exception, string> formatter function that is being passed to your function is basically just a utility function to convert the state into a single string message. 传递给函数的Func<TState, Exception, string> formatter函数基本上只是一个实用函数,用于将状态转换为单个字符串消息。 Inside your logger, you are basically just expected to call formatter(state, exception) to get the formatted message that should be logged. 在记录器内部,基本上只需要调用formatter(state, exception)来获取应该记录的格式化消息。

Usually, you do not really need to care about the function, other than calling it to get the formatted message, so that's what all loggers usually do. 通常,除了调用它以获取格式化消息之外,您实际上并不需要关心该函数,因此这是所有记录器通常所做的事情。 For the purpose of a JSON logger, you could just ignore it completely, or at least also export the formatted message so it's there as a readable string. 出于JSON记录器的目的,您可以完全忽略它,或者至少还导出格式化消息,以便它作为可读字符串存在。

A quick and dirty JSON logger's Log method could actually look like this: 快速而又脏的JSON记录器的Log方法实际上看起来像这样:

public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
{
    var jsonLine = JsonConvert.SerializeObject(new {
        logLevel,
        eventId,
        parameters = (state as IEnumerable<KeyValuePair<string, object>>)?.ToDictionary(i => i.Key, i => i.Value),
        message = formatter(state, exception),
        exception = exception?.GetType().Name
    });

    // store the JSON log message somewhere
    Console.WriteLine(jsonLine);
}

As you can see, it's not that much magic to generate the JSON object here. 正如您所看到的,在这里生成JSON对象并不是那么神奇。

First, this doesn't really have anything to do with .NET Core. 首先,这与.NET Core没有任何关系。 It looks like you're using Microsoft.Extensions.Logging , which is just a generic logging facade that you can use pretty much anywhere, .NET Core app or not. 看起来你正在使用Microsoft.Extensions.Logging ,它只是一个通用的日志记录,你几乎可以在任何地方使用.NET Core app。 It just happens to be the default used by ASP.NET Core apps; 它恰好是ASP.NET核心应用程序使用的默认值; that's all. 就这样。

Second, if you don't know how to implement this method, it may not be a good idea to be creating your own logging provider. 其次,如果您不知道如何实现此方法,那么创建自己的日志记录提供程序可能不是一个好主意。 What is it that you're trying to do that there's not already an existing adapter for? 您尝试做的是什么,还没有现有的适配器?

That said, the Func<TState, Exception, string> type means that it needs a function that accepts a parameter of type TState and one of type Exception and returns a string. 也就是说, Func<TState, Exception, string>类型意味着它需要一个接受类型为TState的参数和一个Exception类型的TState并返回一个字符串。 Essentially: 实质上:

public string MyFormatter<TState>(TState state, Exception exception)
{
    // create some string
    return someString;
}

Or, it could be implemented via a lambda: 或者,它可以通过lambda实现:

(state, exception) => exception?.Message ?? state.ToString();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM