我有我的nlog.config
具有数据库目标的文件设置,并希望从 appsettings.json
读取连接字符串(对于任何感兴趣的人来说,appsettings
中的值实际上存储在 Azure KeyVault 中)。执行此操作的正确方法是什么?我有 ASP.NET core 3.1 应用程序。
nlog.config
<?xml version="1.0" encoding="utf-8" ?>
<!--The first nLog section internal log file logs the NLog configuration issues, Don't remove this-->
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true"
internalLogLevel="debug"
internalLogFile="c:\temp\foo.txt">
<extensions>
<add assembly="NLog.Extensions.Logging"/>
<add assembly="NLog.Web.AspNetCore"/>
</extensions>
<variable name="defaultLayout" value="${date} ${threadid} ${uppercase:${level}} ${logger} ${ndc} - ${message}${newline}" />
<variable name="logDirectory" value="./logs/${shortdate}"/>
<variable name="apiFileLogPath" value="c:\temp\api-nlog.txt"/>
<!-- the targets to write to -->
<targets>
<target name="dbTarget"
xsi:type="Database"
commandText="..."
connectionString="${configsetting:item=NLogConfiguration.ConnectionString}">
<!--<connectionString></connectionString>-->
</target>
<!-- write to the void aka just remove -->
<target xsi:type="Null" name="blackhole" />
</targets>
<!-- rules to map from logger name to target -->
<rules>
<!--Skip Microsoft logs and so log only own logs-->
<!--<logger name="*" minlevel="Trace" writeTo="blackhole" final="true" />-->
<logger name="*" minlevel="Debug" writeTo="dbTarget" />
</rules>
</nlog>
appsettings.json
的相关部分文件:
{
"NLogConfiguration": {
"ConnectionString": "..."
}
}
我的代码中有一个断点来检查数据库目标,但 ConnectionString 值未设置。它保持为 ${configsetting:item=NLogConfiguration.ConnectionString}
我在启动时没有任何 NLog 代码。我有一个围绕 NLog 的包装类,并以这种方式初始化它。该类如下所示:
DBLogManager
using System;
using System.Configuration;
using System.Globalization;
using System.Web;
using NLog;
using System.Xml;
using System.IO;
using System.Reflection;
using System.Text;
using Microsoft.Extensions.Configuration;
namespace DBLog.DBLogManager
{
public class DBLogManager : IDBLogManager
{
static ILogger InitializeDBLogManager()
{
XmlDocument nLogConfig = new XmlDocument();
var path = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "nlog.config");
NLog.LogManager.Configuration = new NLog.Config.XmlLoggingConfiguration(path);
return LogManager.GetLogger("AppLogger");
}
private static readonly ILogger Logger = InitializeDBLogManager();
}
}
最佳答案
我会将DBLogManager
更改为:
namespace DBLog.DBLogManager
{
public class DBLogManager : IDBLogManager
{
// NLog will automatically load NLog.config from AppDomain.BaseDirectory
private static readonly NLog.Logger Logger = NLog.LogManager.GetLogger("AppLogger");
}
}
只需添加 UseNLog()
即可,如下所示:
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseNLog();
如果您想在 IHostBuilder 之前创建 NLog Logger,请执行以下操作(而不是 NLogBuilder.ConfigureNLog
):
var logger = LogManager.Setup()
.LoadConfigurationFromAppSettings()
.GetCurrentClassLogger();
关于c# - 如何正确从 appsettings.json 中提取 NLog DB ConnectionString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68070775/