[英]How to Properly Pull NLog DB ConnectionString from appsettings.json
I have my nlog.config
file setup with a database target and want to read connectionstring from appsettings.json
(and for anyone interested, the value in appsettings
is actually stored in Azure KeyVault).我有一个带有数据库目标的
nlog.config
文件设置,并想从appsettings.json
读取连接appsettings.json
(对于任何感兴趣的人, appsettings
的值实际上存储在 Azure KeyVault 中)。 What is the proper way to do this?这样做的正确方法是什么? I have ASP.NET core 3.1 application.
我有 ASP.NET 核心 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>
Relevant part of appsettings.json
file: appsettings.json
文件的相关部分:
{
"NLogConfiguration": {
"ConnectionString": "..."
}
}
I have a breakpoint in my code to inspect the database target but the connectionString value doesn't get set.我的代码中有一个断点来检查数据库目标,但没有设置 connectionString 值。 it is staying as
${configsetting:item=NLogConfiguration.ConnectionString}
它保持为
${configsetting:item=NLogConfiguration.ConnectionString}
I don't have any NLog code at startup.我在启动时没有任何 NLog 代码。 I have a wrapper class around NLog and am initializing it that way.
我有一个围绕 NLog 的包装类,并以这种方式初始化它。 That class is shown below:
该类如下所示:
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();
}
}
I would change the DBLogManager
to this:我会将
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");
}
}
And just add UseNLog()
like this:只需像这样添加
UseNLog()
:
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseNLog();
If you want to have NLog Logger created before the IHostBuilder, then do this (Instead of NLogBuilder.ConfigureNLog
):如果您想在 IHostBuilder 之前创建 NLog Logger,请执行以下操作(而不是
NLogBuilder.ConfigureNLog
):
var logger = LogManager.Setup()
.LoadConfigurationFromAppSettings()
.GetCurrentClassLogger();
For ${configsetting}
you need at least the package NLog.Extensions.Logging or one of the dependent packages like NLog.Web.AspNetCore or NLog.Extensions.Hosting.对于
${configsetting}
您至少需要NLog.Extensions.Logging包或 NLog.Web.AspNetCore 或 NLog.Extensions.Hosting 等依赖包之一。
Normally you would use .UseNLog()
on IHostBuilder
, see NLog - Getting started with ASP.NET Core 3通常您会在
IHostBuilder
上使用.UseNLog()
,请参阅NLog - Getting started with ASP.NET Core 3
To manual register the Microsoft Extension IConfiguration
with ${configsetting}
:要使用
${configsetting}
手动注册 Microsoft 扩展IConfiguration
:
IConfigurationRoot config = new ConfigurationBuilder()
.AddJsonFile(path: "AppSettings.json").Build();
NLog.Extensions.Logging.ConfigSettingLayoutRenderer.DefaultConfiguration = config;
SeeNLog - ConfigSetting Layout Renderer参见NLog - ConfigSetting 布局渲染器
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.