Quantcast

Nlog not working with Windows service

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Nlog not working with Windows service

Sharad
Hi I am using VS 2010 to create Windows service. below is my service code and app.config

using NLog;

namespace MyFirstService
{
    public partial class Service1 : ServiceBase
    {
        private static Logger logger = LogManager.GetCurrentClassLogger();  
        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            logger.Info("My first service log info. On Start");
           
            logger.Error("***************************");
            logger.Error("My first service log error. On Start");
            logger.Error("***************************");
        }

        protected override void OnStop()
        {
            logger.Info("My first service log info. On Stop");
        }
    }
}

--config--
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog" />
  </configSections>
  <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <targets>
      <target name="console" layout="${shortdate}|${level}|${message}" xsi:type="Console" />
     
      <target name="file" layout="${longdate}|${stacktrace}|${message}" xsi:type="File" fileName="c:\Logs\Servicelogger.txt" />
      <target name="csv" xsi:type="File" fileName="${basedir}/file.csv">
        <layout xsi:type="CSVLayout">
          <column name="time" layout="${longdate}" />
          <column name="message" layout="${message}" />
          <column name="logger" layout="${logger}"/>
          <column name="level" layout="${level}"/>
        </layout>
      </target>
      <target xsi:type="EventLog"
         name="String"
         layout="Layout"
         machineName="String"
         source="String"
         category="Layout"
         eventId="Layout"
         log="String" />

    </targets>
    <rules>
      <logger name="*" writeTo="console" minlevel="Debug"  />
      <logger name="22" writeTo="file" minlevel="Info" />
      <logger name="*" minlevel="Debug" writeTo="csv" />
      <logger name="*" minlevel ="Fatal" writeTo ="file" />     

    </rules>
  </nlog>
</configuration>

above code works fine with winforms application.
But in Windows service I see it is not logging, I have tried below to resolve it but no luck

1) Ensured nlog.dll and its config file are in same installation directory ie c:\prog files\company\product\
2) when above not worked Tried keeping nlog.xml also in above directory
3) I am admin into system and service scope is local system, Also started service with admin credentails
4) Create file in advance and checked if anything is logged
5) Check system\32 and 64 folder if logs are created there
6) Added system.configuration reference
7) Checked if any exception while running Nlog code by attaching service to code, all runs fine without exception
8) Eventviewer message does not show any trace for failure in Service
9) Quickwatch window while debugging shows all value properly captured in Logger instance including rules and file name/location

All above trid with no luck. Can someone please help and let me know to use Nlog in Windows service what are
steps mandatorily required to do effective logging.

Thanks
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Out of Office AutoReply: Nlog not working with Windows service

ian.clegg
Im currently out of the office until 22nd August 2011.
Please contact G 1ST IT&S FrontOfficeFramework (G1STIT&[hidden email]) for assistance
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Nlog not working with Windows service

Jason LaFlair
Administrator
In reply to this post by Sharad
I guess the first think I'd look at is changing your file target:

From: c:\Logs\Servicelogger.txt
To: c:/Logs/Servicelogger.txt

then check the file permissions for each of the directories and verify you have the proper file permissions.

Also - to rule file permissions our try using something like "${specialfolder:folder=CommonApplicationData}/myapp/log.txt" for a file target filename.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Nlog not working with Windows service

Sharad
Hi Jason,

I tried as you suggested, but it does not work. Same working all the way in sample winforms application. However I see I can create csv file anywhere. Looks like logging in text file not working. Let me know your advice next.

--<target name="file" layout="${longdate}|${stacktrace}|${message}" xsi:type="File" fileName="c:\Logs\logmydata.txt" />
--<target name="file" layout="${longdate}|${stacktrace}|${message}" xsi:type="File" fileName="c:/Logs/logmydata.txt" />
--<target name="file" layout="${longdate}|${stacktrace}|${message}" xsi:type="File" fileName="${specialfolder:folder=CommonApplicationData}/myapp/logmydata.txt" />
--<target name="file" layout="${longdate}|${stacktrace}|${message}" xsi:type="File" fileName="${specialfolder:folder=CommonApplicationData}\myapp\logmydata.txt" />

csv - target
 
      <target name="csv" xsi:type="File" fileName="C:\logs\fileService.csv">
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Nlog not working with Windows service

Sharad
I found the issue. Its in Rules section
-- <logger name="*" writeTo="file" minlevel="Info" />

If i put * in logger name than it works else do not.
Is it a bug ? Can we not give logger names in windows service ?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Nlog not working with Windows service

DCastenholz
For anyone looking at this thread in the future, another reason to not see output is if you do not specify the directory on the file.

ex:  <target name="file" fileName="log_NLog.log" xsi:type="File" layout="${date} ${message}"/>

When a windows service starts up, the current directory is windows\system32 (or the equivalent).  Look in this directory for output.  To solve this problem, specify the directory

ex:  <target name="file" fileName="${basedir}/log_NLog.log" xsi:type="File" layout="${date} ${message}"/>
Loading...