Rohit Gupta

Engaging talk on Microsoft Technologies ....My Resume

  Home  |   Contact  |   Syndication    |   Login
  39 Posts | 0 Stories | 52 Comments | 0 Trackbacks

News



Twitter












Archives

Image Galleries

Personal

Tuesday, March 03, 2009 #

If you needed to generate dynamic logfile names with log4net then you can use the following config

   1: <appender name="RollingFileAppenderV1" type="log4net.Appender.RollingFileAppender">
   2:     <file type="log4net.Util.PatternString" value="F:\HornetFeed\%property{LogName}" />
   3:     <appendToFile value="true" />
   4:     <rollingStyle value="Size" />
   5:     <maxSizeRollBackups value="-1" />
   6:     <maximumFileSize value="5000KB" />
   7:     <staticLogFileName value="true" />
   8:     <countDirection value="1"/>
   9:     <layout type="log4net.Layout.PatternLayout">
  10:         <conversionPattern value="%m%n" />
  11:     </layout>
  12:     <filter type="log4net.Filter.PropertyFilter">
  13:         <Key value="Version" />
  14:         <StringToMatch value="1" />
  15:     </filter>
  16:     <filter type="log4net.Filter.DenyAllFilter" />
  17: </appender>

Note the %property{LogName} this is a log4net Property which we can set at runtime using C# code.

   1: log4net.GlobalContext.Properties["LogName"] = "file1.log";

Remember to set the GlobalContext Properties before instantiating the log4net logger. i.e. before this call:

   1: log4net.ILog log = LogManager.GetLogger(typeof(Program));

Also note the “PropertyFilter”

   1: <filter type="log4net.Filter.PropertyFilter">
   2:     <Key value="Version" />
   3:     <StringToMatch value="1" />
   4: </filter>

This Property is also set dynamically at runtime using C# code. We can use ThreadContext to determine which logfile the log entry will be written to. Thus if you wanted that the RollingFileAppenderV1 was used for writing insure that the Thread calling the log.Warn method precedes the call with this following line

   1: log4net.ThreadContext.Properties["Version"] = "1";

If you wanted to use RollingFileAppenderV1 always for logging all messages other than Exception messages then use the following

   1: try
   2: {
   3:     log4net.GlobalContext.Properties["Version"] = "1";
   4:     //Business logic with many log.Warn calls
   5: }
   6: catch (Exception ex)
   7: {
   8:     LogError(ex);
   9: }
  10:  
  11: ///Helper method to log errors:
  12: internal static void LogError(Exception ex)
  13: {
  14:     string state = "1";
  15:     if(log4net.ThreadContext.Properties["Version"] != null)
  16:         state = log4net.ThreadContext.Properties["Version"].ToString();
  17:     log4net.ThreadContext.Properties["Version"] = "0";
  18:     logger.HandleException(ex, "Error");
  19:     log4net.ThreadContext.Properties["Version"] = state;
  20: }

Note I am using GlobalContext and hence all threads will use this setting to write to log files. No need to set the TrheadContext.Properties on individual threads.

But I prefer to use ThreadContext so that we can control the setting on each thread level.

Also make sure the log4net XML config is set as follows:

   1: <log4net>
   2: // the following logs only those logs when the Property Version is set to 1 i.e.
   3: // if Version =1  then log.Warn calls are logged to this dynamically named file
   4:     <appender name="RollingFileAppenderV1" type="log4net.Appender.RollingFileAppender">
   5:         <file type="log4net.Util.PatternString" value="F:\HornetFeed\%property{LogName}" />
   6:         <appendToFile value="true" />
   7:         <rollingStyle value="Size" />
   8:         <maxSizeRollBackups value="-1" />
   9:         <maximumFileSize value="5000KB" />
  10:         <staticLogFileName value="true" />
  11:         <countDirection value="1"/>
  12:         <layout type="log4net.Layout.PatternLayout">
  13:             <conversionPattern value="%m%n" />
  14:         </layout>
  15:         <filter type="log4net.Filter.PropertyFilter">
  16:             <Key value="Version" />
  17:             <StringToMatch value="1" />
  18:         </filter>
  19:         <filter type="log4net.Filter.DenyAllFilter" />
  20:     </appender>
  21:  
  22: //The following matches and logs all events except Version=1 and Version=2 and strings containing CACHE_CALL_LOG
  23:  
  24:     <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
  25:         <file type="log4net.Util.PatternString" value="F:\logfiles\trace.log" />
  26:         <appendToFile value="true" />
  27:         <rollingStyle value="Size" />
  28:         <maxSizeRollBackups value="10" />
  29:         <maximumFileSize value="3000KB" />
  30:         <staticLogFileName value="true" />
  31:         <countDirection value="1"/>
  32:         <layout type="log4net.Layout.PatternLayout">
  33:             <conversionPattern value="%d [%t] %-5p %c [%x] - %m%n" />
  34:         </layout>
  35:         <filter type="log4net.Filter.PropertyFilter">
  36:             <Key value="Version" />
  37:             <StringToMatch value="1" />
  38:             <acceptOnMatch value="false" />
  39:         </filter>
  40:         <filter type="log4net.Filter.PropertyFilter">
  41:             <Key value="Version" />
  42:             <StringToMatch value="2" />
  43:             <acceptOnMatch value="false" />
  44:         </filter>
  45:         <filter type="log4net.Filter.StringMatchFilter">
  46:             <stringToMatch value="CACHE_CALL_LOG" />
  47:             <acceptOnMatch value="false" />
  48:         </filter>
  49:     </appender>
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

