NLog在发布模式下无法工作

15

我正在使用NLog来记录我的asp.net mvc(C#)应用程序中的异常。

NLog在发布模式下不起作用。但在调试模式下运行正常。

可能的问题是什么?有没有解决方法?


你的发布版本中是否包含了nlog配置文件? - curtisk
所以,如果你在本地同时运行它们(即,只需按下ctrl-f5),它在发布时失败了?你有收到任何错误吗? - Beep beep
当我在本地以发布模式运行时,它无法正常工作。错误信息类似于“无法解析ILogger”。 - Prasad
嗨,Prasad。你解决了这个问题吗?我在我的MVC 3应用程序上也遇到了同样的问题。 - niaher
@niaher,我现在正在使用http://code.google.com/p/elmah进行错误处理。这非常有用。看一下它,也许对你有帮助。 - Prasad
显示剩余2条评论
9个回答

6

我遇到了和你一样的问题:

  • ASP.NET MVC 3
  • .NET 4
  • IIS 7
  • Release Mode

我尝试更改目录和权限,但都没有用。 我甚至尝试启用内部日志记录,但是仍然无法解决! 没有失败、没有异常、什么都没有发生!

经过进一步调查,我找到了解决方案。出现这种情况是因为无法加载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


我已经这样做了,现在它显示web.config文件无效。 - The One
@Tuco,你现在的配置是什么样子? - John B
2
请忽略,它现在可以工作了,也许你应该提到需要一个部分声明。https://gist.github.com/kishorejangid/4004715 - The One

2
我认为您应该将IIS_IUSRS提供给发布目录以获得写入权限。

1
设置环境变量:NLOG_INTERNAL_LOG_LEVEL和NLOG_INTERNAL_LOG_FILE,重新运行您的发布构建,然后检查日志文件,看看出了什么问题。

我尝试过这个,但是没有成功。在调试模式下本地可以运行,但是当我部署到发布模式时,即使内部记录日志也不起作用。我很确定我的权限足够。 - John B

1

对于任何不确定为什么nlog在生产环境中无法工作的人:

  1. 进入Nlog.config文件
  2. nlog标签中设置throwExceptions="true",并使用正确的错误调试。

祝你好运。


0

假设您已经按照其他答案所建议的正确配置了NLog,请尝试以下操作之一

  1. 为您的服务器添加写入权限

    如果您正在使用IIS,请为用户IIS_IUSRS在日志文件夹中授予写入权限。

  2. 构造函数

     public LoggerService() {
         _logger = NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
     }
    
    nlog.config的路径可能对您不同。我的位于同一文件夹中
     

0
请确保您的目标文件保存在“/logs/”文件夹中。请参见下文。
<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>

0

另一个值得检查的事情是您的日志目录和/或文件的写入权限。

如果启用了内部日志记录,则会显示权限错误或任何其他错误。这是我设置我的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>

0
将nlog配置文件转移到您的应用程序配置文件中(例如web.config),然后再试一次。

0

您可以从代码中激活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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接