如何修改 Serilog 日志级别以匹配 Log4Net? [英] How do I modify Serilog Log Levels to match up with Log4Net?

查看:84
本文介绍了如何修改 Serilog 日志级别以匹配 Log4Net?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在将日志框架从 Log4Net 交换到 Serilog,我们在其中注销到文本文件.

So I am in the process of swapping logging frameworks from Log4Net to Serilog where we log out to a text file.

我希望文本文件的格式与我之前的完全相同,以便用户无缝迁移.

I want the format of the text file to be exactly the same as I had previously, so users have a seamless migration.

因此,使用 Serilog 文件接收器,我可以修改 outputTemplate 以满足我的需要,但是我无法使 Serilogs 日志级别与 Log4Net 中的完全相同.

So with the Serilog File Sink, I am able to modify the outputTemplate to suit my needs, however I am unable to get Serilogs log level to match exactly the same as in Log4Net.

我希望:

  • 错误
  • 信息
  • 警告
  • 调试
  • 等等...

通过阅读 outputTemplates 的文档https://github.com/serilog/serilog/wiki/Configuration-基础知识#output-templates

By reading the documentation for outputTemplates https://github.com/serilog/serilog/wiki/Configuration-Basics#output-templates

对于更紧凑的级别名称,请分别使用 {Level:u3} 或 {Level:w3} 等格式表示三个字符的大写或小写级别名称.

For more compact level names, use a format such as {Level:u3} or {Level:w3} for three-character upper- or lowercase level names, respectively.

所以我尝试了以下方法来尝试获取 5 个大写字母 {Level:u5} 并返回:

So I tried the following to try and get 5 letter uppercase {Level:u5} and got back:

  • 信息
  • 错误
  • 华尼
  • 调试
  • 等等.

有人可以给我指点一下吗?

Is anyone able to give me a pointer on this please?

推荐答案

在挖掘 Serilog 的源代码后,我发现了 u3、w3 等的速记格式发生的地方,并提交了一个 PR,以便 5 个字符长度的日志级别将根据我的需要输出.

After digging through the source code of Serilog, I found where the shorthand formatting for u3, w3 etc was happening and submitted a PR so that 5 character length log levels would be output as I needed.

经过 Serilog 维护者的礼貌回应,他们给了我一个更简单的解决方案,即实现我自己的 ILogEventEnricher 以向日志添加一个新属性,例如 {Log4NetLevel}其中,Enricher 代码可以映射 Serilog 级别,以 Log4Net 将使用的确切格式输出.

After a polite response from Serilog maintainers, they gave me a much simpler solution of implementing my own ILogEventEnricher to add a new property to the log such as {Log4NetLevel} where the Enricher code can map Serilog Levels to be output in the exact format that Log4Net would use.

然后我可以更新我的 outputTemplate 以使用我的新属性 {Log4NetLevel} 而不是 {Level:u5}

Then I can update my outputTemplate to use my new property {Log4NetLevel} as opposed to {Level:u5}

这是一个基本的例子

/// <summary>
/// This is used to create a new property in Logs called 'Log4NetLevel'
/// So that we can map Serilog levels to Log4Net levels - so log files stay consistent
/// </summary>
public class Log4NetLevelMapperEnricher : ILogEventEnricher
{
    public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
    {
        var log4NetLevel = string.Empty;

        switch (logEvent.Level)
        {
            case LogEventLevel.Debug:
                log4NetLevel = "DEBUG";
                break;

            case LogEventLevel.Error:
                log4NetLevel = "ERROR";
                break;

            case LogEventLevel.Fatal:
                log4NetLevel = "FATAL";
                break;

            case LogEventLevel.Information:
                log4NetLevel = "INFO";
                break;

            case LogEventLevel.Verbose:
                log4NetLevel = "ALL";
                break;

            case LogEventLevel.Warning:
                log4NetLevel = "WARN";
                break;
        }

        logEvent.AddPropertyIfAbsent(propertyFactory.CreateProperty("Log4NetLevel", log4NetLevel));
    }
}

然后我只需要更新我的 Serilog 配置以使用我的浓缩器,一个更干净的解决方案.感谢 Serilog 团队!

Then I simply need to update my Serilog configuration to use my enricher, a much cleaner solution. Thanks Serilog team!

Log.Logger = new LoggerConfiguration()
    .MinimumLevel.Debug()
    .Enrich.With<Log4NetLevelMapperEnricher>()
    .WriteTo.File(
        $@"{AppDomain.CurrentDomain.BaseDirectory}\App_Data\Logs\UmbracoTraceLog.{Environment.MachineName}.txt",
        rollingInterval: RollingInterval.Day,
        restrictedToMinimumLevel: LogEventLevel.Debug,
        retainedFileCountLimit: null,
        outputTemplate:
        "{Timestamp:yyyy-MM-dd HH:mm:ss,fff} [P{ProcessId}/D{AppDomainId}/T{ThreadId}] {Log4NetLevel}  {Message:lj}{NewLine}{Exception}")
    .CreateLogger();

这篇关于如何修改 Serilog 日志级别以匹配 Log4Net?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