c# - 用于 asp.net core 的 NLog 不过滤 Microsoft 日志

标签 c# nlog asp.net-core-2.1

我在 ASP.NET Core 2.1 项目中有以下 nlog.config 文件。但是,它将来自每个记录器(包括 Microsoft 记录器)的消息记录到控制台。

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      autoReload="true"
      internalLogLevel="Error"
      internalLogFile="${specialfolder:folder=UserProfile}\nlog\internal-nlog.txt">

  <variable name="fullLayout" value="${shortdate}  [${uppercase:${level}}]  ${logger}: ${message}  ${exception:format=tostring}  url: ${aspnet-request-url}" />

  <!-- enable asp.net core layout renderers -->
  <extensions>
    <add assembly="NLog.Web.AspNetCore"/>
  </extensions>

  <!-- the targets to write to -->
  <targets>
    <!-- write to console -->
    <target name="console" xsi:type="ColoredConsole" layout="${fullLayout}" />

    <!-- write to file -->
    <target xsi:type="File"
            name="allfile"
            fileName="${defaultDirectory}/test-api-all.log"
            archiveEvery="Day"
            archiveFileName="${defaultDirectory}/test-api-all-${#}.log"
            archiveNumbering="Date"
            archiveDateFormat="yyyy-MM-dd"
            maxArchiveFiles="5"
            layout="${fullLayout}" />

    <!-- another file log, only own logs. Uses some ASP.NET core renderers -->
    <target xsi:type="File"
            name="ownFile-web"
            fileName="${defaultDirectory}/test-api-app.log"
            archiveEvery="Day"
            archiveFileName="${defaultDirectory}/test-api-app-${#}.log"
            archiveNumbering="Date"
            archiveDateFormat="yyyy-MM-dd"
            maxArchiveFiles="5"
            layout="${fullLayout}" />
  </targets>

  <!-- rules to map from logger name to target -->
  <rules>
    <!--All logs, including from Microsoft-->
    <logger name="*" minlevel="Trace" writeTo="console" />

    <!--Skip non-critical Microsoft logs and so log only own logs-->
    <logger name="Microsoft.*" maxLevel="Info" final="true" />
    <logger name="*" minlevel="Trace" writeTo="ownFile-web" />
  </rules>
</nlog>

我在 appsettings.json 文件中没有任何日志记录设置。 nlog.config 中的所有记录器配置。在 Program.cs 中,我像这样注册 NLog:

public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .ConfigureLogging(logging => logging.ClearProviders())
                .UseNLog()
                .Build();

如何过滤掉 Microsoft 日志?

编辑:

上述配置第二天就开始正常工作了,我没有做任何更改:O

最佳答案

也许这会起作用:

  <!-- rules to map from logger name to target -->
  <rules>
    <!--Skip non-critical Microsoft logs and so log only own logs-->
    <logger name="Microsoft.*" maxLevel="Info" final="true" />

    <logger name="*" minlevel="Trace" writeTo="console" />

    <logger name="*" minlevel="Trace" writeTo="ownFile-web" />
  </rules>

日志记录规则的顺序很重要,因为规则是从顶部开始匹配的。另见 https://github.com/NLog/NLog/wiki/Configuration-file#rules

关于c# - 用于 asp.net core 的 NLog 不过滤 Microsoft 日志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53996340/

相关文章:

c# - 找到多个名为 'NewProject.Models.DbContext' 的 DbContext 通过使用准确大小写的完全限定名称指定要使用的一个

c# - 使用 Wix# 构建 MSI 的递归 DirFiles

logging - 如何打开和关闭 NLog 中特定级别的日志记录

c# - 如何将 Windows.UI.Composition 动画集成到 UWP MVVM Light 应用程序中

c# - 动态设置文件名时,未使用 NLog 创建日志文件

c# - 确保 NLOG 确实进行日志记录?

c# - ASP.NET Core 2.1 路由 - CreatedAtRoute - 没有路由与提供的值匹配

c# - 如何在 net core 2.1+/net 5 中禁用预编译 View 进行调试?

c# - 自定义内容类型中的 MediaLibraryPickerField 不显示

c# - 如何在C#中通过ID查找元素