0

I am troubleshooting an issue where NLog isn't loading it's configuration from the App.config in a .NET Core 2.2 console application.

When calling NLog.LogManager.GetCurrentClassLogger() the resulting object is blank and has no logging targets or other configuration.

<appSettings> configuration items can be looked up without issue using the usual method: ConfigurationManager.AppSettings["settingKey"].

In the process of trying to figure this out, I called ConfigurationManager.GetSection("nlog") to just see if I could get the settings manually. This threw the following exception:

System.Configuration.ConfigurationErrorsException: 
'An error occurred creating the configuration section handler for nlog: 
Could not load type 'NLog.Config.ConfigSectionHandler' from assembly 'NLog'

The entire app.config from my sample app looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
  </configSections>
  <appSettings>
    <add key="value1" value="TEST1"/>
    <add key="value2" value="TEST2"/>
  </appSettings>
  <nlog>
    <targets>
      <target name="logfile" type="File" fileName="${basedir}/logs/${shortdate}.log" layout="${longdate} ${uppercase:${level}} ${message}" />
      <target name="logconsole" type="Console" />
    </targets>
    <rules>      
      <logger name="*" minlevel="Info" writeTo="logconsole"/>
      <logger name="*" minlevel="Info" writeTo="logfile"/>
    </rules>
  </nlog>
</configuration>

NLog is version 4.6.7 from nuget.

DDevs
  • 31
  • 6
  • 2
    Think you are trying to sit down between two chairs. NetCore has appsettings.json. NetFramework has app.config. You have to make a choice. If your application is NetCore then use appsettings.json and `${configsetting}`. See also https://github.com/NLog/NLog/wiki/ConfigSetting-Layout-Renderer – Rolf Kristensen Oct 11 '19 at 15:59
  • Saying you must use appsettings.json in .net core is incorrect: https://stackoverflow.com/a/47636600/10660197 Transforming configs for different environments is much more straightforward when using SlowCheetah and App.config. – DDevs Oct 11 '19 at 17:11
  • 1
    Glad you found the answer. NLog does not depend on nuget package `System.Configuration.ConfigurationManager` and cannot read app.config in NetCore. But you can create your own custom layoutrenderer and make it happen, since reading AppSettings is easy. See also https://github.com/NLog/NLog/wiki/How-to-write-a-custom-layout-renderer alternative you can explicit copy the wanted AppSettings into NLog GDC. See also https://github.com/NLog/NLog/wiki/Gdc-layout-renderer – Rolf Kristensen Oct 11 '19 at 18:04

2 Answers2

2

In case anyone else runs into this:

Since NLog doesn't support app.config in .NET Core, my solution was to create a separate nlog.config (with SlowCheetah environment transforms) and run NLog.LogManager.LoadConfiguration(".\\nlog.config"); at the beginning of the application.

I'll set this as the answer unless someone rolls through with a clever workaround for keeping things all in the one config.

DDevs
  • 31
  • 6
0

There might be multiple reasons. One reason I faced was permission for the file to be created on designated folder. If there is any internal errors that will be logged to the Test_log.txt inside bin folder because of below code. Also make sure the target folder is accessible to create file.

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xsi:schemaLocation="NLog NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      autoReload="true" internalLogLevel="Warn"
      internalLogFile="Test_log.txt">

  <extensions>
    <add assembly="Microsoft.ApplicationInsights.NLogTarget" />
  </extensions>

  <!-- the targets to write to -->
  <targets>
    <!-- write logs to file -->

    <target xsi:type="File" name="ownFile" fileName="C:\Logs\log_${shortdate}.log"
                 layout="${longdate}|${logger}|${uppercase:${level}}|  ${message} ${exception}" />

                 <target xsi:type="Null" name="blackhole" />

  </targets>

  <rules>
    <logger name="Microsoft.*" minlevel="Trace" writeTo="blackhole" final="true" />
    <logger name="*" minlevel="Info" writeTo="ownFile" />
  </rules>
</nlog>
Amir
  • 1,855
  • 3
  • 24
  • 40