augustoproiete / prism-logging-serilog

Integrate Serilog with Prism in your WPF, UWP, or Xamarin Forms apps
Apache License 2.0
40 stars 2 forks source link
hacktoberfest logging mvvm prism serilog sinks uap uwp wpf xamarin xaml
README.md
Prism.Logging.Serilog

Prism.Logging.Serilog

Integrate [Serilog](https://serilog.net) with [Prism](https://prismlibrary.github.io) in your WPF, UWP, or Xamarin Forms apps. [![NuGet Version](http://img.shields.io/nuget/v/Prism.Logging.Serilog.svg?style=flat)](https://www.nuget.org/packages/Prism.Logging.Serilog) [![Stack Overflow](https://img.shields.io/badge/stack%20overflow-serilog-orange.svg)](http://stackoverflow.com/questions/tagged/serilog) [![Stack Overflow](https://img.shields.io/badge/stack%20overflow-prism-orange.svg)](http://stackoverflow.com/questions/tagged/prism) 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! :star:

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

Getting started :rocket:

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

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.