question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Enable or disable Serilog configuration based on condition

See original GitHub issue

I 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:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
rebeccapowellcommented, Mar 14, 2021

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?

0reactions
nblumhardtcommented, Mar 14, 2021

@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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found