Geeks With Blogs
Swiss Coder .net and stuff...
Here's a short tutorial on how to use log4net in C#

1. Get log4net from the apache website.

2. Open your project with visual studio.

3. Add the reference to your project: You find the reference in the zip that you just downloaded: \bin\net\xxx\release\log4net.dll. xxx is your .net version.

4. Add the Appender section to your app.config. The following code uses a file for the logging:

<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
  </configSections>
  <log4net>
    <root>
      <level value="DEBUG" />
      <appender-ref ref="LogFileAppender" />
    </root>
    <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender" >
      <param name="File" value="log-file.txt" />
      <param name="AppendToFile" value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="10MB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
    </appender>
  </log4net>
</configuration>


5. Use the following code to use the configuration you just added to app.config:

static void Main()
{
      log4net.Config.XmlConfigurator.Configure();
...


6. To log use the following code:

using log4net;
...
private static readonly ILog log = LogManager.GetLogger(typeof(Bar));
log.Debug("this is the first log message");
...


7. If Visual studio doesn't recognize log4net. Configure your project like this:

Visual Studio -> project -> project name properties -> target framework -> .net Framework (not client)


Posted on Sunday, May 13, 2012 6:54 PM | Back to top


Comments on this post: C# Tutorial for log4net

# re: C# Tutorial for log4net
Requesting Gravatar...
Thanks for a very simple and working example.
Left by Amogh on May 13, 2013 3:03 PM

# re: C# Tutorial for log4net
Requesting Gravatar...
Thanks for a very simple and working example!
Left by Andrei on May 30, 2013 1:37 PM

# re: C# Tutorial for log4net
Requesting Gravatar...
Thanks a lot, very well explained
keep up the good work
Step 6 you can either choose
private static readonly ILog log = LogManager.GetLogger(typeof(Bar));
OR
private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

Cheers
Left by Cena on Jul 18, 2013 8:38 AM

# re: C# Tutorial for log4net
Requesting Gravatar...
Yes, good work. Up an running in 3 minutes.
Left by Pergh on Nov 01, 2013 9:39 AM

# re: C# Tutorial for log4net
Requesting Gravatar...
you are the man!
Left by sam on Dec 03, 2013 7:19 AM

# re: C# Tutorial for log4net
Requesting Gravatar...
Simple works thankss!!!
Left by Simble on Dec 06, 2013 3:23 AM

# re: C# Tutorial for log4net
Requesting Gravatar...
very many thanks ,however I used

private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

because i dont know what is "BAR" thanks
Left by siddharth on Jan 07, 2014 6:37 AM

# re: C# Tutorial for log4net
Requesting Gravatar...
This configuration did not work for me and some others judging by the questions on stack overflow. I used this configuration:
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
</configSections>
<log4net>
<root>
<level value="DEBUG" />
<appender-ref ref="LogFileAppender" />
</root>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender" >
<param name="File" value="log-file.txt" />
<param name="AppendToFile" value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
</log4net>
Left by branko on Jan 26, 2015 3:02 PM

# re: C# Tutorial for log4net
Requesting Gravatar...
hey this was pretty awesome !
Left by Raj on Jun 22, 2015 4:14 PM

Your comment:
 (will show your gravatar)


Copyright © huwyss | Powered by: GeeksWithBlogs.net | Join free