Reputation: 3066
I am using a library called DataFlowEx and it requires a configuration for NLog in order to output it's debug and other information.
The tutorial shows the configuration using xml files.
I thought of using Microsoft.Extensions.Configuration
, and pointing that to the XML but it doesn't seem to work...
Here is what I have so far:
var config = new ConfigurationBuilder().AddXmlFile("app.config", true).Build();
And the config file:
<?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>
And the 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">
<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>
Nothing is ever output... Am I doing this right?
Upvotes: 0
Views: 14751
Reputation: 11233
All my code for nlog looks like this in the Startup.cs. Namely in the Configure method:
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...
}
For Console apps you don't need anything special...
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
}
}
The above is just from a random class in my project. There is no configuration code needed. My nlog.config resides in the same directory as the executable
Upvotes: 2
Reputation: 19952
Maybe this can help:
https://github.com/net-commons/common-logging/issues/153
Stop using the app.config, but instead use a dedicated nlog.config (Remember to configure the nlog.config-file to "Copy Always")
Upvotes: 4