如何在 .NET Core 应用程序中配置 NLog
How to configure NLog in a .NET Core app
我正在使用一个名为 DataFlowEx 的库,它需要 NLog 的配置才能输出它的调试和其他信息。
教程显示了使用 xml 个文件的配置。
我想使用 Microsoft.Extensions.Configuration
,并将其指向 XML,但它似乎不起作用...
这是我目前的情况:
var config = new ConfigurationBuilder().AddXmlFile("app.config", true).Build();
和配置文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="common">
<section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" requirePermission="false" />
</sectionGroup>
</configSections>
<common>
<logging>
<factoryAdapter type="Common.Logging.NLog.NLogLoggerFactoryAdapter, Common.Logging.NLog20">
<arg key="configType" value="FILE" />
<arg key="configFile" value="~/NLog.config" />
</factoryAdapter>
</logging>
</common>
</configuration>
和 nlog 配置:
<?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">
<variable name="logFormat" value="${date:format=yy/MM/dd HH\:mm\:ss} [${logger}].[${level}] ${message} ${exception:format=tostring} "/>
<targets>
<target xsi:type="Console" name="console" layout="${logFormat}"/>
<target xsi:type="File" name ="file" fileName="Gridsum.DataflowEx.Demo.log" layout="${logFormat}" keepFileOpen="true"/>
</targets>
<rules>
<logger name ="Gridsum.DataflowEx*" minlevel="Trace" writeTo="console,file"></logger>
</rules>
</nlog>
没有任何输出...我这样做对吗?
也许这可以帮助:
https://github.com/net-commons/common-logging/issues/153
停止使用 app.config,而是使用专用的 nlog.config(记得将 nlog.config 文件配置为 "Copy Always")
我所有的 nlog 代码在 Startup.cs 中看起来都是这样的。即在Configure方法中:
using NLog.Extensions.Logging;
using NLog.Web;
using Nlog;
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
//setup logging
loggerFactory.AddNLog();
env.ConfigureNLog("nlog.config");
app.AddNLogWeb();
// other unrelated stuff...
}
对于控制台应用程序,您不需要任何特殊的东西...
using NLog;
public class Engine
{
private readonly ILogger _logger;
public Engine()
{
_logger = LogManager.GetCurrentClassLogger();
}
public async Task SendAsync(OutboundMessageType messageType)
{
_logger.Trace($"Entered SendAsync() for message type '{messageType}'.");
// other stuff here
}
}
以上只是我项目中的随机class。不需要配置代码。我的 nlog.config 与可执行文件
位于同一目录中
我正在使用一个名为 DataFlowEx 的库,它需要 NLog 的配置才能输出它的调试和其他信息。
教程显示了使用 xml 个文件的配置。
我想使用 Microsoft.Extensions.Configuration
,并将其指向 XML,但它似乎不起作用...
这是我目前的情况:
var config = new ConfigurationBuilder().AddXmlFile("app.config", true).Build();
和配置文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="common">
<section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" requirePermission="false" />
</sectionGroup>
</configSections>
<common>
<logging>
<factoryAdapter type="Common.Logging.NLog.NLogLoggerFactoryAdapter, Common.Logging.NLog20">
<arg key="configType" value="FILE" />
<arg key="configFile" value="~/NLog.config" />
</factoryAdapter>
</logging>
</common>
</configuration>
和 nlog 配置:
<?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">
<variable name="logFormat" value="${date:format=yy/MM/dd HH\:mm\:ss} [${logger}].[${level}] ${message} ${exception:format=tostring} "/>
<targets>
<target xsi:type="Console" name="console" layout="${logFormat}"/>
<target xsi:type="File" name ="file" fileName="Gridsum.DataflowEx.Demo.log" layout="${logFormat}" keepFileOpen="true"/>
</targets>
<rules>
<logger name ="Gridsum.DataflowEx*" minlevel="Trace" writeTo="console,file"></logger>
</rules>
</nlog>
没有任何输出...我这样做对吗?
也许这可以帮助:
https://github.com/net-commons/common-logging/issues/153
停止使用 app.config,而是使用专用的 nlog.config(记得将 nlog.config 文件配置为 "Copy Always")
我所有的 nlog 代码在 Startup.cs 中看起来都是这样的。即在Configure方法中:
using NLog.Extensions.Logging;
using NLog.Web;
using Nlog;
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
//setup logging
loggerFactory.AddNLog();
env.ConfigureNLog("nlog.config");
app.AddNLogWeb();
// other unrelated stuff...
}
对于控制台应用程序,您不需要任何特殊的东西...
using NLog;
public class Engine
{
private readonly ILogger _logger;
public Engine()
{
_logger = LogManager.GetCurrentClassLogger();
}
public async Task SendAsync(OutboundMessageType messageType)
{
_logger.Trace($"Entered SendAsync() for message type '{messageType}'.");
// other stuff here
}
}
以上只是我项目中的随机class。不需要配置代码。我的 nlog.config 与可执行文件
位于同一目录中