What do you have in your log4net config? Hacks, optimizations, observations?

This is my log4net config file

<?xml version="1.0" encoding="utf-8" ?>
<log4net debug="true">
  <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >
    <filter type="log4net.Filter.LevelRangeFilter">
      <acceptOnMatch value="true" />
      <levelMin value="DEBUG" />
      <levelMax value="FATAL" />
    </filter>
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%-5p %5rms [%-17.17t] %-22.22c{1} %-18.18M - %m%n" />
    </layout>
  </appender>

  <appender name="SmtpAppender" type="MySmtpClientSmtpAppender">
    <to value="a@a.com" />
    <from value="admin@x.com" />
    <subject value="Game Server Monitoring Failed!" />
    <smtpHost value="smtp.x.com" />
    <username value="admin" />
    <password value="x" />
    <bufferSize value="256" />
    <lossy value="true" />
    <evaluator type="log4net.spi.LevelEvaluator">
      <threshold value="ERROR" />
    </evaluator>
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%-5p %d [ThreadId: %t] Class:%c{1} Method:%M %nMESSAGE:%n%m%n%n" />
    </layout>
  </appender>

  <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
    <file value="Logs\\wwwlog" />
    <appendToFile value="true" />
    <datePattern value="yyyyMMdd" />
    <maximumFileSize value="10MB" />
    <!--<rollingStyle value="Date" />-->
    <filter type="log4net.Filter.LevelRangeFilter">
      <acceptOnMatch value="true" />
      <levelMin value="INFO" />
      <levelMax value="FATAL" />
    </filter>
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline" />
    </layout>
  </appender>


  <root>
    <level value="DEBUG" />
    <appender-ref ref="ConsoleAppender" />
    <appender-ref ref="MySmtpAppender" />
    <appender-ref ref="RollingLogFileAppender" />
  </root>
</log4net>

Am I missing anything? Any tips/ advice to decrease log size while maintaining clarity, better conversion patterns? Any interesting hacks?

Edit: XMl file didnt get into the code block.Updating. Any sugggestions?

Answers


One suggestion for the RollingLogFileAppender is to use the minimal lockingModel. With this setting the log file isn't locked, rather it is opened and then closed for each write.

<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />

Nobody mentioned encoding. It's pretty important if you are logging messages in Chinese, Japanese, Arabic or any double-byte language. Here is an example:

<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
  <encoding value="utf-8" />
  <!-- etc. -->
</appender>

For logging with diferent colors when showing on console depending on the message level (debug, info, error or fatal). The second appender is to save it on a file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
   <configSections>
     <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
   </configSections>

<log4net debug="false">
  <appender name="trace" type="log4net.Appender.TraceAppender, log4net">
    <layout type="log4net.Layout.PatternLayout,log4net">
      <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
    </layout>
  </appender>

 <appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender,log4net">
   <mapping>
    <level value="ERROR" />
    <foreColor value="White" />
    <backColor value="Red" />
  </mapping>
  <mapping>
    <level value="DEBUG" />
    <foreColor value="White" />
  </mapping>
  <mapping>
    <level value="INFO" />
    <foreColor value="Green" />
  </mapping>

  <layout type="log4net.Layout.PatternLayout">
    <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
  </layout>
</appender>

<appender name="RollingFile" type="log4net.Appender.RollingFileAppender,log4net" >
  <param name="File" value="Logs/NSCurrent.log" />
  <param name="AppendToFile" value="true" />
  <param name="RollingStyle" value="Date" />
  <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
  <datePattern value="yyyy-MM-dd" />
  <layout type="log4net.Layout.PatternLayout,log4net">
    <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
  </layout>
</appender>

  <root>
    <priority value="DEBUG" />
    <appender-ref ref="RollingFile" />
    <appender-ref ref="ColoredConsoleAppender" />
  </root>
 </log4net>
 </configuration>

To reduce log file size, you could try adjusting the ConversionPattern:

here's the format I use:

  <layout type="log4net.Layout.PatternLayout,log4net">
    <!-- a reduced format, saves bit of log space: -->
    <param name="ConversionPattern" value="%date{MM/dd HH:mm:ss,fff}-{%level}-%message%newline"/>
  </layout>

BEFORE:

2012-06-05 10:58:40,819 [1] INFO My App [(null)] - My App - ===star ting....

AFTER:

06/05 11:17:29,151-{INFO}-My App - ===starting...

note: the documentation from Apache seems to be incorrect for log4net - as it says to use SSS for milliseconds, whereas log4net uses fff

