Log4net - Suppress “exception” from being appended

2020-04-02 08:03发布

问题:

When using a custom "PatternLayout", log4net is appending the "exception" information (when present) to every log entry. I am trying to control the output of the message and stack trace information and would like to "suppress" this information. I have searched around but cannot find a way to do it. Any ideas?

Sample web.config entry (for a RollingFileAppender):

<layout type="Example.Class.CustomLog4netLayouts,Example">    
    <conversionPattern value="%date [%thread] [RID:%property{CLIENT_REQUESTID}] 
     %-5level %logger [%property{NDC}] - %cleanmessage - %cleanstack%newline" />
</layout>

Thanks

回答1:

Configure the layout like this:

<layout type="Example.Class.CustomLog4netLayouts,Example">
    <IgnoresException value="False" />
    ...

Setting IgnoresException to false tells the appender that the layout will take care of the exception. Thus you can choose not to print the stack trace.