Enable or disable Serilog configuration based on condition
See original GitHub issueI have the following configuration for Serilog. As you can see below, there are 3 sinks registered for Serilog. I want to enable or disable any specific sink based on a condition. Is it possible to do that in Serilog?
Example For on-premises application, I want to disable AppInsights sink and for a cloud application I want to disable EventLog sink. Could anyone please help me with this?
Serilog Configuration
`
"Serilog": {
"Enrich": [ "FromLogContext" ],
"MinimumLevel": {
"Default": "Debug",
"Override": {
"Microsoft": "Information"
}
},
"Using": [ "Serilog.Sinks.ApplicationInsights", "Serilog.Sinks.EventLog", "Serilog.Sinks.File" ],
"WriteTo": [
{
"Name": "ApplicationInsights",
"Args": {
"InstrumentationKey": "02FD2F33-C643-4A6A-8DF6-C980C87B7E1F",
"restrictedToMinimumLevel": "Information",
"telemetryConverter": "Serilog.Sinks.ApplicationInsights.Sinks.ApplicationInsights.TelemetryConverters.TraceTelemetryConverter,Serilog.Sinks.ApplicationInsights"
}
},
{
"Name": "File",
"Args": {
"path": "log.txt",
"rollingInterval": "Day"
}
},
{
"Name": "EventLog",
"Enabled": false,
"Args": {
"manageEventSource": true,
"restrictedToMinimumLevel": "Error",
"source": "KiranKumar",
"logName": "Serilog"
}
}
]
}`
Serilog Version: 2.9.0 .netCore Version: 3.1
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Conditionally disable Serilog Sinks individually
As of Serilog 2.9.0 you can now use .WriteTo.Conditional and specify the condition that defines if the sink will be written to or...
Read more >[Solved]-How to write logs for specific environments only?-C#
Logger = configuration.CreateLogger();. Here are some more examples of configuring Serilog based on conditions: Conditionally disable Serilog Sinks individually ...
Read more >Setting up Serilog in ASP.NET Core - Detailed Beginner ...
This article covers the implementation of Serilog in ASP.NET Core which provides structured logging that is easier to be read by programs.
Read more >Serilog Tutorial for .NET Logging: 16 Best Practices and Tips
With Serilog you can control the format of your logs, such as which fields you include, their order, and etc. Here is a...
Read more >Serilog Best Practices - Ben Foster
Serilog can be configured using either a Fluent API or via the Microsoft Configuration system. We recommend using the configuration system since ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I have the same issue here. My assumption is that I can remove one of the sinks from the “Using” to disable it, even though the"WriteTo" still exists. Is this a misunderstanding of the “Using” section?
@rebeccapowell
Using
works mostly to provide hints on which assemblies Serilog needs to load before the other parts of configuration will become available - it won’t be a reliable way to conditionally turn sinks on/off, unfortunately.