views:

1871

answers:

4

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="[email protected]" />
    <from value="[email protected]" />
    <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?

+1  A: 

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/"&gt;

    <!--
    <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

leeand00
+3  A: 

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>
despart
+2  A: 

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 lever, 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.

Dillie-O
+6  A: 

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" />
Dscoduc
Good call. I use that too and it seems to help with my own log viewer so that it can browse the file as well.
Dillie-O