I have configured a SMTP appender to send an email whenever there's an error.
<appender name="EmailAppender">
<bufferSize value="50" />
<lossy value="false" />
<threshold value="ALL" />
<evaluator type="log4net.Core.LevelEvaluator,log4net">
<threshold value="ERROR" />
</evaluator>
</appende开发者_开发百科r>
I need to also get an email when an operation (which is not an error) is completed. I can in theory log this as an error and get an email but in reality it's not an error but an indication that an operation has been completed (More like an INFO).
Should I add another SMTP appender?
You should be able to change <threshold value="ERROR"/>
to <threshold value="INFO"/>
. This appender will then deliver emails for INFO, WARN, ERROR and FATAL.
If you need a custom level, you can use something like:
<level>
<name value="CUSTOM"/>
<value value="35000"/>
</level>
See this question for more information on how to filter with custom levels. Apache provides more information on levels here.
In one of my projects I use namespaces to control logging. Here's sample configuration:
<log4net>
<appender name="LogFileAppender" type="log4net.Appender.FileAppender,log4net" >
<param name="File" value="c:\\tmp\\remoteServerLog.txt" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="5" />
<maximumFileSize value="3MB" />
<param name="AppendToFile" value="false" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d %-5p %c - %m%n" />
</layout>
</appender>
<root>
<priority value="ALL" />
</root>
<logger name="NHibernate.SQL">
<level value="WARN" />
<appender-ref ref="LogFileAppender" />
</logger>
<logger name="NHibernate.Cfg">
<level value="WARN" />
<appender-ref ref="LogFileAppender" />
</logger>
<logger name="NHibernate">
<level value="WARN" />
<appender-ref ref="LogFileAppender" />
</logger>
<logger name="MyNameSpace.MyBusinessCore">
<level value="ALL" />
<appender-ref ref="LogFileAppender" />
</logger>
Depending on the namespace of the source class, you can adjust the logging level. Thus in the sample above I log only WARNings from NHibernate and everything from MyNameSpace.MyBusinessCore (including DEBUG and TRACE).
To sum up, in your example I would only add the following:
<root>
<priority value="INFO" />
</root>
<logger name="Your.Namespace ">
<level value="INFO" />
<appender-ref ref="EmailAppender" />
</logger>
Than Your.Namespace will also send INFO level logs.
精彩评论