6

I am trying to follow this walkthrough.

I'm using Visual Studio 2010 Premium.

The only events I see in Server Explorer or in 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.");
        }
    }
}
abatishchev
  • 98,240
  • 88
  • 296
  • 433
feenst
  • 61
  • 1
  • 2

3 Answers3

3

I had the same issue with Visual Studio 2010 Professional on Win 7 64. My newly compiled service was installed with InstallUtil.exe running as Administrator. It started and stopped correctly. No exceptions but no amount of refreshing the Event Viewer revealed a new event log. Fran71's suggestion worked for me: I closed the Event Viewer and reopened. Voilà, the newly created application log file appeared.

FumblesWithCode
  • 441
  • 3
  • 7
  • Welcome to Stack Overflow! Please use the *Post answer* button only for actual answers. Invest some time in the site and you will gain sufficient [privileges](http://stackoverflow.com/privileges) to upvote answers you like, which is the Stack Overflow way of saying thank you. – dcastro Mar 20 '14 at 00:24
  • 1
    Thanks FumblesWithCode(or Fran71, although could not find him here). Closing and reopening it was what worked for me as well. – Veverke Jul 20 '15 at 12:40
  • "Fran71" seems to equal "Jordan" – BIBD Jun 24 '16 at 21:24
1

FWIW, I ran into this problem in Win8 and found that Refreshing the Event Viewer did not cause my newly created Event Log to show up, but closing and reopening Event Viewer did. Was driving me insane since no exceptions were being thrown when Creating the Event source or writing a log entry.

Jordan
  • 639
  • 1
  • 13
  • 30
  • *no exceptions were being thrown when Creating the Event source* Well according to [MSDN](http://msdn.microsoft.com/en-us/library/2awhba7a(v=vs.110).aspx): *Create the new event source during the installation of your application. This allows time for the operating system to refresh its list of registered event sources and their configuration. If the operating system has not refreshed its list of event sources, and you attempt to write an event with the new source, the write operation will fail.* So creating the event source, at least, is asynchronous. – ta.speot.is Feb 02 '14 at 04:50
  • That makes sense, but the write operation was succeeding and all my previously written logs while testing showed up in the correct place once the Viewer was reopened. Just a little confusing. – Jordan Feb 02 '14 at 15:46
  • OMG thankyou. I could see my service starting up in the Application log, but my Application's Log hadn't shown up until I restarted the Event Viewer. There we are, every single one of the hundreds of tests that I ran today. '*sob*' – BIBD Jun 24 '16 at 21:23
0

Probably a permissions problem. Like the commenter said, try running VStudio or your compiled service as an Administrator.

Also, you can still debug a service - put a thread.Sleep(up to 20 seconds) as your first line (to give yourself time to attach the debugger), then put a break point on the line after that. Then go Tools --> Attach To Process and select your .exe. If you get that done before the end of your Thread.Sleep() then you should break.

Keep in mind that the service must complete the OnStart within IIRC 30 seconds or Windows will think it is not responding and kill your process, so move your code out of the service start if you are going to do much debugging. Just throw a timer in there or something.

To make it easier to debug, consider moving your functionality to a DLL and then keep your service to just enough code to call into your DLL - this will ease unit testing and debugging - but don't forget that lots of things change when you're actually running it as a service.

ScottBai
  • 1,471
  • 1
  • 14
  • 15
  • instead of the `Thread.Sleep` trick, you can call [`Debugger.Launch()`](https://msdn.microsoft.com/en-us/library/system.diagnostics.debugger.launch(v=vs.110).aspx) which will break and wait for a debugger to be attached – default Dec 05 '16 at 10:06