4

I have added NLog to my project and in the development environment, it works fine.

I created a Setup file to deploy my application. The NLog.config file did not show up as a dependency in the Setup project. So, I added it as a file and it is present in the same directory as the exe file and App.config when deployed.

It does not do any logging. I don't know why. Here is the config file:

<?xml version="1.0" encoding="utf-8" ?>
<?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">
  <variable name="day" value="${date:format=dd}" />
  <variable name="month" value="${date:format=MM}" />
  <variable name="year" value="${date:format=yyyy}" />  
  <variable name="verbose" value="${longdate} | ${level} | ${message} | ${exception:format=tostring,message,method:maxInnerExceptionLevel=5:innerFormat=shortType,message,method}}" />

  <targets>
    <target name="logfile" xsi:type="File" fileName="${basedir}/Logs/${year}${month}${day}.log" layout="${verbose}" />
  </targets>

  <rules>
    <logger name="*" minlevel="Error" writeTo="logfile" />
  </rules>
</nlog>

Any help would be great. Cheers!

onefootswill
  • 3,707
  • 6
  • 47
  • 101

4 Answers4

7

Does NLog.config have the property "Copy to Output Directory" set as "Copy always"? https://stackoverflow.com/a/8881521/438760

Community
  • 1
  • 1
kolbasov
  • 1,548
  • 11
  • 15
4

Put your NLog configuration within the yourapp.exe.config file. Like so:

<configuration>
   <configSections>
      <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
   </configSections>
   <nlog>
      <variable name="day" value="${date:format=dd}" />
      ...
      <targets>
         <target name="logfile" xsi:type="File" .../>
      </targets>
      <rules>
         <logger name="*" minlevel="Error" writeTo="logfile" />
     </rules>
   </nlog>
</configuration>
BaBu
  • 1,923
  • 2
  • 16
  • 27
0

I'm guessing the double xml version statements (line 1 and 2) was a copy / paste issue....

Probably a dumb question, but you have the minLevel set to Error. Are you actually encountering errors that would be logged, or have you tried lowering this to info or debug?

Joe
  • 5,389
  • 7
  • 41
  • 63
0

For me, the reason my NLog stopped logging was different to the above suggestions.

During updating the packages, something must have automatically added an nlog section to the bottom of my Web.config. It may have been ApplicationInsights. Obviously this took preference and, this caused it to stop checking my Nlog.config file.

Once I removed the section from my web.config it started magically reading from my nlog.config file again. I took care to take the extentions, targets and rules from web.config and ensure they were placed neatly in my nlog.config file instead.

Dirk R
  • 602
  • 7
  • 14