By Default we have the following values for following properties of RollingFileAppender

  • staticLogFileName = true
  • countDirection = –1
  • rollingStyle = Composite
  • maxSizeRollBackups = 0 // be careful with this
  • maximumFileSize = “10MB”
  • datePattern = ".yyyy-MM-dd"

staticLogFileName indicates whether you need to keep writing (log) to the same file all the time. You will need to set it to false when using Date as the rolling style and you have large number of backups.

Optionally file.log.yyyy-mm-dd for current formated datePattern can by the currently logging file (or file.log.curSizeRollBackup (rollingStyle=Size) or even file.log.yyyy-mm-dd.curSizeRollBackup --- (rollingStyle=Composite)) This will make time based roll overs with a large number of backups much faster -- it won't have to rename all the backups!

Recommend to leave it at its default value “true”

countDirection when its value is –1, then newest logfile backup will always be file.log.1.. hence this would involve more number of file renaming.

By default newer files have lower numbers. (countDirection < 0) ie. log.1 is most recent, log.5 is the 5th backup, etc... countDirection > 0 does the opposite ie. log.1 is the first backup made, log.5 is the 5th backup made, etc. For infinite backups use countDirection > 0 to reduce rollOver costs.

rollingStyle can be either Date, Size or Composite. the default setting Composite, uses a combination of Size and Date settings. Thus if you have the datePattern set to “.yyyy-MM-dd” and maxSizeRollBackups set to 10, themn it will maintain 10 log backups for each day.

If you have the DatePattern set to “.yyyy-MM-dd HH:mm” and maxSizeRollbackups = 10 then it will maintain 10 logfile backups per minute

Samples:

   1: <appender name="RollingFileAppenderV1" type="log4net.Appender.RollingFileAppender">
   2:     <file type="log4net.Util.PatternString" value="F:\HornetFeed\%property{LogName}" />
   3:     <appendToFile value="true" />
   4:     <rollingStyle value="Size" />
   5:     <maxSizeRollBackups value="-1" />
   6:     <maximumFileSize value="5000KB" />
   7:     <staticLogFileName value="true" />
   8:     <countDirection value="1"/>
   9:     <layout type="log4net.Layout.PatternLayout">
  10:         <conversionPattern value="%m%n" />
  11:     </layout>
  12:     <filter type="log4net.Filter.PropertyFilter">
  13:         <Key value="Version" />
  14:         <StringToMatch value="1" />
  15:     </filter>
  16:     <filter type="log4net.Filter.DenyAllFilter" />
  17: </appender>

This will create infinite file backups with the countdirection > 0 so that the newest file has the latest/greatest name i.e. log.5 for the newest backup (5th backup)

   1: <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
   2:         <file value="logfile" />
   3:         <appendToFile value="true" />
   4:         <rollingStyle value="Composite" />
   5:         <datePattern value=".yyyyMMdd-HHmm" />
   6:         <maxSizeRollBackups value="10" />
   7:         <maximumFileSize value="1MB" />
   8:         <countDirection value="1"/>
   9:         <layout type="log4net.Layout.PatternLayout">
  10:             <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
  11:         </layout>
  12:     </appender>

This is a Composite RollingFileAppender which keeps max of 10 1MB log backups every minute

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati