Humprey Evangelista Cogay

My Programming Adventures

April 2008 - Posts

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());

        }

Help =)

Hello IT friends hehehe Help naman po, Post naman a comment or critique about my Shots... Just want to know if there's improvement. Here's my Blog

Http://HumpreyCogay.BlogSpot.Com

Thank you....

SQL versions

Its been months since i have'nt posted anything haha. I was addicted to photography indeed. Http://HumpreyCogay.BlogSpot.Com

Any way i want to share one of my problems then. I've been coding SQL Querries for SQL Server for several years now, but working for the bank we use AS400 DB2 as a main DBMS. The problem here is there are several differences between the 2. SQL Server and DB2.

 Luckily i hava a book titled SQL Cook Book published by O'Reilly.

Here's a sample problem that i have(from the book)

   update e
      set e.sal  = ns.sal,
          e.comm = ns.sal/2
     from emp e,
          new_sal ns
    where ns.deptno = e.deptno

And you cannot use it in DB2, Here's the DB2 Version instead

 update emp e set (e.sal,e.comm) = (select ns.sal, ns.sal/2
                                      from new_sal ns
                                     where ns.deptno=e.deptno)
  where exists ( select null
                   from new_sal ns
                  where ns.deptno = e.deptno )

Now I'm back in the community... Hehehhe

Posted: 04-23-2008 2:44 PM by Comgen | with 1 comment(s)
Filed under: ,