如何在调试模式下禁用serilog

时间:2018-09-26 16:19:18

标签: c# serilog

我正在Serilog.Sinks.Elasticsearch中使用Startup.cs接收器进行记录,如下所示:

Log.Logger = new LoggerConfiguration()
.Enrich.FromLogContext()
.Enrich.WithExceptionDetails()
  .WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri(settings.ElasticSearchUrl))
  {
      ModifyConnectionSettings = conn =>
      {
          var httpConnection = new AwsHttpConnection(settings.Region
              , new StaticCredentialsProvider(new AwsCredentials
              {
                  AccessKey = settings.AccessKey,
                  SecretKey = settings.SecretKey,
              }));
          var pool = new SingleNodeConnectionPool(new Uri(settings.ElasticSearchUrl));
          var conf = new ConnectionConfiguration(pool, httpConnection);
          return conf;
      },
      ConnectionTimeout = new TimeSpan(0, 10, 0),
      IndexFormat = "test-{0:yyyy.MM}"
  })
.CreateLogger();

当我使用VS2017在本地调试我的应用程序时,这些事件也被记录到我不想要的Elasticsearch中。

如何在调试模式下禁用日志记录?

1 个答案:

答案 0 :(得分:1)

可以查明是否已连接调试器,但这不是很可靠(例如,可以在记录器初始化后附加调试器。)

if(Debugger.IsAttached)

您最好的选择可能是条件编译,并且仅在不处于调试模式时才初始化记录器:

#if !DEBUG
   // initialize logger here
#endif

请注意,这将检查通过“调试”配置设置的调试符号,而不是是否按 F5