项目作者: augustoproiete

项目描述 :
Integrate Serilog with Prism in your WPF, UWP, or Xamarin Forms apps
高级语言: C#
项目地址: git://github.com/augustoproiete/prism-logging-serilog.git
创建时间: 2019-01-10T00:52:28Z
项目社区:https://github.com/augustoproiete/prism-logging-serilog

开源协议:Apache License 2.0

下载


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! :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:

  1. Install-Package Prism.Logging.Serilog

Then register Serilog with Prism’s IContainerRegistry using RegisterSerilog():

  1. protected override void RegisterTypes(IContainerRegistry containerRegistry)
  2. {
  3. // ...
  4. containerRegistry.RegisterSerilog();
  5. }

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:

  1. private Serilog.ILogger _logger = Log.Logger;
  2. protected override void RegisterTypes(IContainerRegistry containerRegistry)
  3. {
  4. // ...
  5. containerRegistry.RegisterSerilog(_logger);
  6. }

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:

  1. public partial class App
  2. {
  3. protected override void OnStartup(StartupEventArgs e)
  4. {
  5. // Configure Serilog and the sinks at the startup of the app
  6. Log.Logger = new LoggerConfiguration()
  7. .MinimumLevel.Debug()
  8. .WriteTo.File(path: "MyApp.log")
  9. .CreateLogger();
  10. base.OnStartup(e);
  11. }
  12. protected override void OnExit(ExitEventArgs e)
  13. {
  14. // Flush all Serilog sinks before the app closes
  15. Log.CloseAndFlush();
  16. base.OnExit(e);
  17. }
  18. protected override void RegisterTypes(IContainerRegistry containerRegistry)
  19. {
  20. // Register your ViewModels, Services, etc...
  21. // ...
  22. // Register Serilog with Prism
  23. containerRegistry.RegisterSerilog();
  24. }
  25. protected override Window CreateShell()
  26. {
  27. return Container.Resolve<MainWindow>();
  28. }
  29. }

Release History

Click on the Releases tab on GitHub.


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