Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable Serilog for product release

Tags:

c#

config

serilog

Is the lack of any defined sinks the proper way to disable Serilog?

I did see this question:

How to turn off Serilog?

But mine's more about how to configure Serilog when releasing a product, so that by default no log files are created when the product is used, and the calls to Serilog have as little overhead as possible.

We configure Serilog using app/web config files, so in a Debug build it looks something like this:

<add key="serilog:minimum-level" value="Verbose" />
<add key="serilog:write-to:RollingFile.pathFormat"
     value="%ProgramData%\Product\debug\AppName-{Date}.log" />
<add key="serilog:write-to:RollingFile.retainedFileCountLimit" value="10" />

These lines are processed like so:

new LoggerConfiguration().ReadFrom.AppSettings()...

When we generate the app/web config files to ship with the product, is it sufficient to remove any sinks from the config files to effectively disable Serilog?

I guess I'm wondering if there's some sort of "off" setting so that the very first thing a call to Serilog does is "if (off) return;", or something along those lines? Or is the lack of any defined sinks basically the same thing? My goal is to configure things so all the calls to Serilog are as fast as possible when we have logging "disabled".

like image 939
Glenn Doten Avatar asked Sep 14 '25 07:09

Glenn Doten


1 Answers

The best option is to simply do nothing; don't create a LoggerConfiguration or assign Log.Logger at all. This will result in all Log methods doing as little work as possible.

If you need an ILogger, just grab Log.Logger and pass that around - it will also do nothing by default, unless you've assigned something to it.

like image 158
Nicholas Blumhardt Avatar answered Sep 17 '25 03:09

Nicholas Blumhardt