Humprey Evangelista Cogay

My Programming Adventures

Starting Log4Net

One of the task that a developer needs to accomplish is to code a module for logging, and creating it from scratch will take some of your precious time.

 So  to be a little more productive, we can use a tested library like Log4Net.

I have browsed for some pages and found a lot of variations. So putting it all together, here is the routine that i used to do...

Note:

   the configuration file for Log4Net is found on another Config File, not on the App.Config 

 

1. Download Log4net

     http://logging.apache.org/log4net/

2. Extract the downloaded file in your preferred destination

3. Add a Reference to Log4Net.Dll (found inside "bin\net\2.0\release")

4. Create a new Config File named Log4Net.Config and paste the code below

 

<?xml version="1.0" ?>

 

                <log4net>

                                <appender name="FileAppender" type="log4net.Appender.RollingFileAppender">

                                                <file value="c:\log\" />

                                                <appendToFile value="true" />

                                                <datePattern value=".yyyyMMdd." />

                                                <rollingStyle value="Date" />

                                                <param name="StaticLogFileName" value="false" />

 

                                                <layout type="log4net.Layout.PatternLayout">

                                                                <conversionPattern value="%date [%thread] %exception %username %-5level %logger [%property{NDC}] - %message - %identity% newline" />

                                                </layout>

                                </appender>

                                <root>

                                                <level value="ALL"/>

                                                <appender-ref ref="FileAppender"/>

                                </root>

                </log4net>

for more information about the Log4Net Configuration you can visit

http://logging.apache.org/log4net/release/manual/configuration.html

 

------------------------------------------------------------------------------

5. Open AssemblyInfo.Cs and Add the bolded line below just after the last [assembly line

 

[assembly: AssemblyTitle("Log4NetExer1")]

[assembly: AssemblyDescription("")]

[assembly: AssemblyConfiguration("")]

[assembly: AssemblyCompany("")]

[assembly: AssemblyProduct("Log4NetExer1")]

[assembly: AssemblyCopyright("Copyright ©  2008")]

[assembly: AssemblyTrademark("")]

[assembly: AssemblyCulture("")]

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4Net.config", Watch = true)]

 

6. place the following code in the load event of your main Form

private void Form1_Load(object sender, EventArgs e)

        {  

            string log4netconfigfile = Application.StartupPath + @"\log4net.config";

            log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(log4netconfigfile));

        }

7. And thats it you are ready to log log and log

sample logging

        private void button1_Click(object sender, EventArgs e)

        {

            Logger.Info("Succesfullll");

            Logger.Error("asdasdasd",new DivideByZeroException());

        }

Comments

Becoming a pilot said:

I don't argue with you, rather say thank for putting this together. The [assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4Net.config", Watch = true)] would probably be missed by someone like me:)

Now, while I do see the point in using third party dlls for this specific task, and get nifty things like file rolling etc for free,   I just want to add a comment on the .config craziness which is everywhere now.

Speaking for myself, I'd rather write 5 lines of code in my startup routine to do all the inistalization. Why? Cause then it's debuggable. Log4net is one example of .config overusage, the data  application blocks is even worse where they even send you a app to edit the configuration settings.

# May 20, 2008 1:40 AM

Comgen said:

Thanks for sharing your Insights Sir and thank you for pointing out about initialization using Code on the start up routine, but this is just showing one way of doing things , And there's a hundred ways to kill a goat hehehhe.....

# May 23, 2008 11:38 PM

Georg Jansen said:

Becoming a pilot said:

>>I just want to add a comment on the .config

>>craziness which is everywhere now

I agree there is a log of config graziness, however with log4net the point of having the configuration in a config file, and on the same time use XmlConfigureAndWatch is that the config will be reloaded if the config file is change. Typical used in production environments when you for example want to change the log level of a running service.

Georg

http://www.l4ndash.com - log4net dashboard

# September 16, 2008 11:58 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: