NLog 没有在控制台应用程序中读取我的 app.config 设置
NLog is not reading my app.config settings in a Console application
我正在尝试做一个小峰值,但无法生成日志文件。
这是我的 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");
但是日志文件和控制台都没有记录任何内容。
application.exe.config
位于 bin/Debug 运行时文件夹中。
我正在使用 SearchEverything 查找日志文件,以便它可以在它所在的任何文件夹中找到。
如果我放置一个断点来检查 logger
变量,则向这个问题添加一些信息我可以看到没有读取配置:
尝试改变
var logger = LogManager.GetLogger("NormalLog");
至
var logger = LogManager.GetLogger("normalLogfile");
因为据我所知,您必须通过目标名称而不是规则名称获取记录器。
//编辑
您是否尝试过删除 app.config 中的 nlog 属性?只是为了确定其中 none 是问题所在。
我正在尝试做一个小峰值,但无法生成日志文件。
这是我的 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");
但是日志文件和控制台都没有记录任何内容。
application.exe.config
位于 bin/Debug 运行时文件夹中。
我正在使用 SearchEverything 查找日志文件,以便它可以在它所在的任何文件夹中找到。
如果我放置一个断点来检查 logger
变量,则向这个问题添加一些信息我可以看到没有读取配置:
尝试改变
var logger = LogManager.GetLogger("NormalLog");
至
var logger = LogManager.GetLogger("normalLogfile");
因为据我所知,您必须通过目标名称而不是规则名称获取记录器。
//编辑
您是否尝试过删除 app.config 中的 nlog 属性?只是为了确定其中 none 是问题所在。