Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NLog Not Writing

Tags:

nlog

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!

like image 431
onefootswill Avatar asked Dec 20 '22 22:12

onefootswill


2 Answers

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

like image 59
kolbasov Avatar answered Feb 27 '23 08:02

kolbasov


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>
like image 37
BaBu Avatar answered Feb 27 '23 09:02

BaBu