Windows service output does not appear in the event log

I am trying to complete the walkthrough.

I am using Visual Studio 2010 Premium.

The only events that I see in the server explorer or in the event viewer are "Service started successfully." and "Service stopped successfully."

Here is the service code:

namespace MyNewService { public partial class MyNewService : ServiceBase { public MyNewService() { InitializeComponent(); if (!EventLog.SourceExists("MySource")) { EventLog.CreateEventSource("MySource", "MyNewLog"); } eventLog1.Source = "MySource"; eventLog1.Log = "MyNewLog"; } protected override void OnStart(string[] args) { eventLog1.WriteEntry("In OnStart"); } protected override void OnStop() { eventLog1.WriteEntry("In onStop."); } } } 
+6
source share
3 answers

I had the same problem with Visual Studio 2010 Professional on Win 7 64. My recently compiled service was installed with InstallUtil.exe under administrator control. It started and stopped correctly. There are no exceptions, but a new event log was not found when updating the Event Viewer. Fran71's offer worked for me: I closed Event Viewer and reopened. Voilà, a new application log file has appeared.

+2
source

FWIW, I ran into this problem in Win8 and found that the Refreshing Event Viewer did not cause the appearance of my newly created event log, but closing and reopening the Event Viewer. I was crazy because there were no exceptions when creating the source of the event or writing to the log.

+1
source

Probably a problem with permissions. As the commentator said, try running VStudio or your compiled service as an administrator.

In addition, you can still debug the service - put thread.Sleep (up to 20 seconds) as the first line (to give time for the debugger to attach), and then put a breakpoint on the line. Then go to Tools -> Attach To Process and select your .exe. If you do this to the end of your Thread.Sleep (), you must break.

Keep in mind that the service must complete OnStart during the second stage of IIRC, or Windows will think that it does not respond and does not kill your process, so move the code from the service start if you are going to debug a lot. Just drop the timer there or something else.

To simplify debugging, think about porting your functionality to a DLL, and then save your service with enough code to call into your DLL - this will facilitate unit testing and debugging - but don't forget that a lot changes when you actually run it as a service.

0
source

Source: https://habr.com/ru/post/897939/


All Articles