开发者

NLog not working in release mode

I am using NLog to log the exceptions in my asp.net mvc (C#) application.

NLog is not working in release mode. The same is 开发者_运维问答working when running in debug mode.

What may be the problem? Is there any fix for this?


I was having the same problem as you:

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

I tried changing directories, and changing permissions to no avail. I even tried enabling the internal logging but even that didn't work! No failures, no exceptions, nothing!

After doing some more investigating, I found the solution. For some reason, NLog wasn't loading the config file AT ALL. I realized this after I programmatically enabled the internal logging. The internal logging reported this:

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

This was basically saying that there were no targets defined for any of the log levels! Definitely not correct!

My NLog configuration file was as simple as it could be (and it was set to Copy to Output Directory):

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

I'm still not sure exactly why this was happening, but moving the NLog configuration into the web.config directly resolved the problem.

See also: https://github.com/nlog/NLog/wiki/Configuration-file#configuration-file-format


set up environment variables:NLOG_INTERNAL_LOG_LEVEL and NLOG_INTERNAL_LOG_FILE, rerun your release build then check the log file see what's wrong


For anyone, who is not sure about 'why nlog is not working in prod environment':

  1. Go to Nlog.config file
  2. SET throwExceptions="true" in nlog tag and start debugging with proper errors.

Good luck.


I think you should provide to your publish directory IIS_IUSRS to Write Permission.


Transfer nlog config to config file of your application (web.config for example), and try again.


Make sure your target file saves within a "/logs/" folder. See below

<target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log"
            layout="${longdate} ${uppercase:${level}} ${message}" />

I tried to log into "root/log.log" and was not working, then tried "root/logs/log.log" and worked

Below full config file.

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


You can activate the NLog InternalLogger from code, so you can rule out the issue with correct deployment of 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");

See also: https://github.com/NLog/NLog/wiki/Internal-Logging

See also: https://github.com/NLog/NLog/wiki/Logging-troubleshooting


Another thing worth checking is the write permission of your log directory and/or files.

Permission error, or any other error, will show up in the internal log if enabled. This is how I set up my 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>


Assuming you've configured NLog in the right way, like the other answers suggest, Try one of these

  1. Write Permission for your Server

    If you are using IIS, give WRITE permission for user IIS_IUSRS for your log folder.

  2. Constructor

     public LoggerService() {
         _logger = NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
     }
    
    The path for nlog.config could be different for you. Mine was on the same folder
    
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