0

I have created a service using visual studio and installed it using visual studio command prompt. It works fine

But now I want to install the same service using a visual studio installer. I have created a installer by following this link. The installer does install the service properly as it does not throw any error or exception. But when I start the service from the service list, the service does start but it does not write into the log file. So can anyone help me and tell why the service performs OK while installed using command prompt but causing issues when installed through installer.

protected override void OnStart(string[] args)
{
    timer1 = new Timer();
    this.timer1.Interval = 5000;
    this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_tick);
    timer1.Enabled = true;
    Library.WriteErrorLog("service started!");
}

private void timer1_tick(object sender, ElapsedEventArgs e) 
{ 
    Library.WriteErrorLog("Timer ticked succesfully"); 
}

protected override void OnStop()
{
    timer1.Enabled = false;
    Library.WriteErrorLog("Service Stopped!");
}
Bojan B
  • 1,882
  • 4
  • 16
  • 24
  • Try debugging your service as explained [here](http://stackoverflow.com/a/126016/1630056) – panda Dec 07 '16 at 07:46
  • are you installing the service to run as user, local service or network service? – Simon Price Dec 07 '16 at 07:47
  • @SimonPrice as local service and also tried as local system – waqar ahmed somra Dec 07 '16 at 08:16
  • ok, have you tried attaching VS to the process when its running and seeing what the error message is? – Simon Price Dec 07 '16 at 08:22
  • @SimonPrice no how i can attach – waqar ahmed somra Dec 07 '16 at 08:22
  • when the service is running, you can use either the debug menu or the tools menu and click attach to process, this will open up a new dialog window for you where you can see all the processes on the system, if you don't see your service click show all processes for all users, find your service and then click attach. – Simon Price Dec 07 '16 at 08:30
  • seeing your comments to Terry a few minutes ago, you installed this via the command line as an administrator I assume? and then when you run the installer to you run this as admin or just run it? – Simon Price Dec 07 '16 at 08:31
  • @SimonPrice i did install as administrator for both cases but same issue – waqar ahmed somra Dec 07 '16 at 08:51
  • what happened when you attached to process? – Simon Price Dec 07 '16 at 09:18
  • it says source file 'c:\......Service1.Designer.cs' does not belong to the project being debugged. while its the same project. both Process id are same but process names are different(process in task manager and process while attaching in VS) – waqar ahmed somra Dec 07 '16 at 10:33
  • Sorry Simon i was making a stupid mistake ... there was a issue with path. i was checking the wrong directory. ufffffff... thanku so much for your help – waqar ahmed somra Dec 07 '16 at 13:18

1 Answers1

0

Have you checked that the service has permission to write to the log file?

Check the permissions for the directory, go to log folder - Properties and the Security tab.

Services are usually run without write access to the Windows file system.

Terry Lennox
  • 17,423
  • 2
  • 18
  • 28
  • When you install via the command prompt, is the user different to the user when you use the installer? IE using Task Manager User Name column? Also do you see the service running when you run services,msc in the Run box on the start menu? – Terry Lennox Dec 07 '16 at 08:36
  • Can you post how you install on the command line? That might give us all some clues? – Terry Lennox Dec 07 '16 at 08:59
  • cd "directory to service.exe" installutil service.exe – waqar ahmed somra Dec 07 '16 at 10:26
  • Could it be that one of the properties in the service installer is not correct? – Terry Lennox Dec 07 '16 at 11:12
  • I've created a basic service using the same mechanism and installed it. I'm getting events in the Event Log when the service starts. I've used the LocalSystem account as well. Have you tried EventLog.WriteEntry("My message") as another test? – Terry Lennox Dec 07 '16 at 11:14
  • yes in event log service is there. but when i am trying to write my own logs it doesnot work only when installed through installer – waqar ahmed somra Dec 07 '16 at 12:37
  • Sorry Terry i was making a stupid mistake ... there was a issue with path. i was checking wrong directory. ufffffff... thanku so much for your help – waqar ahmed somra Dec 07 '16 at 13:17
  • No problem, I'm glad you got this working, I have been in the same position myself!! – Terry Lennox Dec 07 '16 at 15:25