A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/serilog/serilog/wiki/Lifecycle-of-Loggers below:

Lifecycle of Loggers · serilog/serilog Wiki · GitHub

Serilog mostly "just works" and doesn't require a lot of thought to be put into creating and disposing loggers. However since there are:

some usage patterns work better and more reliably than others.

The easiest way to use Serilog is via the global Log class:

Log.Logger = new LoggerConfiguration()
    .WriteTo.File(@"myapp\log.txt")
    .CreateLogger();

Log.Information("Hello!");

// Your application runs, then:

Log.CloseAndFlush();

If you do this, only configure the logger once and then use it for the lifetime of the application.

To create more specialized loggers:

If you do not wish to use the static Log class, you will use LoggerConfiguration to create an ILogger.

using (var log = new LoggerConfiguration()
        .WriteTo.File(@"myapp\log.txt")
        .CreateLogger())
{
    log.Information("Hello again!");

    // Your app runs, then disposal of `log` flushes any buffers
}

In this case, Log.CloseAndFlush() is not used. Instead, when the application no longer needs the logger it is disposed.

Only the root logger created from a LoggerConfiguration needs to be treated this way. ILoggers returned from ForContext() and similar methods don't need any special treatment.

See the Autofac integration example showing how injectable ILoggers can be used with the Autofac IoC container. Please raise an issue to update this page with instructions for alternative containers.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4