Logger for WPF Application

Using Apache log4net.

Rishabh Verma
Nerd Stuff
4 min readNov 16, 2017

--

Logging is the process of recording application actions and state to a secondary interface.

What is logging and why is it important?

We are recording application actions and state . We are recording these to a secondary interface, which is a file in this case but could be anything like Web-Service, email, etc. The nature of the interface is not so important, what is important is that this is a secondary interface. The user who sees this interface, the support guy looking at the application log, does not have access to the application’s primary interface. This means that what is detailed in this log is very important; it is often the main source of information when diagnosing a problem.

Clearly, logging is an important application function, and one which deserves just a little bit of thought!

Here we will be creating a Logger in WPF Application and log some clicks, but you can do anything, Its just a demo.

→ Create a WPF application in Visual Studio.

Creating a WPF Application
Selecting NuGet Package Manager
  • Select Tools from top menu.
  • Go to NuGet package manager.
  • Select Manage NuGet Packages for Solution.

→ Select log4net by Apache Software.

Selecting log4net from Package Manager

→ Add a config file to project name it as “log4net.config”.

  • Select Application Configuration File from menu.
Adding Config File
  • Rename file to “log4net.config”.
Adding log4net.config file

→ Select log4net.config from solution explorer, see for options in properties :

  • Build Action → Change to “ Content”.
Build Action → Change to “ Content”.
  • Copy to output directory → Change to “copy always”.
Copy to output directory → Change to “copy always”

→ Add this in log4net.config file. Between <configuration></configuration> tag. Change <file value> for changing location of log files.

log4net.config file

→ Open AssemblyInfo.cs from solution explorer and add at the end

Assembly.cs file

→ Configure App.xaml.cs file.

  • Add using log4net; reference.
  • add these codes inside “public partial class App : Application” :
App.xaml.cs File

→ Open MainWindow.cs

Add reference :

Add these codes inside “public partial class MainWindow

Inside public MainWindow() :

Options available for logs :

On Clicking Say Hello Button
On Clicking Exit
Logs

Start Logging and keep learning.

Smiles :)

Find this project on Github → Link to Project

--

--