0
votes

I am trying to separate out a Performance Logger into a separate log using Log4Net...

I setup my new Appender

<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
    <file value="C:\Logs\ServiceLayer.log" />
    <appendToFile value="true" />
    <rollingStyle value="Composite" />
    <datePattern value="_yyyy.MM.dd.lo\g" />
    <maximumFileSize value="5MB" />
    <maxSizeRollBackups value="10" />
    <PreserveLogFileNameExtension value="true" />
    <layout type="log4net.Layout.PatternLayout">
        <!-- <conversionPattern value="%level %thread %logger - %message%newline" /> -->
        <conversionPattern value="%d{yyyy-MM-dd HH:mm:ss.fff} %5level [%thread][%property{bms.log.username}::%property{bms.log.businessRequest}(%property{bms.log.activityLogID})]  - %message%newline" />
    </layout>
    <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
</appender>
<appender name="PerfFile" type="log4net.Appender.RollingFileAppender">
    <file value="C:\Logs\Perf.log" />
    <appendToFile value="true" />
    <rollingStyle value="Composite" />
    <datePattern value="_yyyy.MM.dd-HH-mm.lo\g" />
    <maximumFileSize value="5MB" />
    <maxSizeRollBackups value="24" />
    <PreserveLogFileNameExtension value="false" />
    <layout type="log4net.Layout.PatternLayout">
        <!-- <conversionPattern value="%level %thread %logger - %message%newline" /> -->
        <conversionPattern value="%d{yyyy-MM-dd HH:mm:ss.fff} %5level [%thread][%property{bms.log.username}::%property{bms.log.businessRequest}(%property{bms.log.activityLogID})]  - %message%newline" />
    </layout>
    <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
</appender>

The perf appender is the new appender I added...

I also set up a BufferForwardingAppender

<appender name="BMSDefaultAppender" type="log4net.Appender.BufferingForwardingAppender">
<bufferSize value="128" />
<lossy value="false" />
<evaluator type="log4net.Core.LevelEvaluator">
    <threshold value="WARN" />
</evaluator>
<appender-ref ref="RollingFile" />

From there I set my Root

<root>
  <level value="ERROR" />
  <appender-ref ref="BMSDefaultAppender" />
</root>

Once I had this setup, I get ERRORs logged to my rolling log file, however I want to capture some info level data into my perf file...

<logger name="BMS.LTD.Activity.Workflow" additivity="false">
    <level value="INFO" />
    <appender-ref ref="PerfFile" />
</logger>
<logger name="BMS.LTD.Activity.Workflow.Rules" additivity="false">
    <level value="WARN" />
    <appender-ref ref="PerfFile" />
</logger>
<logger name="BMS.LTD.Persistence.DataSourceDataProviders" additivity="false">
    <level value="INFO" />
    <appender-ref ref="PerfFile" />
</logger>
<logger name="BMS.LTD.Persistence.DataSourceDataProviders.DataSourceDataCache" additivity="false">
    <level value="WARN" />
    <appender-ref ref="PerfFile" />
</logger>

I now get my Info and Warn in my Perf log but I also get my errors there too, is it possible to get my ERRORs in my main log and my info levels in my perf logs?

1

1 Answers

1
votes

To control what log events get into each file, Add LevelRangeFilters to your appenders:

<!-- log error and above - add to the RollingFile appender config -->
<filter type="log4net.Filter.LevelRangeFilter">
  <levelMin value="ERROR"/>
  <levelMax value="FATAL"/>
</filter>

and

<!-- log INFO and below - add to the PerfFile appender config -->
<filter type="log4net.Filter.LevelRangeFilter">
  <levelMin value="DEBUG"/>
  <levelMax value="INFO"/>
</filter>