If you need logging in your projects, log4Net is probably a good choice. Log4Net is a great tool and a defacto-standard how logging should be done. It is simple, powerful and extensible.

But, we need to know some things to setup it correctly and make it running.

I don’t want to write a big story about it, just the most important things to get it work. I will provide some links for further education.

 

The Configuration

First, you have to decide where you want to put your configuration. In a desktop application it could be in the app.config file or web.config in web application. I like the third option, to put it in a separate file to make everything cleaner. I put the file (lets say “Log4Net.config”) to the root of the project and say in the properties to be always copied.

To make your configuration work, you have to setup it in your AssemblyInfo.cs

If you have it in the app.config or web.config, you can leave the Configfile.

 

The ConfigurationFile

 Explanation

ConfigSections

In a configfile, where (potentially) will have more than one configuration, you have to specify where the log4net configuration will be housed. Here is the section name “log4net”.

Appenders

I used in my sample configuration file two appenders, one for the console and one to write to a file.  Find here more usable appenders.

I used in my example the ColoredConsoleAppender

and the RollingFileAppender

I gave them both a name. You can choose one as you like. Rember it, you will need it in the root section.

The ColoredConsoleAppender is simply a ConsoleAppender, but you can specify which color each level should have.

The RollingFileAppender is similar to the FileAppender, but here you can specify the max file size and how many files should be created before log4net starts deleting it. This should be used if possible, because a file can get huge also in small applications. You also have to specify a Filename and a Path if it is not in the root of the application execution. We specify that one file is maximum 5 MB  and there will be created 5 files. The oldest one will be deleted after a new file is created.

 

Filter

You have these loggin levels:

In an appender, you can specify for example with the LevelRangeFilter to not log debug level:

With this, i am saying that the minimum level is Info, and max is Fatal, so Debug wont be logged.  Here you will find a list of possible filters.

Note, that in the above example, the DEBUG is automatically exluded. This is not true for all filters. The LevelRangeFilter has a denyfilter included.

For example, the StringMatchFilter and LevelMatchFilter does not have one included.

When you use the above config, all levels would be logged instead of just error. To make it work correctly, you have to add the DenyAllFilter

If the DenyAllFilter is the only filter, nothing will be logged, but if you have the LevelMatchFilter, nothing will be logged, but the Error level will be.

Layout

Inside of each appender must be a layout section. This may be a bit different depending on the type of appender being used, but the basics are the same. You need a type that specifies how the data will be written.

To configure the conversionPattern and say how the logs are written to console or the file, here is a list of all possibilities.

Use the logger

So, now we are ready to use it. If you do not have the log4net dll in your reference, it is a good time to add them 🙂

There are now several ways how to use the logger. You can create it in each class which will use the log4net or, what i prefere, write a wrapper for the log4net and instantiate the log4netWrapperclass and use the methods given there. In this way, you are more flexible when something changes. You do not have to change each class using log4net, just change the wrapper.

This is my sampleWrapper:

You see, you have always two ways of logging. You can pass just the message or extend it also with an exception.

You can then instantiate your wrapper this way,

or lets say this way

Sure, you could also use generics and other ways.. This is just a small, general sample you can use.

 

Thats it, use your logger 🙂

 

Setup Log4Net with your C# Project
Tagged on:         

4 thoughts on “Setup Log4Net with your C# Project

  • February 11, 2015 at 6:25 pm
    Permalink

    Excellent tutorial !!

    Reply
  • February 21, 2017 at 9:34 am
    Permalink

    hi Damir, great Post. Crispy, i have only one query.. please help me out.. thing is when i use some wrapper like you used at last how i ll be able to log some data to SQL, some data to Mongo Db, some to File i can inject classes name (where to be placed in bcj in we specify class name which is binded with appender-ref ) but what about root ???

    My concerned if you dont get above.

    1. I want to log data in different database (SQL, MONGOdb) .
    2. want to create wrapper and inject class name as dependecy injection and log accordingly.
    3. inject these class is configurable / customisable from web.config

    how can i do that??

    Reply

Leave a Reply

Follow

Get every new post on this blog delivered to your Inbox.

Join other followers:

Welcome Damir Kusar

Log in

Lost your password?
%d bloggers like this: