Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 225
Prasad Avatar asked Dec 24 '09 17:12

Prasad


People also ask

Does NLog support .NET 6?

The NLog. Web. AspNetCore-package supports the platforms: For ASP.NET Core 6, .

How does NLog work in C#?

NLog is “a free logging platform for . NET, Silverlight and Windows Phone with rich log routing and management capabilities. It makes it easy to produce and manage high-quality logs for your application regardless of its size or complexity.”


1 Answers

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

like image 52
John B Avatar answered Oct 05 '22 07:10

John B