Rohit Gupta

Engaging talk on Microsoft Technologies ....My Resume

  Home  |   Contact  |   Syndication    |   Login
  43 Posts | 0 Stories | 42 Comments | 0 Trackbacks

News



Twitter












Archives

Image Galleries

Personal

If you need to use one log4net file name for Messages having Level between WARN and ERROR and a different filename for Messages having Level between INFO AND DEBUG, then use the following log4net config:

   1: <log4net>
   2:     <appender name="RollingFileAppenderInfo" type="log4net.Appender.RollingFileAppender">
   3:       <file type="log4net.Util.PatternString" value="Results\%property{LogName}" />
   4:       <appendToFile value="true" />
   5:       <rollingStyle value="Size" />
   6:       <maxSizeRollBackups value="-1" />
   7:       <maximumFileSize value="10240KB" />
   8:       <staticLogFileName value="true" />
   9:       <countDirection value="1"/>
  10:       <threshold value="DEBUG"/>
  11:       <layout type="log4net.Layout.PatternLayout">
  12:         <conversionPattern value="%m%n" />
  13:       </layout>
  14:       <filter type="log4net.Filter.LevelRangeFilter">
  15:         <levelMin value="DEBUG" />
  16:         <levelMax value="INFO" />
  17:       </filter>
  18:       <filter class="log4net.Filter.DenyAllFilter"/>
  19:     </appender>
  20:  
  21:     <appender name="RollingFileAppenderError" type="log4net.Appender.RollingFileAppender">
  22:       <file type="log4net.Util.PatternString" value="%property{LogName}" />
  23:       <appendToFile value="true" />
  24:       <rollingStyle value="Size" />
  25:       <maxSizeRollBackups value="10" />
  26:       <maximumFileSize value="10240KB" />
  27:       <staticLogFileName value="true" />
  28:       <countDirection value="1"/>
  29:       <threshold value="WARN"/>
  30:       <layout type="log4net.Layout.PatternLayout">
  31:         <conversionPattern value="%d [%t] %-5p %c [%x] - %m%n" />
  32:       </layout>
  33:       <filter type="log4net.Filter.LevelRangeFilter">
  34:         <levelMin value="WARN" />
  35:         <levelMax value="ERROR" />
  36:       </filter>
  37:     </appender>
  38:  
  39:     <root>
  40:       <level value="DEBUG" />
  41:       <appender-ref ref="RollingFileAppenderInfo" />
  42:       <appender-ref ref="RollingFileAppenderError" />
  43:     </root>
  44:   </log4net>

Note here that I am using Dynamic logfile names so you need to call the following in your code to set up the Log4net File Names:

   1: GlobalContext.Properties["LogName"] = Log4netSetup.GetLog4NetLogFileName();
   2: XmlConfigurator.Configure(new FileInfo("CreateAssetCashFlows.exe.config"));

The Level’s in log4net are Off, FATAL, ERROR, WARN, INFO, DEBUG, ALL where OFF is the Highest Level and ALL is the Lowest Level.

Thus note that the following config is important :

   1: <threshold value="WARN"/>
   2: <filter type="log4net.Filter.LevelRangeFilter">
   3:   <levelMin value="WARN" />
   4:   <levelMax value="ERROR" />
   5: </filter>

The threshold parameter specifies that all Messages BELOW the Level WARN Will be ignored by this FileAppender. ( i.e. INFO, DEBUG & ALL Messages will be ignored)
The LevelRangeFilter states that Messages Between WARN & Error ONLY will be logged by this Appender.

posted on Wednesday, June 23, 2010 5:58 AM