NLog没有在控制台应用程序中读取我的app.config设置

时间:2017-06-14 10:13:22

标签: c# .net console-application app-config nlog

我正在尝试做一点尖峰,但我无法生成日志文件。

这是我的NLog配置:

<?xml version="1.0" encoding="utf-8"?>
<configuration>

  <configSections>
    <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog" />
  </configSections>

  <startup> 
      <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
  </startup>  

  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Npgsql" publicKeyToken="5d8b90d52f46fda7" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.2.4.0" newVersion="3.2.4.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>


  <nlog throwExceptions="true"
        internalLogLevel="Warning"
        internalLogFile="Rebus.Tests.Output.NLog.Internal.log"
        internalLogToConsole="true"
        xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <targets>
      <target name="normalLogfile" type="File" fileName="${basedir}/Rebus.Tests.Output.log" />
      <target name="normalConsole" type="Console" detectConsoleAvailable="true" />
    </targets>

    <rules>
      <logger name="NormalLog" minlevel="Trace" writeTo="normalLogfile, normalConsole" />
    </rules>

  </nlog>

</configuration>

这是我在控制台应用程序中的静态Main:

    var logger = LogManager.GetLogger("NormalLog");

    logger.Error("This is a log error line");

但LogFile和Console都没有记录任何内容。

application.exe.config位于bin / Debug运行时文件夹中。 我正在寻找带有SearchEverything的日志文件,因此可以在任何文件夹中找到它。

如果我设置一个断点来检查logger变量,那么在这个问题中添加一些信息我可以看到没有读取任何配置:

enter image description here

1 个答案:

答案 0 :(得分:2)

Try to change

var logger = LogManager.GetLogger("NormalLog");

to

var logger = LogManager.GetLogger("normalLogfile");

because as far as I know you have to get the logger via target-name and not via rule-name.

//edit

Have you tried to remove the nlog attributes in you app.config? Just to be sure none of them is the problem.

相关问题