Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to turn off Serilog?

Tags:

c#

serilog

We are using Serilog to log items into a db with a Windows service, and the users wanted to be able to do a manual run, so we made a button (on a web page) to make a call to the same code (as a module, not the service itself).

When we added in the code to initialize the log so the code will continue adding to the db log table, it also logs all the http traffic after that as well. So after this code runs we want to 'turn off' the Logger running on the Webserver. Is there an easy way to do that?

Log.Logger = new LoggerConfiguration()
       .WriteTo.MSSqlServer(ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString,
                "LOGS")
       .CreateLogger();
like image 742
bowlturner Avatar asked Jun 15 '15 15:06

bowlturner


People also ask

What is the use of Serilog?

Serilog is a third-party logging library that plugs into the default ILogger of our application with its own implementations. It enables the developers to log the events into various destinations like console, file, database, and more.

What is Serilog C#?

Serilog is a logging library for . NET and C# that allows for more detailed and structured logging than the default . NET logging library. Serilog can be used to log information about application events, errors, and performance metrics.

What is Minimumlevel in Serilog?

Serilog defines several levels of log events. From low to high, these are Verbose , Debug , Information , Warning , Error and Fatal . You can set the minimum level you want to log, meaning that events for that level or higher will be logged.

What is structured logging in Serilog?

Structured logging is a modern approach where logging events are treated as structured data rather than text, for example: { “payload”: { “order”: { “id”: 10 }, “message”: “OrderId: 10 placed successfully” }


1 Answers

Log levels can be modified at runtime with LoggingLevelSwitch:

var ls = new LoggingLevelSwitch();

Log.Logger = new LoggerConfiguration()
   .MinimumLevel.ControlledBy(ls)
   .WriteTo.MSSqlServer(...)
   .CreateLogger();

Logging will initially be at the Information level, you can change this via the switch.

Serilog doesn't define an Off level, but you can approximate it with:

ls.MinimumLevel = ((LogEventLevel) 1 + (int) LogEventLevel.Fatal);

...to turn logging off.

like image 113
Nicholas Blumhardt Avatar answered Oct 07 '22 02:10

Nicholas Blumhardt