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
- Open your WPF project where you want to implement the log4net.
- Go to Solution Explorer, then right click on the project name->open Manage NuGet Package for Solution by clicking on the Manage NuGet Package...
- Click on the Brose tab, then search for log4net and Install the latest version.
- Go to Solution Explorer, then expand the Properties. You will find it just below your project name.
- Double click on the AssemblyInfo.cs to open it and paste the below code just after the AssemblyCulture code.
[assembly: log4net.Config.XmlConfigurator]
- Your code placement should look like below code.
[assembly: AssemblyCulture("")] [assembly: log4net.Config.XmlConfigurator]
- 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>
- 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" />
- 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#
- 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.
- 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));
- 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.
- 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.
Related Articles
- How to use log4net in C# Console Application
- How to use log4net in C#
- log4net ASP NET MVC Example
- log4net Configuration for Multiple Files
Thanks
Tags
wpf