2

I'm developing WPF .Net project in VS 2019 not .Net Core. I would like logging exceptions. I have some questions about it:

  1. May I will add Serilog to that kind of application?
  2. What Serilog package I need select to install? In VS in NuGet Packager Manager I found a bunch of Serilog packages.
  3. That is multi layers architecture project. In what layer the Sirelog must to be installed? In Data or UI layer?
  4. How to setup, configure and use Serilog?
  5. How to configure Serilog globally that catch exceptions and log them to a file or database from any forms of the solution?

I will appreciate for detail explanation and samples.

Thanks

eugz
  • 39
  • 1
  • 6

1 Answers1

7

May I will add Serilog to that kind of application?

I think you meant to ask "Can I add Serilog to a WPF project?" and the answer is "yes".


What Serilog package I need select to install? In VS in NuGet Packager Manager I found a bunch of Serilog packages.

Read the documentation. Each package serves a specific purpose and their corresponding readme files will let you know if you need them or not.


That is multi layers architecture project. In what layer the Serilog must to be installed? In Data or UI layer?

"Multi-layer architecture project" is not a descriptive term: every competently written non-trivial project has layers.

Serilog supports different styles of use-cases:

  • The best style, in my opinion and (as far as I know) the opinion of Serilog's maintainers, is to use serilog-extensions-logging:

    • First, use a DI container in your application (namely Microsoft.Extensions.DependencyInjection - you can use others, but if you're new to DI then you should use Microsoft.Extensions.DependencyInjection).
    • Then add NuGet package references to Microsoft.Extensions.Logging and Microsoft.Extensions.Logging.Abstractions.
    • Then add NuGet package references to Serilog and Serilog.Extensions.Logging - this lets you use Serilog as a backend for Microsoft.Extensions.Logging. You should avoid using Serilog directly in application code, instead perform all logging via Microsoft.Extensions.Logging.ILogger (not to be confused with Serilog's Serilog.ILogger!).
    • For more details on how to use serilog-extensions-logging read their documentation. Note that while a lot of the documentation refers to .NET Core, fact is you can use it with .NET Framework 4.7.2 projects just fine, provided you have the Microsoft.Extensions.DependencyInjection and Microsoft.Extensions.Logging packages working.
  • But Serilog also supports its global static Serilog.Logger.Log object property which allows any code from anywhere to log data.

    • But you should avoid using this approach as it makes it harder to test your code. Also be careful about using this approach with a logging backend that performs synchronous IO, as high-frequency logging will slow down your application considerably.

How to setup, configure and use Serilog?

Read the documentation!


How to configure Serilog globally that catch exceptions and log them to a file or database from any forms of the solution?

That depends on your application's project platform. Serilog does have NuGet packages that will set-up global exception logging for specific platforms but in my opinion you should not just let Serilog handle that for you and do nothing else. Just because Serilog will log an uncaught exception doesn't mean you shouldn't act on it yourself.

That said, in WPF specifically, you'll need to handle two events:

  • AppDomain.UnhandledException
    • This actually applies to all .NET projects, not just WPF.
  • Application.DispatcherUnhandledException.
    • This is specific to WPF. This is where most (but not all) uncaught exceptions thrown inside WPF-triggered event-handlers (and model-binding too?) will be caught.

Note that WPF has a lot of runtime reporting of non-fatal errors and warnings (such as problems with View-Model Binding, which may or may not be exceptional based on your application's design), in which case you need to implement an TraceListener and log anything notable to Serilog yourself.

Dai
  • 141,631
  • 28
  • 261
  • 374