0

经过几年的“正常工作”后,我目前正在重新熟悉 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.? [
4

1 回答 1

0

最后通过反复试验解决了这个问题。虽然我找不到任何示例来帮助我弄清楚,但我发现“StringMatchFilter”方法仅适用于消息的原始文本,而不是记录的整行。

于 2021-05-25T16:58:56.053 回答