Whenever you have the need for debugging some application that by whatever reason cannot be properly debugged in Visual Studio, or even because your application requires some kind of logging/auditing functionality, instead of developing your own tool you have the option to use some existent logging frameworks.

One that I have tried and enjoyed its features and ease of use is log4net – http://logging.apache.org/log4net/.

Like they say on their webpage, "log4net is a tool to help the programmer output log statements to a variety
of output targets. log4net is a port of the excellent log4j framework to the
.NET runtime
". Its features are present here: http://logging.apache.org/log4net/release/features.html.

One of its great features is the ability to control and configure the logging framework entirely by an XML file. This allows the building and deployment of applications that at run-time can dynamically change its logging configuration. For this you should initialize to log in the following manner (in the example the XML configuration file is received in the application arguments):

// Import log4net classes.
using log4net;
using log4net.Config;

public class MyApp
{
    private static readonly ILog log = LogManager.GetLogger(typeof(MyApp));

    static void Main(string[] args)
    {
        // BasicConfigurator replaced with XmlConfigurator.
        XmlConfigurator.Configure(new System.IO.FileInfo(args[0]));      
    }
}

After this, you can check if the initialization went ok by testing the value of: _log.Logger.Repository.Configured .

Now it is real easy to start logging things. You just have to do:

            log.Info("Entering application.");

        log.Debug("Entering application.");

and the framework takes care of the rest, logging acording to the configuration that you provided. Note that in the above code I am already logging into two different log levels, just as an example for showing you, that while developing I can have the level configured at DEBUG, and at production I can reduce the level to INFO, or even NONE, if I dont want any logging at all. If a problem occurs in production, I can just as easily change the log level to DEBUG again, and without having to change anything in the application, the logs start being generated. 

As a final example of the capabilities of the framework, I just want to show here a possible XML configuration file:

<?xml version="1.0" encoding="utf-8" ?>
<log4net>   
    <!– Rolling File Appender –>
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
        <file value="log-file.txt" />
        <appendToFile value="true" />
        <maximumFileSize value="1KB" />
        <maxSizeRollBackups value="0" />
        <staticLogFileName value="true" />           
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date [%thread] %-5level %logger – %message%newline" />
        </layout>
    </appender>

    <!– Console appender –>
    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
        <target value="Console.Error" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%message%newline" />
        </layout>
    </appender>
   
    <root>
        <level value="DEBUG" />
        <appender-ref ref="RollingFileAppender" />
        <appender-ref ref="ConsoleAppender" />
    </root>   
</log4net> 

In this configuration, I define two different appenders (RollingFile and Console), each with its own configuration. At the root level, I define the logging level at DEBUG (so I want to see everything until this level), and that the logging should be done to the two defined appenders. So, when I log something with this configuration, the log is done to a file and to the application's console.

So, you see that it is easy to add logging capabilities to your application. 

LEAVE A REPLY

Please enter your comment!
Please enter your name here