augustoproiete / prism-logging-serilog

Integrate Serilog with Prism in your WPF, UWP, or Xamarin Forms apps

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

README.md
Prism.Logging.Serilog

Prism.Logging.Serilog

Integrate Serilog with Prism in your WPF, UWP, or Xamarin Forms apps.

NuGet Version Stack Overflow Stack Overflow

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.

Give a Star! ⭐

If you like or are using this project please give it a star. Thanks!

Getting started 🚀

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);
}

Mapping of Prism Log messages to Serilog

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 called Priority, with the value of the priority as a string. e.g. "High".

  • Log messages forwarded to Serilog have the SourceContext property set to Prism.Logging.Serilog.SerilogLoggerFacade, allowing developers to use use filters, sub-loggers, and minimum level overrides.

Example

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>();
    }
}

Release History

Click on the Releases tab on GitHub.


Copyright © 2019-2023 C. Augusto Proiete & Contributors - Provided under the Apache License, Version 2.0.

About

Integrate Serilog with Prism in your WPF, UWP, or Xamarin Forms apps

License:Apache License 2.0


Languages

Language:C# 100.0%