Log4net示例用于不同级别的多个appender

时间:2018-04-25 12:07:28

标签: c# log4net rollingfileappender smtpappender

我在[uwsgi] emperor = /etc/uwsgi/vassals(.*) 文件中有多个appender。我想在发生错误时发送电子邮件。 这是我的 log4net.config

Log4net.config

所以,我打电话使用:

 <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
    <file type="log4net.Util.PatternString" value="Logs\${ProcessName}.${InstanceName}.Current.log"/>
    <appendToFile value="true"/>
    <rollingStyle value="Date"/>
    <datePattern value="yyyyMMdd"/>
    <layout type="namespace.CustomPatternLayout, namespace">
        <conversionPattern value="%ProgramId %date [%thread] %-5level %logger [%property{NDC}] - %message%newline"/>
    </layout>
</appender>
<appender name="CustomSmtpAppender" type="namespace.CustomSmtpAppender,namespace">
  <to value="hahm@domain.com"/>
  <from value="hahm@domain.com" />

   <subjectLayout type="log4net.Layout.PatternLayout, log4net">
        <conversionPattern value="Message from Support//%p:%location"/>
    </subjectLayout>
  <smtpHost value="domain" />
  <bufferSize value="1" />
  <authentication value="basic"/>
  <port value="587"/>
  <filter type="log4net.Filter.LevelRangeFilter">
  <levelMin value="ERROR" />
  <levelMax value="OFF" />
  </filter>
  <username value="hahm@domain.com"/>
  <password value=""/>
  <lossy value="true"/>
  <evaluator type="log4net.Core.LevelEvaluator">
      <threshold value="ERROR"/>
   </evaluator>
   <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="Error occur on %date %newline%-5level - %message%newline" />
   </layout>
</appender>
  <logger name="EmailLogger" additivity="false" >
    <level value="ERROR" />
       <appender-ref ref="CustomSmtpAppender" />
  </logger>
  <root>
      <level value="DEBUG" />
    <appender-ref ref="RollingLogFileAppender"/>
    <appender-ref ref="ColoredConsoleAppender"/>
    <appender-ref ref="ConsoleAppender"/>
    <appender-ref ref="FallbackAppender"/>
</root>

示例:

 LogManager.GetLogger("EmailLogger").Error(ex);

我只想在EmailLogger描述时调用'CustomSmtpAppender'

   catch (Exception ex)
        {
            LogManager.GetLogger("EmailLogger").Error(ex);
            //SaveExceptions.SendExceptionToDB(ex); 
            Console.WriteLine(ex.Message);
            Logger.Error(ex);
            throw;
        }

否则我想在catch

中描述下面的时候调用RollingLogFileAppender
LogManager.GetLogger("EmailLogger").Error(ex);

当我运行我的代码时,它总是调用CustomSmtpAppender。

0 个答案:

没有答案