gpt4 book ai didi

asp.net-core - NLog.Web.AspNetCore 布局渲染器为空

转载 作者:行者123 更新时间:2023-12-04 17:38:07 27 4
gpt4 key购买 nike

我正在开发一个 ASP.net 核心 (2.2) MVC 应用程序,但我无法让日志记录工作。 AspNetCore 布局渲染器返回空值。我安装了以下软件包:

    <PackageReference Include="NLog" Version="4.6.2" />
<PackageReference Include="NLog.Extensions.Logging" Version="1.4.0" />
<PackageReference Include="NLog.Web.AspNetCore" Version="4.8.1" />

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"
internalLogLevel="Trace"
internalLogFile="c:\temp\internal-nlog.txt">

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

<!-- the targets to write to -->
<targets>
<!-- write logs to file -->
<target xsi:type="File" name="allfile" fileName="c:\temp\nlog-all-${shortdate}.log"
layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}" />

<!-- another file log, only own logs. Uses some ASP.NET core renderers -->
<target xsi:type="File" name="ownFile-web" fileName="c:\temp\nlog-api-${shortdate}.log"
layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" />
</targets>

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

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

启动.cs

services.AddLogging(builder =>
{
builder.ClearProviders();
builder.SetMinimumLevel(LogLevel.Trace);
builder.AddNLog();
});

日志:

2019-04-19 11:55:01.2521||INFO|MyApp.API.Controllers.ConfigController| |url: |action: 

最佳答案

最简单的方法就是按照 wiki 指南,调用 UseNLog():

https://github.com/NLog/NLog.Web/wiki/Getting-started-with-ASP.NET-Core-2#4-update-programcs

但是如果你喜欢按照自己的方式做事,并且像在 ASP.NET Core 版本中那样做。 1:

https://github.com/NLog/NLog/issues/2859#issuecomment-415865357

关于asp.net-core - NLog.Web.AspNetCore 布局渲染器为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55760209/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com