Logging with Serilog for WPF / WPF Core Applications
- Posted by Muthukumar Dharmar
- Categories WPF / WPF Core
- Date August 29, 2021
In this short article, we will have a step by step walkthrough of implementing application logging using the Serilog library for Desktop applications with WPF / WPF Core technologies.
About Serilog
Serilog provides a simple and easy to use solution for application logging into files with .NET applications. It is very easy to setup and has a clean API for logging. For more details please refer here.
Level | Usage |
---|---|
Verbose | Verbose is the noisiest level, rarely (if ever) enabled for a production app. |
Debug | Debug is used for internal system events that are not necessarily observable from the outside, but useful when determining how something happened. |
Information | Information events describe things happening in the system that correspond to its responsibilities and functions. Generally these are the observable actions the system can perform. |
Warning | When service is degraded, endangered, or may be behaving outside of its expected parameters, Warning level events are used. |
Error | When functionality is unavailable or expectations broken, an Error event is used. |
Fatal | The most critical level, Fatal events demand immediate attention. |
Step 1 - NuGet packages
Install the below NuGet packages into your WPF application.
Step 2 - Add logger file configuration into App.config
Include the Serilog logger file configurations like file path, rolling interval, log level, etc., into the AppSettings section of the App.config file.
Step 3 - Setup Serilog instance in Application Startup
We can initialize our Serilog instance by invoking the below method in the OnStartup method of App.xaml.cs file. Which will set up our logger configuration based on our AppSettings.
Step 4 - Log the events
Now we are all setup and ready to write logs events.
Sample log output
Hope you have enjoyed this article 🙂
Tag:.NET, Application Logging, C#.NET, Error Logging, Logger, WPF, WPF Core