log4net Tutorial for .NET Logging for beginners

What is log4net?


log4net is a tool to help the programmer output log statements to a variety of output targets. In case of problems with an application, it is helpful to enable logging so that the problem can be located. With log4net it is possible to enable logging at runtime without modifying the application binary.

There are three parts to log4net. There is the configuration, the setup, and the call. The configuration is typically done in the app.config or web.config file.

Logging Levels


There are seven logging levels, five of which can be called in your code. They are :
  1. OFF - nothing gets logged (cannot be called)
  2. FATAL
  3. ERROR
  4. WARN
  5. INFO
  6. DEBUG
  7. ALL - everything gets logged (cannot be called)

These levels will be used multiple times, both in your code as well as in the config file. There are no set rules on what these levels represent (except the first and last).

For this example, I'm using an ASP.Net MVC web application.

Step 01: Add log4net Package

Right-click your project references, then click Manage Nuget packages. It will open up a new window. In here browse log4net. And install. 



Step 02: Web.Config

In your Web.Config file add below line inside of <configSections></configSections>.

<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>

From this line says we have a section in our config file which is called "log4net". 

Now we are going to create the section log4net which we initialized in configSections. Add the following code for that.

<log4net>
    <appender name="FileAppender" type="log4net.Appender.FileAppender">
      <file value="C:\ExampleLogs\Log.txt" />
      <appendToFile value="true" />
      <lockingModel type="log4net.Appender.FileAppender+minimalLock" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date{ABSOLUTE} [%logger] %level -%message%newline%exception" />
      </layout>
    </appender>
    <root>
      <level value="DEBUG" />
      <appender-ref ref="FileAppender" />
    </root>
  </log4net>

Above code says everything about creating log, when to log, what to log, how to log in your application.

an appender is a logging tool. Here we specify what do you want to log to. There are many appenders like FileAppender, ConsoleAppender. In my case, I want to take logs in to a separate file called Logs.txt. It will be located at C:\ExampleLogs. That information is in the second line of the log4net section.

In the layout section in log4net, we define how to layout our log message in the file.

Web.config


Step 03: Code

Edit your code like the following image.

Code - HomeController.cs


Then run your project and see whether you have logs in C:\ExampleLogs folder. Open Logs.txt file.

Logs.txt

If you have logs in your file in the layout you have specified in your web.config file, you have done everything correctly. Congratulations, Now you can use logs in your .Net applications.


Thank you!

Kalpani


Comments