【问题标题】:How to configure Serilog sink wrapper and wrapped sinks如何配置 Serilog sink wrapper 和 Wrapped sinks
【发布时间】:2021-08-28 02:38:31
【问题描述】:

按照C. Augusto ProieteHow to change the LogLevel of specific log events in Serilog? 的回答,下面的代码可以工作,但是将这个配置移动到配置文件的推荐方法是什么。保留Serilog.Settings.Xml 是我们的首选,但我们会考虑其他选项。

另外,写入两个接收器是否有问题?还是已经解决了?

Log.Logger = new LoggerConfiguration()
    .ReadFrom.Xml(serilogXmlFilePath)
    .WriteTo.LogLevelModifierSink(writeTo => {
        writeTo.File(
            outputTemplate: "{Timestamp:HH:mm:ss.fff} [{Level:u3}] {SourceContext} {Message:lj}{NewLine}{Exception}{NewLine}{ThreadId}",
            path: @"C:\inetpub\logs\serilog.txt",
            rollingInterval: RollingInterval.Day,
            shared: true);
        writeTo.Seq("http://localhost/seq/");
    })
    .CreateLogger();

【问题讨论】:

    标签: serilog


    【解决方案1】:

    Serilog.Settings.XmlSerilog.Settings.Configuration 等 Serilog 配置扩展都以相同的方式工作:它们尝试在您的配置中找到与接收器名称和参数匹配的扩展方法

    即当你做这样的事情时:

    <?xml version="1.0" encoding="utf-8" ?>
    <serilog>
      <using>
        <add name="Serilog.Sinks.File" />
      </using>
      
      <writeTo>
        <sink name="File">
          <arg name="path" value="log.txt" />
        </sink>
      </writeTo>
    </serilog>
    

    Serilog 配置扩展尝试运行如下所示的方法:

    public static LoggerConfiguration File(
        this LoggerSinkConfiguration loggerSinkConfiguration, string path)
    {
       // ...
    }
    

    在这个例子中,是Serilog.Sinks.File提供的方法。

    在您的情况下,鉴于您正在创建自定义接收器,您还必须提供一个自定义扩展方法,该方法可以接收配置接收器所需的所有参数,然后进行相应设置。所以它看起来像这样:

    <?xml version="1.0" encoding="utf-8" ?>
    <serilog>
    
      <using>
        <add name="Namespace.Where.Your.Extension.Method.Is" />
      </using>
    
      <writeTo>
        <sink name="LogLevelModifier">
          <arg name="filePath" value="C:\inetpub\logs\serilog.txt" />
          <arg name="seqUrl" value="http://localhost/seq/" />
        </sink>
      </writeTo>
    
    </serilog>
    

    // Extension method to hook the wrapper into the configuration extension
    public static class LoggerSinkConfigurationLogLevelModifierExtensions
    {
        public static LoggerConfiguration LogLevelModifier(
            this LoggerSinkConfiguration loggerSinkConfiguration,
            string filePath = null,
            string seqUrl = null)
        {
            return LoggerSinkConfiguration.Wrap(loggerSinkConfiguration, sink =>
                new LogLevelModifierSink(sink), writeTo =>
                {
                    if (!string.IsNullOrWhiteSpace(filePath))
                    {
                        writeTo.File(filePath);
                    }
                    
                    if (!string.IsNullOrWhiteSpace(seqUrl))
                    {
                        writeTo.Seq(seqUrl);
                    }
    
                    // etc.
    
                }, LevelAlias.Minimum, null);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多