Search code examples
c#.net-coreconfiguration-filesnlog

How to configure NLog in a .NET Core app


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?


Solution

  • 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")