我正在使用NLog来记录我的asp.net mvc(C#)应用程序中的异常。
NLog在发布模式下不起作用。但在调试模式下运行正常。
可能的问题是什么?有没有解决方法?
我正在使用NLog来记录我的asp.net mvc(C#)应用程序中的异常。
NLog在发布模式下不起作用。但在调试模式下运行正常。
可能的问题是什么?有没有解决方法?
我遇到了和你一样的问题:
我尝试更改目录和权限,但都没有用。 我甚至尝试启用内部日志记录,但是仍然无法解决! 没有失败、没有异常、什么都没有发生!
经过进一步调查,我找到了解决方案。出现这种情况是因为无法加载NLog配置文件。我在程序中启用了内部日志记录后发现了这一点。内部日志记录报告如下:
2012-02-13 11:34:40.3181 Debug Targets for MyMvcController by level:
2012-02-13 11:34:40.3181 Debug Trace =>
2012-02-13 11:34:40.3181 Debug Debug =>
2012-02-13 11:34:40.3181 Debug Info =>
2012-02-13 11:34:40.3181 Debug Warn =>
2012-02-13 11:34:40.3181 Debug Error =>
2012-02-13 11:34:40.3181 Debug Fatal =>
这句话的意思是所有的日志级别都没有定义目标!绝对不正确!
我的NLog配置文件非常简单(并且已设置为复制到输出目录):
<configuration>
<configSections>
<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
</configSections>
<!-- Other XML Sections -->
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<target name="file" xsi:type="File" fileName="${basedir}/MyApplication.log" />
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="file" />
</rules>
</nlog>
</configuration>
我仍然不确定为什么会发生这种情况,但是将NLog配置移动到web.config中直接解决了问题。
另请参阅:https://github.com/nlog/NLog/wiki/Configuration-file#configuration-file-format
对于任何不确定为什么nlog在生产环境中无法工作的人:
祝你好运。
假设您已经按照其他答案所建议的正确配置了NLog,请尝试以下操作之一
为您的服务器添加写入权限
如果您正在使用IIS,请为用户IIS_IUSRS在日志文件夹中授予写入权限。
构造函数
public LoggerService() {
_logger = NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
}
nlog.config的路径可能对您不同。我的位于同一文件夹中
<target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log"
layout="${longdate} ${uppercase:${level}} ${message}" />
我尝试登录"root/log.log"但无法正常工作,然后尝试了"root/logs/log.log"并成功了。
以下是完整的配置文件。
<?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"
xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
autoReload="true"
throwExceptions="false"
internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log" >
<!-- optional, add some variabeles
https://github.com/nlog/NLog/wiki/Configuration-file#variables
-->
<variable name="myvar" value="myvalue"/>
<!--
See https://github.com/nlog/nlog/wiki/Configuration-file
for information on customizing logging rules and outputs.
-->
<targets>
<!--
add your targets here
See https://github.com/nlog/NLog/wiki/Targets for possible targets.
See https://github.com/nlog/NLog/wiki/Layout-Renderers for the possible layout renderers.
-->
<!--
Writing events to the a file with the date in the filename. -->
<target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log"
layout="${longdate} ${uppercase:${level}} ${message}" />
</targets>
<rules>
<!-- add your logging rules here -->
<!--
Write all events with minimal level of Debug (So Debug, Info, Warn, Error and Fatal, but not Trace) to "f"-->
<logger name="*" minlevel="Debug" writeTo="f" />
</rules>
</nlog>
另一个值得检查的事情是您的日志目录和/或文件的写入权限。
如果启用了内部日志记录,则会显示权限错误或任何其他错误。这是我设置我的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"
xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
autoReload="true"
<!-- setting to True will break your application so be careful -->
throwExceptions="false"
<!-- change level to your preference -->
internalLogLevel="Error" internalLogFile="c:\your-path\nlog-internal.log">
<!-- your NLog settings -->
<!-- ... -->
</nlog>
您可以从代码中激活NLog InternalLogger,以便排除NLog.config正确部署的问题:
// enable internal logging to the console
NLog.Common.InternalLogger.LogToConsole = true;
// enable internal logging to a file
NLog.Common.InternalLogger.LogFile = "c:\\nlog-internal.txt"; // On Linux one can use "/home/nlog-internal.txt"
// set internal log level
NLog.Common.InternalLogger.LogLevel = LogLevel.Debug;
// Perform test output, ensure first NLog Logger is created after InternalLogger is enabled.
NLog.LogManager.GetLogger("Test").Info("Hello World");
另请参阅:https://github.com/NLog/NLog/wiki/Internal-Logging
另请参阅:https://github.com/NLog/NLog/wiki/Logging-troubleshooting