.net core NLog无法读取.NET Core 2.2控制台应用程序中的app.config configSection

.net core NLog无法读取.NET Core 2.2控制台应用程序中的app.config configSection,.net-core,configuration,nlog,.net Core,Configuration,Nlog,我正在对NLog没有从.NET Core 2.2控制台应用程序中的App.config加载其配置的问题进行故障排除 调用NLog.LogManager.GetCurrentClassLogger时,生成的对象为空,没有日志记录目标或其他配置 使用通常的方法:ConfigurationManager.AppSettings[settingKey],可以毫无问题地查找配置项 在尝试解决这个问题的过程中,我调用了ConfigurationManager.GetSectionnlog,看看是否可以手动获

我正在对NLog没有从.NET Core 2.2控制台应用程序中的App.config加载其配置的问题进行故障排除

调用NLog.LogManager.GetCurrentClassLogger时,生成的对象为空,没有日志记录目标或其他配置

使用通常的方法:ConfigurationManager.AppSettings[settingKey],可以毫无问题地查找配置项

在尝试解决这个问题的过程中,我调用了ConfigurationManager.GetSectionnlog,看看是否可以手动获取设置。这引发了以下异常:

System.Configuration.ConfigurationErrorsException: 
'An error occurred creating the configuration section handler for nlog: 
Could not load type 'NLog.Config.ConfigSectionHandler' from assembly 'NLog'
我的示例应用程序中的整个app.config如下所示:

<?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是nuget的4.6.7版。

如果其他人遇到这种情况:

由于NLog不支持.NET Core中的app.config,我的解决方案是使用SlowCheetah环境转换创建一个单独的NLog.config,并运行NLog.LogManager.LoadConfiguration。\\NLog.config;在应用程序开始时


我会将此设置为答案,除非有人提出了一个聪明的解决方案,将所有内容都保存在一个配置中。

可能有多种原因。我面临的一个原因是允许在指定文件夹中创建文件。如果由于以下代码,有任何内部错误将被记录到bin文件夹内的Test_log.txt中。还要确保目标文件夹可用于创建文件

<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>

我想你是想坐在两把椅子之间。NetCore具有appsettings.json。NetFramework具有app.config。你必须做出选择。如果您的应用程序是NetCore,则使用appsettings.json和${configsetting}。另请参见在.net core中说必须使用appsettings.json是不正确的:使用SlowCheetah和App.config时,为不同环境转换配置要简单得多。很高兴您找到了答案。NLog不依赖于nuget package System.Configuration.ConfigurationManager,无法在NetCore中读取app.config。但您可以创建自己的自定义layoutrenderer并实现它,因为读取AppSettings很容易。另请参见备选方案,您可以将想要的AppSettings显式复制到NLog GDC中。另见