We use Microsoft.Extensions.Logging for logging abstractions and Serilog as the implementation.
For more documentation on logging, read the references listed at the bottom.
We use the following convention for log levels:
- Trace : Used for parts of a method to capture a flow.
- Debug : Used for diagnostics information.
- Information : Used for general successful information. Generally the default minimum.
- Warning : Used for anything that can potentially cause application oddities. Automatically recoverable.
- Error : Used for anything that is fatal to the current operation but not to the whole process. Potentially recoverable.
- Critical : Used for anything that is forcing a shutdown to prevent data loss or corruption. Not recoverable.
We use Serilog to implement log providers (called sinks).
- We use Serilog.Sinks.Xamarin for native console logging.
- We use Serilog.Sinks.File for file logging.
- We use Serilog.Sinks.XUnit for xUnit test logging.
- We use Serilog.Settings.Configuration to load the log filters from appsettings.json; filters are different between environments.
- We use Serilog.Extensions.Hosting to configure the loggers with
GenericHost
.
The loggers are configured inside the LoggingConfiguration.cs file.
To log, you simply need to get a ILogger
and use the appropriate methods.
var myLogger = myServiceProvider.GetService<ILogger<MyType>>();
myLogger.LogInformation("This is an information log.");
Multiple logging features can be tested from the diagnostics screen. This is configured in LoggersDiagnosticsViewModel and SummaryDiagnosticsViewModel.
- You can test the different log levels / providers.
- You can enable / disable console logging.
- You can enable / disable file logging.
- You can see if a log file exists.
- You can share the logs / app summary by email, etc.