如何解决我无法让 log4net 过滤工作 - 似乎忽略了级别和过滤器设置
在“正常工作”几年后,我目前正在重新熟悉 log4Net 配置,并且在使用 ASP.NET Core 的 Visual Studio 2019 中工作时遇到了一个问题。任何帮助解决这个问题将不胜感激。
我有以下过滤器和级别设置,但我的日志文件仍然以 IDE 调试窗口中的所有“Cahtter”结束。如您所见,我已将级别设置为“INFO”及以上,并尝试过滤掉包含“Microsoft.AspNetCore”的“噪音”,但没有成功。我研究了如何进行过滤并认为我做得对,但它并没有过滤任何东西。
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<appender name="DebugAppender" type="log4net.Appender.DebugAppender" >
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
<file value="SDPerf.log" />
<appendToFile value="true" />
<maximumFileSize value="100KB" />
<maxSizeRollBackups value="2" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %5level %logger.%method [%line] - MESSAGE: %message%newline %exception" />
</layout>
<filter type="log4net.Filter.StringMatchFilter">
<stringToMatch value="Microsoft.AspNetCore"/>
<acceptOnMatch value="false"/>
</filter>
</appender>
<root>
<level value="INFO"/>
<appender-ref ref="DebugAppender" />
<appender-ref ref="RollingFile" />
</root>
</log4net>
这是日志文件中的一个片段:
2021-05-21 15:50:01,744 INFO Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.? [?] - MESSAGE: Executing action method LongRunningTask.Controllers.HomeController.Test2 (LongRunningTask) - Validation state: Valid
2021-05-21 15:50:01,754 INFO Microsoft.AspNetCore.Mvc.ViewFeatures.PartialViewResultExecutor.? [?] - MESSAGE: Executing PartialViewResult,running view Jobs.
2021-05-21 15:50:01,762 INFO Microsoft.AspNetCore.Mvc.ViewFeatures.PartialViewResultExecutor.? [?] - MESSAGE: Executed PartialViewResult - view Jobs executed in 7.4295ms.
2021-05-21 15:50:01,768 INFO Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.? [?] - MESSAGE: Executed action LongRunningTask.Controllers.HomeController.Test2 (LongRunningTask) in 43.365ms
2021-05-21 15:50:01,772 INFO Microsoft.AspNetCore.Routing.EndpointMiddleware.? [?] - MESSAGE: Executed endpoint 'LongRunningTask.Controllers.HomeController.Test2 (LongRunningTask)'
2021-05-21 15:50:01,776 INFO Microsoft.AspNetCore.Hosting.Diagnostics.? [?] - MESSAGE: Request finished in 71.9539ms 200 text/html; charset=utf-8
2021-05-21 15:50:03,561 INFO Microsoft.AspNetCore.Hosting.Diagnostics.? [?] - MESSAGE: Request starting HTTP/2.0 GET https://localhost:44345/Home/ProcessJob
2021-05-21 15:50:03,567 INFO Microsoft.AspNetCore.Routing.EndpointMiddleware.? [?] - MESSAGE: Executing endpoint 'LongRunningTask.Controllers.HomeController.ProcessJob (LongRunningTask)'
2021-05-21 15:50:03,588 INFO Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.? [?] - MESSAGE: Executing action method LongRunningTask.Controllers.HomeController.ProcessJob (LongRunningTask) - Validation state: Valid
2021-05-21 15:50:03,592 INFO SDPerf..ctor [24] - MESSAGE: Starting Task calls
2021-05-21 15:50:03,602 INFO SDPerf.NewMethod [51] - MESSAGE: Starting iterations for method '2'
2021-05-21 15:50:03,606 INFO SDPerf.NewMethod [51] - MESSAGE: Starting iterations for method '3'
2021-05-21 15:50:03,612 INFO SDPerf.NewMethod [51] - MESSAGE: Starting iterations for method '1'
2021-05-21 15:50:03,619 INFO Microsoft.AspNetCore.Mvc.RedirectToActionResult.? [?] - MESSAGE: Executing RedirectResult,redirecting to /Home/ShowJobProgress?requestId=7b1a8cf8-641f-4599-a90f-748707f4ed63.
2021-05-21 15:50:03,621 INFO Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.? [?] - MESSAGE: Executed action LongRunningTask.Controllers.HomeController.ProcessJob (LongRunningTask) in 44.8757ms
2021-05-21 15:50:03,624 INFO Microsoft.AspNetCore.Routing.EndpointMiddleware.? [?] - MESSAGE: Executed endpoint 'LongRunningTask.Controllers.HomeController.ProcessJob (LongRunningTask)'
2021-05-21 15:50:03,627 INFO Microsoft.AspNetCore.Hosting.Diagnostics.? [
解决方法
最终通过反复试验解决了这个问题。虽然我找不到任何示例来帮助我弄清楚,但我发现“StringMatchFilter”方法仅适用于消息的原始文本,而不是正在记录的整行。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。