4

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

Community
  • 1
  • 1
Glenn Doten
  • 2,603
  • 3
  • 21
  • 22

1 Answers1

2

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.

Nicholas Blumhardt
  • 30,271
  • 4
  • 90
  • 101
  • Doesn't really answer the question. What does the user have to do with the app.config file to disable the logger? – dylanT Aug 03 '16 at 05:50