简体   繁体   English

如何创建支持配置逻辑的 Serilog Sink 扩展

[英]How to create a Serilog Sink Extension that will support configuration logic

I'm trying to wrap the configuration of the Serilog.Sinks.Elasticsearch sink so that my log configuration looks something like this.我正在尝试包装 Serilog.Sinks.Elasticsearch 接收器的配置,以便我的日志配置看起来像这样。

    var logger = Log.Logger = new LoggerConfiguration()
          .ReadFrom.AppSettings()
          .Enrich.FromLogContext()
          .Enrich.WithMachineName()
          .Enrich.WithProcessId()
          .Enrich.WithThreadId()
          .WriteTo.ElasticsearchIfEnabled() // <--- Chained Sink
          .WriteTo.Async(
              a => a.File(
                  filePath,
                  outputTemplate: CanonicalTemplate,
                  retainedFileCountLimit: 31,
                  rollingInterval: RollingInterval.Day,
                  rollOnFileSizeLimit: true,
                  buffered: true,
                  flushToDiskInterval: TimeSpan.FromMilliseconds(500)))
        .CreateLogger();

My inspiration came from this answer , but I'm stuck with the internal condition logic, specifically if the the sink is disabled.我的灵感来自这个答案,但我坚持内部条件逻辑,特别是如果接收器被禁用。

Here is what I have so far.这是我到目前为止所拥有的。

public static class MyConfigExtensions
{
    public static LoggerConfiguration ElasticsearchIfEnabled(this LoggerSinkConfiguration loggerSinkConfiguration)
    {
        var isElasticSinkEnabled = GetThisValue();

        if (isElasticSinkEnabled)
        {
            // Code necessary to configure sink....

            return loggerSinkConfiguration.Sink(new ElasticsearchSink(elasticSearchSinkOptions));
        }

        return loggerSinkConfiguration; // <-- what is passed here that will propagate the configuration?
    }
}

So my question is how to propagate the configuration when the internal sink is disabled, and I need to pass the original configuration on to the next sink in the chain?所以我的问题是如何在禁用内部接收器时传播配置,并且我需要将原始配置传递给链中的下一个接收器?

There are a few properties of the configuration object, Logger, Sink, and Sink<> but I'm unsure of which to use, and more importantly, correctly.配置对象有一些属性,Logger、Sink 和 Sink<>,但我不确定要使用哪个,更重要的是,是否正确。

This will work:这将起作用:

return loggerSinkConfiguration.Sink(new LoggerConfiguration().CreateLogger());

But, conditionally configuring the logger would, too:但是,有条件地配置记录器也会:

var configuration = new LoggerConfiguration()
      .ReadFrom.AppSettings()
      .Enrich.FromLogContext()
      .Enrich.WithMachineName()
      .Enrich.WithProcessId()
      .Enrich.WithThreadId();

if (isElasticSinkEnabled)
    configuration.WriteTo.ElasticsearchIfEnabled();

var logger = Log.Logger = configuration.WriteTo.Async(
          a => a.File(
              filePath,
              outputTemplate: CanonicalTemplate,
              retainedFileCountLimit: 31,
              rollingInterval: RollingInterval.Day,
              rollOnFileSizeLimit: true,
              buffered: true,
              flushToDiskInterval: TimeSpan.FromMilliseconds(500)))
    .CreateLogger();

I know I'm late to the party here.我知道我在这里参加聚会迟到了。 But this is how I did it to keep the fluent approach.但这就是我保持流畅方法的方式。

  public static class SerilogExtensions
    {
        public static LoggerConfiguration If(this LoggerConfiguration loggerConfiguration, Func<bool> condition, Func<LoggerConfiguration, LoggerConfiguration> func)
        {
            return !condition.Invoke() ? loggerConfiguration : func.Invoke(loggerConfiguration);
        }
    }

Call with:致电:

Log.Logger = new LoggerConfiguration()
   .WriteTo.Debug()
   .If(() => true, x => x.WriteTo.Console())
   .CreateLogger();

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

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