您可以创建两个单独的附加程序并LevelRangeFilter
为每个附加程序设置一个,如下所示:
<appender name="WarnSmtpAppender" type="log4net.Appender.SmtpAppender">
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="WARN" />
<levelMax value="WARN" />
</filter>
<to value="to@domain.com" />
<from value="from@domain.com" />
<subject value="Warning logging message" />
<smtpHost value="SMTPServer.domain.com" />
<bufferSize value="512" />
<lossy value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%newline%date [%thread] %-5level %logger [%property{NDC}] - %message%newline%newline%newline" />
</layout>
</appender>
<appender name="ErrSmtpAppender" type="log4net.Appender.SmtpAppender">
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="ERROR" />
<levelMax value="ERROR" />
</filter>
<to value="to@domain.com" />
<from value="from@domain.com" />
<subject value="Error logging message" />
<smtpHost value="SMTPServer.domain.com" />
<bufferSize value="512" />
<lossy value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%newline%date [%thread] %-5level %logger [%property{NDC}] - %message%newline%newline%newline" />
</layout>
</appender>
然后,您使用两个附加程序设置您的记录器:
<root>
<!-- Log4Net available levels:
ALL DEBUG INFO WARN ERROR FATAL OFF-->
<level value="WARN" />
<appender-ref ref="WarnSmtpAppender" />
<appender-ref ref="ErrSmtpAppender" />
</root>
请注意,LOGGER 级别必须设置为 WARN 或更低。如果设置为 ERROR,记录器将不会发出警告,因此警告附加程序将不会获取它们。
这应该可以解决问题。