logging in log4net to different appenders based on circumstances

后端 未结 1 1912
难免孤独
难免孤独 2020-12-16 21:01

I am using log4net and in one class require logging to a RollingFile appender, but then in another class, I wish to log to the event log + rolling file + console appender.

相关标签:
1条回答
  • 2020-12-16 21:20

    You can do this by applying a filter to an appender. Only if the log event passes the filter does the event get logged by that appender.

    This filter configuration will log only those events coming from the logger named "MyLogger":

    <appender name="EventLogAppender" ...
        <filter type="log4net.Filter.LoggerMatchFilter">
            <loggerToMatch value="MyLogger" />
        </filter>       
        <filter type="log4net.Filter.DenyAllFilter" />
    </appender>
    

    ...and this one will match log messages with certain contained text:

    <filter type="log4net.Filter.StringMatchFilter">
        <stringToMatch value="database" />
    </filter>
    <filter type="log4net.Filter.DenyAllFilter" />
    

    There's a good bit of configuration possible with filters. See the log4net SDK, or the Filters section of the manual, for more details.

    0 讨论(0)
提交回复
热议问题