代码之家  ›  专栏  ›  技术社区  ›  User3250

如何在调试模式下禁用serilog

  •  0
  • User3250  · 技术社区  · 6 年前

    我正在使用 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 回复  |  直到 6 年前
        1
  •  1
  •   nvoigt    6 年前

    你呢 能够

    if(Debugger.IsAttached)
    

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

    #if !DEBUG
       // initialize logger here
    #endif
    

    请注意,这检查调试符号,即通过“调试”配置设置,而不是是否按下 不管怎样。