README.md |
---|
Integrate Serilog with Prism in your WPF, UWP, or Xamarin Forms apps.
This project provides a custom implementation of Prism's ILoggerFacade
, that forwards messages to a Serilog logger, allowing developers to capture the logging events written in their ViewModels and Services, in Serilog.
If you like or are using this project please give it a star. Thanks!
To use the Prism.Logging.Serilog
, first install the NuGet package:
Install-Package Prism.Logging.Serilog
Then register Serilog with Prism's IContainerRegistry
using RegisterSerilog()
:
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
// ...
containerRegistry.RegisterSerilog();
}
Log events from Prism will be written to Serilog's Log.Logger
by default. Alternatively, you can provide a specific instance of a Serilog.ILogger
:
private Serilog.ILogger _logger = Log.Logger;
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
// ...
containerRegistry.RegisterSerilog(_logger);
}
Prism.Logging.Serilog
does The Right Thing™ :), as you'd expect:
Prism Category | Serilog LogEventLevel |
---|---|
Category.Debug |
LogEventLevel.Debug |
Category.Info |
LogEventLevel.Information |
Category.Warn |
LogEventLevel.Warning |
Category.Exception |
LogEventLevel.Error |
-
The
Priority
set in log messages written via Prism gets forwarded to Serilog as a context property calledPriority
, with the value of the priority as a string. e.g."High"
. -
Log messages forwarded to Serilog have the
SourceContext
property set toPrism.Logging.Serilog.SerilogLoggerFacade
, allowing developers to use use filters, sub-loggers, and minimum level overrides.
In the source code you can find a demo project of a WPF application using Prism and Serilog. The initial setup looks something like this:
public partial class App
{
protected override void OnStartup(StartupEventArgs e)
{
// Configure Serilog and the sinks at the startup of the app
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.File(path: "MyApp.log")
.CreateLogger();
base.OnStartup(e);
}
protected override void OnExit(ExitEventArgs e)
{
// Flush all Serilog sinks before the app closes
Log.CloseAndFlush();
base.OnExit(e);
}
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
// Register your ViewModels, Services, etc...
// ...
// Register Serilog with Prism
containerRegistry.RegisterSerilog();
}
protected override Window CreateShell()
{
return Container.Resolve<MainWindow>();
}
}
Click on the Releases tab on GitHub.
Copyright © 2019-2023 C. Augusto Proiete & Contributors - Provided under the Apache License, Version 2.0.