gpt4 book ai didi

asp.net-core - appsettings.json 中的 NLog 配置而不是 .NET Core 中的 nlog.config

转载 作者:行者123 更新时间:2023-12-03 03:36:57 34 4
gpt4 key购买 nike

NLog 文档解释了如何使用 nlog.config XML 文件为 .NET Core 应用程序配置 NLog。不过,我希望我的应用程序只有一个配置文件 - appsettings.json。对于 .NET Framework 应用,可以将 NLog 配置放入 app.configweb.config 中。是否可以以同样的方式将 NLog 配置放入 appsettings.json 中?

例如,我如何将这个配置示例放在 NLog documentation for ASP.NET Core 2 中进入appsettings.json

<?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="Info"
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-own-${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" /> <!-- BlackHole without writeTo -->
<logger name="*" minlevel="Trace" writeTo="ownFile-web" />
</rules>
</nlog>

最佳答案

是的,这是可能的,但有最低版本要求。您必须使用 NLog.Extensions.Logging >= 1.5.0。请注意,对于 ASP.NET Core 应用程序,如果您安装 NLog.Web.AspNetCore >= 4.8.2,这将作为依赖项安装。

然后,您可以在 appsettings.json 中创建 NLog 部分,并使用以下代码加载它:

var config = new ConfigurationBuilder()
.SetBasePath(System.IO.Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true).Build();
NLog.Config.LoggingConfiguration nlogConfig = new NLogLoggingConfiguration(config.GetSection("NLog"));

例如,对于 ASP.NET Core 应用程序,Program.cs 中的 Main() 方法应如下所示:

public static void Main(string[] args)
{
var config = new ConfigurationBuilder()
.SetBasePath(System.IO.Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true).Build();
LogManager.Configuration = new NLogLoggingConfiguration(config.GetSection("NLog"));

var logger = NLog.Web.NLogBuilder.ConfigureNLog(LogManager.Configuration).GetCurrentClassLogger();
try
{
logger.Debug("Init main");
CreateWebHostBuilder(args).Build().Run();
}
catch (Exception ex)
{
logger.Error(ex, "Stopped program because of exception");
}
finally {
LogManager.Shutdown();
}
}

可以通过 appsettings.json 中的以下设置来实现与问题中类似的配置:

"NLog":{
"internalLogLevel":"Info",
"internalLogFile":"c:\\temp\\internal-nlog.txt",
"extensions": [
{ "assembly": "NLog.Extensions.Logging" },
{ "assembly": "NLog.Web.AspNetCore" }
],
"targets":{
"allfile":{
"type":"File",
"fileName":"c:\\temp\\nlog-all-${shortdate}.log",
"layout":"${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}"
},
"ownFile-web":{
"type":"File",
"fileName":"c:\\temp\\nlog-own-${shortdate}.log",
"layout":"${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}"
}
},
"rules":[
{
"logger":"*",
"minLevel":"Trace",
"writeTo":"allfile"
},
{
"logger":"Microsoft.*",
"maxLevel":"Info",
"final":"true"
},
{
"logger":"*",
"minLevel":"Trace",
"writeTo":"ownFile-web"
}
]
}

编辑:感谢 Rolf Kristensen(他首先为 NLog 开发了此功能!)指出此 wiki 页面以及有关此功能的更多文档:https://github.com/NLog/NLog.Extensions.Logging/wiki/NLog-configuration-with-appsettings.json

关于asp.net-core - appsettings.json 中的 NLog 配置而不是 .NET Core 中的 nlog.config,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56246416/

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