Serilog mostly "just works" and doesn't require a lot of thought to be put into creating and disposing loggers. However since there are:
File
and RollingFile
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:
Log.ForContext(...)
to receive an ILogger
with additional properties attached; this doesn't need any special close/flush logic, as this will be handled by the parent loggerILogger
using a separate LoggerConfiguration
and use WriteTo.Logger(Log.Logger)
to pipe events to the root logger; in this case the disposal logic below must be followedIf 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. ILogger
s returned from ForContext()
and similar methods don't need any special treatment.
See the Autofac integration example showing how injectable ILogger
s 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