Log into Distinct Files [Log4Net]

1

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

But even following the tutorials, they 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>
    
asked by anonymous 01.03.2018 / 17:14

1 answer

1

Set loglevel in appender to 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>
    
23.03.2018 / 21:06