A small saving per line could make a big difference overall :)


I run a pretty minimal log4net configuration and prefer to use the XML format, but one thing I'd suggest adding (for web apps) is the ASP.NET trace appender. You simply have to add the following:

<appender name="AspNetTraceAppender" type="log4net.Appender.AspNetTraceAppender" >
   <layout type="log4net.Layout.PatternLayout" />
</appender>

Then down at your root logger, or wherever you decide to activate it:

<root>
   <level value="WARN" />
   <appender-ref ref="XmlSchemaFileAppender" />
   <appender-ref ref="AspNetTraceAppender" />
</root>

This allows me to get all of my pertinent debug data together on a single page when I'm debugging some minute ASP.NET details. Most of the time I have the ASP.NET trace appender commented out to disable it, but has really helped out in a few instances, especially when you're trying to figure out what particular server post back method is causing the hangup in your app.


This may help a little, depending on how similar log4j and log4net are, I would think they are based on the same specification, but I don't know for sure.

Well I can't tell you what I have in my log4net config, but I can tell you what I have in my log4j config if that's good enough...

<?xml version="1.0" encoding="UTF-8"?>
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

    <!--
    <appender name="CHAINSAW_APPENDER"
            class="org.apache.log4j.net.SocketAppender">
        <param name="RemoteHost" value="localhost" />
        <param name="Port" value="4445" />
        <param name="LocationInfo" value="true"/>
        <param name="ReconnectionDelay" value="10000"/>
    </appender>
    -->

    <appender name="FILE_APPENDER"
        class="org.apache.log4j.RollingFileAppender">
        <param name="File" value="c:/dev/logs/OSB.log" />
        <param name="Append" value="true" />
         <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d [%t] %-5p [[[[%c]]]] (((%F:%L))) ___%C___ - %m%n"/>
            </layout>
    </appender>

    <logger name="org.apache">
        <level value="WARN"/>
    </logger>

    <!--
    <logger name="com.conciliarpress.webapp.filter.GZIPFilte">
        <level value="WARN"/>
    </logger>

    <logger name="com.conciliarpress">
        <level value="DEBUG"/>
    </logger>

    <logger name="BibleFormControllerTest">
        <level value="DEBUG"/>
    </logger>

    <logger name="com.conciliarpress.webapp.action.sample.jmesa.JMesaSampleController">
        <level value="DEBUG"/>
    </logger>

    <logger name="com.conciliarpress.webapp.action.binding">
        <level value="DEBUG"/>
    </logger>

    <logger name="org.springframework.web.servlet.mvc">
        <level value="DEBUG"/>
    </logger>

    <logger name="org.directwebremoting">
        <level value="DEBUG"/>
    </logger>

    <logger name="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <level value="DEBUG"/>
    </logger>

    -->

    <logger name="com.conciliarpress.dao.hibernate">
        <level value="DEBUG"/>
    </logger>

    <logger name="com.conciliarpress.service.impl">
        <level value="DEBUG"/>
    </logger>

    <logger name="com.conciliarpress.service.dwr">
        <level value="DEBUG"/>
    </logger>

    <logger name="com.conciliarpress.webapp.action">
        <level value="DEBUG" />
    </logger>

    <root>
        <level value="WARN" />
        <appender-ref ref="FILE_APPENDER"/>
    </root>

</log4j:configuration>

Also I have a corresponding chainsaw configuration file:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration PUBLIC "-//log4j/log4j Configuration//EN" "log4j.dtd"> 
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="true">
   <plugin name="logFileReceiver" class="org.apache.log4j.varia.LogFilePatternReceiver">
     <param name="fileURL" value="file:///c:/dev/logs/OSB.log" />
     <param name="timestampFormat" value="yyyy-MM-d HH:mm:ss,SSS"/> 
     <param name="logFormat" value="TIMESTAMP [THREAD] LEVEL [[[[LOGGER]]]] (((FILE:LINE))) ___CLASS___ - MESSAGE"/> 
     <param name="name" value="DLA_M_LogFile" />
     <param name="tailing" value="true" /> 
   </plugin>

   <root>
      <level value="debug"/>
   </root>
</log4j:configuration>

Also here is some information that may help you decifer the latter file: http://wiki.apache.org/logging-log4j/Configuring_a_log4j_RollingFileAppender_with_Chainsaw


Need Your Help

Binding the Loaded event?

c# wpf data-binding mvvm loaded

I am trying to display a login window once my MainWindow loads while sticking to the MVVM pattern. So I am trying to Bind my main windows Loaded event to an event in my viewmodel.