Log in Separate Files [Log4net]

Asked

Viewed 401 times

1

I use Log4net in my application. I need to create a configuration that "deposit" everything from level="INFO" in a file and everything that is light="ERROR" in another file.

But even following the tutorials, do not work. Look how I did below. Thanks in advance...

<log4net>
  <root name="LogFilesAll">
    <level value="ERROR" />
    <appender-ref ref="RollingFileErro"/>
    <level value="INFO" />
    <appender-ref ref="RollingFileInfo"/>
    <level value="DEBUG" />
    <appender-ref ref="Console"/>
  </root>

  <!-- APPENDER DE ERRO -->
  <appender name="RollingFileErro" type="log4net.Appender.RollingFileAppender">
    <file value="GestorReq32Error.log" />
    <appendToFile value="true" />
    <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%-5p %date{dd/MM/yyyy HH:mm:ss} %message%newline" />
    </layout>
    <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
    <appendToFile value="true"/>
    <maximumFileSize value="15MB"/>
    <staticLogFileName value="true"/>
    <rollingStyle value="Size"/>
    <maxSizeRollBackups value="10"/>    
  </appender>

  <!-- APPENDER DE ERRO -->
  <appender name="RollingFileInfo" type="log4net.Appender.RollingFileAppender">
    <file value="GestorReq32Info.log" />
    <appendToFile value="true" />
    <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%-5p %date{dd/MM/yyyy HH:mm:ss} %message%newline" />
    </layout>
    <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
    <appendToFile value="true"/>
    <maximumFileSize value="15MB"/>
    <staticLogFileName value="true"/>
    <rollingStyle value="Size"/>
    <maxSizeRollBackups value="10"/>
  </appender>

  <appender name="Console" type="log4net.Appender.ConsoleAppender">
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%5level [%thread] (%file:%line) - %message%newline" />
    </layout>
  </appender>

</log4net>
  • The archive log4net.config is along the .exe of its application?

  • Yes, it is in the same directory and I can generate the log, but only one file.

1 answer

1


Define the loglevel within appender for filter. More details can be found here

<filter type="log4net.Filter.LevelRangeFilter">
       <levelMin value="DEBUG"/>
       <levelMax value="DEBUG" />      
</filter>

For example:

<log4net>
  <root name="LogFilesAll">
    <!-- Aqui deixe "ALL" para processar todos os tipos de logs -->
    <level value="ALL" />
    <appender-ref ref="RollingFileErro"/>
    <appender-ref ref="RollingFileInfo"/>
    <appender-ref ref="Console"/>
  </root>

  <!-- APPENDER DE ERRO -->
  <appender name="RollingFileErro" type="log4net.Appender.RollingFileAppender">
    <file value="GestorReq32Error.log" />
    <appendToFile value="true" />
    <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%-5p %date{dd/MM/yyyy HH:mm:ss} %message%newline" />
    </layout>
    <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
    <appendToFile value="true"/>
    <maximumFileSize value="15MB"/>
    <staticLogFileName value="true"/>
    <rollingStyle value="Size"/>
    <maxSizeRollBackups value="10"/>
    <!-- Define o filtro -->
    <filter type="log4net.Filter.LevelRangeFilter"> 
           <levelMin value="ERROR"/>
           <levelMax value="ERROR" />      
    </filter>    
  </appender>

  <!-- APPENDER DE INFO -->
  <appender name="RollingFileInfo" type="log4net.Appender.RollingFileAppender">
    <file value="GestorReq32Info.log" />
    <appendToFile value="true" />
    <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%-5p %date{dd/MM/yyyy HH:mm:ss} %message%newline" />
    </layout>
    <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
    <appendToFile value="true"/>
    <maximumFileSize value="15MB"/>
    <staticLogFileName value="true"/>
    <rollingStyle value="Size"/>
    <maxSizeRollBackups value="10"/>
    <!-- Define o filtro -->
    <filter type="log4net.Filter.LevelRangeFilter">
           <levelMin value="DEBUG"/>
           <levelMax value="INFO" />
    </filter>          
  </appender>

  <appender name="Console" type="log4net.Appender.ConsoleAppender">
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%5level [%thread] (%file:%line) - %message%newline" />
    </layout>
  </appender>

</log4net>
  • It worked perfectly! Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.