Did you ever think that creating log file from your application is so easy?

It could be very useful if your application (doesn’t matter if it’s a Winforms or web application) create a log file and write every important moment in it.

The best package I found is Log4Net. It can be easily used in any kind of application: you can set it up to use Windows Event and/or log file in any of your folder and of course Output window of Visual Studio.

First of all you have to add log4net from NuGet Manager as reference:

Add log4net to your project

The next step is to set it up in App.config (or Web.config if you use it in web application). The order of the new tags are quite important. All the tags will be placed in configuration tag! Either in App.config or Web.config you must place the configSections tag right after the configuration and before anything else!

If you create a Winforms application, the beginning of the App.config file should look like this:

If you create a web application, the beginning of Web.config file should look like this:

Now let’s tell to log4net where we want to save the log. In my example I set up all the 3 possibilities: console, windows event and file. This tag can be anywhere inside configuration tag. You have to modify only the highlighted fields upon your requests.

In this case the application will automatically create a folder named Log in application directory and creates a new file named logfile.log.

Please keep in mind that Windows event will be generated only if you start the Winforms application with highest privileges as far as normal users cannot write windows events!

If you create a web application, by default IIS user can write windows events.

After finishing this set up procedure, let’s initialize log4net in our application. In my Winforms application I wrote only 2 lines in the starting class: Program.cs

An internal static variable will be visible from every Forms, and the XmlConfigurator in Main() method:

Now place 3 buttons on your forms: btnInfo, btnWarning and btnError. These buttons will each create a different event.

On every form where you want to use log4net, you have to “reach out” for it in the constructor and store it in a class level variable:

private readonly ILog _logger;

Constructor of the form:

public Form1()

_logger = LogManager.GetLogger(GetType().Name);

Don’t forget to add log4net in the using section.

The click events now are very simple. You only have to call the corresponding method of the ILog interface:

private void btnInfo_Click(object sender, EventArgs e)
_logger.Info("Info logged");

private void btnWarning_Click(object sender, EventArgs e)
_logger.Warn("Warning logged");

private void btnError_Click(object sender, EventArgs e)
_logger.Error("Error logged");

There are many log viewer application can be found. My favorite one is Microsoft’s cmtrace.exe. You can download System Center Configuration Manager Toolkit here and it’ll be included.
This small exe is part of Microsoft SCCM. It shows you the log file without any viewer configuration in color:

Another log viewer can be downloaded from here.

It’s called Glogg. After installing it you can set it up to show logs in color:

You can use any color or matching pattern you like. After setting it up, you should get some good looking result:

Microsoft’s CMTrace shows you the log in real time, Glogger updates its lines in every 2 seconds (by default).

If your client always tells you that there was an error message in the application but cannot remember the message, you just check the log file and debugging is easy. It’s highly recommended to place a _logger.Error in every catch part of try-catch block.