Log4Net - 仅注销某些文件的异常堆栈跟踪
目前,我使用 log4net 在我的应用程序中有多个日志文件。
我有一个顶级日志文件,其中包含每种类型的消息。我还有一个错误日志文件,其中仅包含错误信息。我正在尝试配置它,以便特定的异常详细信息和堆栈跟踪仅出现在错误日志文件中。
我使用的调用是 Log.Error(myMessage, myException);
我的配置如下所示:
<configuration>
<log4net>
<root>
<level value="ALL"/>
<appender-ref ref="GeneralTextLog"/>
<appender-ref ref="ErrorTextLog"/>
</root>
<!-- The general appender rolls by date -->
<appender name="GeneralTextLog" type="log4net.Appender.RollingFileAppender">
<filter type="log4net.Filter.LevelRangeFilter">
<level value="ALL"/>
</filter>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d{HH:mm:ss.fff} [%type] %-5p %message%n"/>
</layout>
<rollingStyle value="Date"/>
<file value="C:/Logs/General_"/>
<datePattern value="yyyy_MM_dd'.log'" />
<appendToFile value="true"/>
<staticLogFileName value="false"/>
</appender>
<!-- The Error appender rolls by date -->
<appender name="ErrorTextLog" type="log4net.Appender.RollingFileAppender">
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="WARN"/>
<levelMax value="FATAL"/>
</filter>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d{HH:mm:ss.fff} [%type] %-5p %message%newline%exception"/>
</layout>
<rollingStyle value="Date"/>
<file value="C:/Logs/Error_"/>
<datePattern value="yyyy_MM_dd'.log'" />
<appendToFile value="true"/>
<staticLogFileName value="false"/>
</appender>
<!-- Loggers -->
<logger name="DefaultLogger">
<appender-ref ref="GeneralTextLog"/>
<level value="ALL"/>
</logger>
<logger name="ErrorLogger">
<appender-ref ref="ErrorTextLog"/>
<levelMin value="WARN"/>
<levelMax value="FATAL"/>
</logger>
尽管我只在错误日志的 conversionPattern 中包含 %Exception,但堆栈跟踪却出现在两个日志中。有谁知道我怎样才能阻止这种情况发生?
I currently have multiple log files in my application using log4net.
I have a top level log file which contains every type of message. I also have an error log file which contains only error information. I am trying to configure it so the specific exception details and stack trace only appear in the error log file.
The call i am using is Log.Error(myMessage, myException);
My config can be seen below:
<configuration>
<log4net>
<root>
<level value="ALL"/>
<appender-ref ref="GeneralTextLog"/>
<appender-ref ref="ErrorTextLog"/>
</root>
<!-- The general appender rolls by date -->
<appender name="GeneralTextLog" type="log4net.Appender.RollingFileAppender">
<filter type="log4net.Filter.LevelRangeFilter">
<level value="ALL"/>
</filter>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d{HH:mm:ss.fff} [%type] %-5p %message%n"/>
</layout>
<rollingStyle value="Date"/>
<file value="C:/Logs/General_"/>
<datePattern value="yyyy_MM_dd'.log'" />
<appendToFile value="true"/>
<staticLogFileName value="false"/>
</appender>
<!-- The Error appender rolls by date -->
<appender name="ErrorTextLog" type="log4net.Appender.RollingFileAppender">
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="WARN"/>
<levelMax value="FATAL"/>
</filter>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d{HH:mm:ss.fff} [%type] %-5p %message%newline%exception"/>
</layout>
<rollingStyle value="Date"/>
<file value="C:/Logs/Error_"/>
<datePattern value="yyyy_MM_dd'.log'" />
<appendToFile value="true"/>
<staticLogFileName value="false"/>
</appender>
<!-- Loggers -->
<logger name="DefaultLogger">
<appender-ref ref="GeneralTextLog"/>
<level value="ALL"/>
</logger>
<logger name="ErrorLogger">
<appender-ref ref="ErrorTextLog"/>
<levelMin value="WARN"/>
<levelMax value="FATAL"/>
</logger>
Despite the fact that i have only included %exception in the conversionPattern for the error log, the stacktrace appears in both logs. Does anyone know how i can stop this from happening?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
像这样配置布局(GeneralTextLog Appender):
将
IgnoresException
设置为 false 告诉附加程序布局将处理异常。因此,您可以选择不打印堆栈跟踪。Configure the layout like this (GeneralTextLog Appender):
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.从您的配置来看,您的堆栈跟踪似乎已包含在
message
参数中。编辑——我应该更清楚。我的猜测是,当您调用 log4net 的日志记录方法时,您的第一个参数(即
message
)包含堆栈跟踪,但这只是一个猜测。它也是两个附加程序中唯一的参数。如果您的附加程序格式中没有参数,则它不应出现在两个日志中...当然,除非您的附加程序是主附加程序,但例外。试试这个,看看会发生什么:
From your configuration, it looks like your stack trace was included as part of your
message
parameter.EDIT -- I should have been clearer. My guess is that when you called log4net's logging methods, your first parameter (which is
message
) contained the stack trace, but that's just a guess. It's also the only parameter in both of your appenders. If you don't have a parameter in your appender format, it shouldn't appear in both logs... unless of course your appender with the exception is the primary appender.Try this instead and see what happens: