I am using Serilog.Sinks.Elasticsearch
sink for logging as below in 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();
When I am debugging my application in local using VS2017 those events are also getting logged too into Elasticsearch which I don't want.
How do I disable logging in Debug mode?
You could find out whether a debugger is attached, but that's not very reliable (for example you could attach after the initialization of the logger).
if(Debugger.IsAttached)
Your best bet is probably conditional compilation and only initialize your logger when not in debug mode:
#if !DEBUG
// initialize logger here
#endif
Please note that this checks for the DEBUG symbol, that is set through the "Debug" configuration, not whether you pressed F5 or not.