我正在尝试禁用 ASP.NET Core 应用程序的默认日志记录。我将 NLog 与
nlog.config
文件一起使用,并且尝试将 Logging
部分添加到 appsettings.json
文件中,但我仍然收到日志。
这就是我的
appsettings.json
:
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "None",
"Microsoft.Hosting.Lifetime": "None",
"Microsoft.AspNetCore.Hosting.Diagnostics": "None"
},
"EventLog": {
"LogLevel": {
"Microsoft.AspNetCore.Hosting.Diagnostics": "None"
}
}
}
这是我在
Program.cs
中的初始配置:
var logger = LogManager.Setup().LoadConfigurationFromAppSettings().GetCurrentClassLogger();
var builder = WebApplication.CreateBuilder(args);
builder.Logging.ClearProviders();
builder.Host.UseNLog();
在我的
nlog.config
中我有这个,但我认为问题不在这里:
<?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"
throwExceptions="false"
internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log">
<targets>
<!-- some targets here that I remove to make the code shorter-->
</targets>
<rules>
<logger name="*" minlevel="Info" writeTo="Console"/>
<logger name="*" minlevel="Info" writeTo="Logfile"/>
<logger name="*" levels="Info" writeTo="dbLogs"/>
</rules>
</nlog>
这是 csproj。我只是删除对另一个项目(只是模型)和本地 dll 的引用:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<AssemblyVersion>0.0.5.0</AssemblyVersion>
<FileVersion>0.0.5.0</FileVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Asp.Versioning.Http" Version="8.1.0" />
<PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" Version="8.1.0" />
<PackageReference Include="Azure.Storage.Blobs" Version="12.22.1" />
<PackageReference Include="Dapper" Version="2.1.35" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.4"/>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="NLog" Version="5.3.4" />
<PackageReference Include="NLog.Database" Version="5.3.4" />
<PackageReference Include="NLog.Extensions.Logging" Version="5.3.14" />
<PackageReference Include="NLog.Web.AspNetCore" Version="5.3.14" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0"/>
<PackageReference Include="System.Data.SqlClient" Version="4.8.6" />
<PackageReference Include="System.Drawing.Common" Version="9.0.0-rc.2.24474.1" />
<PackageReference Include="System.Text.Json" Version="9.0.0-rc.2.24473.5" />
</ItemGroup>
</Project>
这些是我收到并尝试禁用的日志:
知道如何禁用该日志或我缺少的内容吗?
我尝试添加命名空间
Microsoft.AspNetCore.Routing
和 Microsoft.AspNetCore.Hosting
但这并没有改变任何东西。
将以下规则添加到
nlog.config
文件时,日志消失了:
<rules>
<logger name="Microsoft.*" final="true" /> <!-- <<<< this rule -->
<logger name="*" minlevel="Info" writeTo="Console"/>
</rules>
final="True"
是为了防止匹配Microsoft.*
命名空间上的记录器的任何其他规则,并注意没有任何writeTo
属性。