Log4Net

by Ronnen

log4net is a great tool for creating Log file. (apache product)
After a short research, I understood how to use that tool for a simple writing-logs task.
Basic steps for using described here, and can be execute in a few minutes.
(there is a small project at the end, so you don't have to 'bother' and implement the steps)

How To Use:

First, You will need the dll file (260 kb).
You can download it from here, or you can find it inside my attached project. (link at the bottom)

Put that dll file in a place that will be easy to reference to.
Add a Reference to that dll from your project.

Add the following code to the Assembly.cs file.

// Configure log4net using the .config file
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
// This will cause log4net to look for a configuration file
// called TestApp.exe.config in the application base
// directory (i.e. the directory containing TestApp.exe)
// The config file will be watched for changes.

In the app.config file:
Inside the 'configSections' tag,
Add a new section-line using 'log4net' name:

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






Add a new section ("log4net") to the same app.config file (Sibling to 'configSections')

<log4net>
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file value="C:\Documents and Settings\Admin\Desktop\Logfile.log" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value=
"%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="FileAppender" />
</root>
</log4net>

















In the class:
Add using log4net;

Define a static logger variable at the top of your class.
Something like this:

private static readonly ILog log = LogManager.GetLogger(typeof(Program));
(Here 'Program' is the class name)

Start using the logger in your class, Enjoy !

My Test class is:

class Program
{
// A static logger variable that will reference the class name ('Program')
private static readonly ILog log = LogManager.GetLogger(typeof(Program));

static void Main(string[] args)
{
log.Info("Entering application.");

for (int i = 0; i < 3; i++)
{
log.DebugFormat("In The loop (i = {0})", i);
log.ErrorFormat("Error message (i = {0})", i);
log.FatalFormat("Fatal message (i = {0})", i);
log.WarnFormat("Warn message (i = {0})", i);
}

log.Info("Exiting application.");
}
}
























The output is:

2009-11-04 09:36:46,078 [10] INFO Program [(null)] - Entering application.
2009-11-04 09:36:46,125 [10] DEBUG Program [(null)] - In The loop (i = 0)
2009-11-04 09:36:46,140 [10] ERROR Program [(null)] - Error message (i = 0)
2009-11-04 09:36:46,140 [10] FATAL Program [(null)] - Fatal message (i = 0)
2009-11-04 09:36:46,140 [10] WARN Program [(null)] - Warn message (i = 0)
2009-11-04 09:36:46,140 [10] DEBUG Program [(null)] - In The loop (i = 1)
2009-11-04 09:36:46,140 [10] ERROR Program [(null)] - Error message (i = 1)
2009-11-04 09:36:46,140 [10] FATAL Program [(null)] - Fatal message (i = 1)
2009-11-04 09:36:46,140 [10] WARN Program [(null)] - Warn message (i = 1)
2009-11-04 09:36:46,140 [10] DEBUG Program [(null)] - In The loop (i = 2)
2009-11-04 09:36:46,140 [10] ERROR Program [(null)] - Error message (i = 2)
2009-11-04 09:36:46,140 [10] FATAL Program [(null)] - Fatal message (i = 2)
2009-11-04 09:36:46,140 [10] WARN Program [(null)] - Warn message (i = 2)
2009-11-04 09:36:46,140 [10] INFO Program [(null)] - Exiting application.



Download

No comments yet

Leave a Reply