Thursday, March 22, 2012

Part 2 : How to use log4net in .net


In Part 1 : How to use log4net in .net you have learnt the basic of log4net. Here I am going to tell you some more details about it.

Create Date based log file

Yes, you can also create your log files date vise. If you want to create a separate file for every day then you can also do that in the log4net. You need to set the <staticLogFileName value="false">
 in the appender.
 The appender section will be

  <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="d:\"/>
      <staticLogFileName value="false"/>
      <appendToFile value="true" />
      <rollingStyle value="Date" />
      <datePattern value="yyyy-MM-dd'.log'" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
  </appender>

Here the value of the file section specify the path of the log file. I have specify the "D:\ "then the file will be created in the "D" drive. If you specify the "d:\log_" then the log file will be "log_2012-03-22.log".


Create separate log file for specified log level

Here I would like to let you know how you can log the information according to their log level (Debug, Info, Warning, Error, Fatal) in different files. For example, if you want to log Debug log into a file and Error log to another file, you can achieve this through the log4net. For that you just have to add the two appender according to their log level. Each appender will have different log file.

<appender name="ErrorLogFileAppender" type="log4net.Appender.FileAppender">
<param name="File"value="D:\ErrorLogFile.txt"/>
<param name="AppendToFile" value="true"/>
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c %m%n"/>
</layout>
<filter type="log4net.Filter.LevelMatchFilter">
<levelToMatch value="ERROR"/>
</filter
<filter type="log4net.Filter.DenyAllFilter" />
</appender>
    <appender name="DebugLogFileAppender" type="log4net.Appender.FileAppender">
<param name="File" value="D:\DebugLogFile.txt"/>
<param name="AppendToFile" value="true"/>
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c %m%n"/>
</layout>
<filter type="log4net.Filter.LevelMatchFilter">
<levelToMatch value="DEBUG"/>
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
</appender>
<root>
<level value="ALL"/>
<appender-ref ref="DebugLogFileAppender" />
<appender-ref ref="ErrorLogFileAppender" />
</root>
 
Here I have created two appender ErrorLogFileAppender and DebugLogFileAppender. ErrorLogFileAppender logs the Error level log and store the log information into the ErrorLogFile.txt and DebugLogFileAppender logs the Debug log and store the log information into the DebugLogFile.txt.


Appenders

We can log the information into differenct destination output through the Appender.  In Previous examples we have seen the FileAppender which is used to log the information into a file. However, log4net provides numbers of Appenders to log the information, you can customized as per your need.

FileAppender
                See the previous examples.


SmtpAppender
 SmtpAppender is used when you want to get the specified event log through the email. The following example will show how to use the SmtpAppender.

    <appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
      <to value="to@domain.com" />
      <from value="from@domain.com" />
      <subject value="my logging message" />
      <smtpHost value="SMTPServer.domain.com" />
      <bufferSize value="512" />
      <lossy value="false" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%newline%date [%thread] %-5level %logger [%property{NDC}] - %message%newline%newline%newline" />
      </layout>
    </appender>

 Here to specifies the address where log event should be send. The from is the address from which account email should be send. The subject specifies the subject of the email. You can also customise this subject. The bufferSize specifies the size of the email. As soon as log event information reached that size, it sends the email.

<subject type="log4net.Util.PatternString" value="%property{log4net:HostName} Error: %appdomain" />

Here I have customised the subject. The HostName and appdomain represents the your host name and application domain of your application.

In the above appender all the log events will be sent to the specified email address, however you can also mentioned which log event you want in email. For that you have to use the following configuration.

<appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">     
<to value="to@domain.com" />     
<from value="from@domain.com" />     
<subject value="my logging message" />     
<smtpHost value="SMTPServer.domain.com" />     
<bufferSize value="512"/>     
<lossy value="true" />     
<evaluator type="log4net.Core.LevelEvaluator">       
<threshold value="ERROR"/>     
</evaluator>      
<layout type="log4net.Layout.PatternLayout">       
<conversionPattern value="%newline%date [%thread] %-5level %logger [%property{NDC}] - %message%newline%newline%newline" />     
</layout>
</appender>
 
Here you will get only Errro log event through the email. For that you have to set the lossy to "true" and evaluator to "ERROR".

ConsoleAppender
 Basically ConsoleAppender is used to show the log event information into the console output. The following configuration is used for the ConsoleAppender.

 <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender"/&agt;     
<layout type="log4net.Layout.PatternLayout"/&gt       
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />     
</layout>    </appender>