How to use log4net in WPF

Kailash Chandra Behera | Thursday, July 09, 2020

Introduction

This blog provides complete guidelines How to use log4net in WPF with code example which will create logger file with out using windows log manager and event log manager.

Getting Started

Hare in the below demonstration, provides step by step guidelines to implement of log4net in WPF application and will create a logger file for storing the log messages from C# WPF.The logger file can be used further to trace logging, it is alternate to event log manager and windows log manager.

Demonstration

  1. Open your WPF project where you want to implement the log4net.

  2. Go to Solution Explorer, then right click on the project name->open Manage NuGet Package for Solution by clicking on the Manage NuGet Package...

  3. Click on the Brose tab, then search for log4net and Install the latest version.

  4. Go to Solution Explorer, then expand the Properties. You will find it just below your project name.

  5. Double click on the AssemblyInfo.cs to open it and paste the below code just after the AssemblyCulture code.

     [assembly: log4net.Config.XmlConfigurator]  
    
  6. Your code placement should look like below code.

     [assembly: AssemblyCulture("")]  
     [assembly: log4net.Config.XmlConfigurator]  
    
  7. Open the App.config file of your WPF project, then find the <configSections></configSections> tag, if you don't find added the <configSections> tag, then copy the below code and past it just after the </configuration> tag.

      <configSections>  
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />  
      </configSections>  
    
  8. If the config Sections tag already there, then copy the below code and paste it just after the <configSections> tag.

    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
    
  9. Find the </configuration> tag and paste below code just before the </configuration> tag. This is the log level and appender setting in config for log4net, The code should place between <configuration> and </configuration> tag.

     <log4net>  
       <appender name="TestAppender" type="log4net.Appender.RollingFileAppender">  
        <file value="D:\Scanid\Mlogs.log" />  
        <encoding value="utf-8" />  
        <appendToFile value="true" />  
        <rollingStyle value="Date" />  
        <layout type="log4net.Layout.PatternLayout">  
         <conversionPattern value="%date- %message%n" />  
        </layout>  
       </appender>  
       <root>  
        <level value="All" />  
        <!-- If the following line is not included the log file will not be created even if log4net is configured with this file. -->  
        <appender-ref ref="TestAppender" />  
       </root>  
      </log4net>  
    

    log4net Configuration C#

  10. Note that here in the log4net config, I have used the log4net RollingFileAppender. to use any other appender or more details regarding appenders, visit to my previous blog.

  11. Open the class, where you want implement logging. Copy the below code and paste it just above the controcture of class or where in the place where we declare the property of class.

      private static log4net.ILog Log=log4net.LogManager.GetLogger(typeof(Program));  
    
  12. Invoke the methods of logger class to log messages for your project. For example, to write the exception messages invoke the Error function in the catch block.

  13. Now you are done with the implementation of logging, build your project. see the below code for more..

 public partial class MainWindow : Window  
   {  
     private static log4net.ILog Log = log4net.LogManager.GetLogger(typeof(MainWindow));  
     public MainWindow()  
     {  
       InitializeComponent();  
     }  
     private void Window_Loaded(object sender, RoutedEventArgs e)  
     {  
       Log.Info("Window_Loaded event execution started");  
       try  
       {  
         Log.Info("WPF Hello World!");  
         Log.Info("try block executed");  
       }  
       catch (Exception ex)  
       {  
         Log.Error(ex);  
       }  
       finally  
       {  
         Log.Info("Finally block executed");  
       }  
       Log.Info("Window_Loaded event execution completed");  
     }  
   }  

log4net example

Summary

Here in the above of this blog, we discussed the each steps on How to use log4net in WPF. I hope you have enjoued it a lot.

Thanks