Search code examples
c#windows-servicesevent-viewer

Windows Service started and then stopped, but no logging


Yesterday, I installed the executable C:\Users\urban\Documents\Visual Studio 2013\Projects\TestService\obj\Release\TestService.exe as a service in Windows 10, so that it now shows up in services.msc. I then started it, and the error message was like

The "service name" service on Local Computer started and then stopped. Some services stop automatically if they are not in use by other service or programs.

Today, I went back into VS and added EventLog and try...catch, so that my code now looks like this:

internal class TestService : ServiceBase
{
    Thread Worker;
    AutoResetEvent StopRequest = new AutoResetEvent(false);
    Toolkit toolkit;

    protected override void OnStart(string[] args)
    {
        try {
            EventLog.Log = "Application";
            EventLog.Source = "TestService";
            EventLog.WriteEntry("Starting Test Service", EventLogEntryType.Information);
            var User = System.Security.Principal.WindowsPrincipal.Current;
            toolkit = new ServiceToolkit(User);
            Worker = new Thread(DoWork);
            Worker.Start();
        }
        catch (Exception e)
        {
            EventLog.WriteEntry(e.GetType().Name + ": " + e.Message, EventLogEntryType.Error);
            throw;
        }
    }

I compiled it and tried to start it from services.msc. Although the error message is still the same, I would expect that the service at least logs that it has been started and which error has been thrown. But nada. I cleared the the Event Viewer's "Application" protocol before starting the service, and the few logs that have been added in the meantime are not from my service.

What's going on here?


Solution

  • If all event Sources are known at the service installation time, I suggest you to register those sources ahead of time, then you be able to get log entries. In such cases you can create your own simple logger which will be flexible to give logs you specify.

    here is my logger I use with my services

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.IO;
    
        namespace TestWindowService
        {
            public static class MyLogger
            {
                public static void WriteErrorLog(Exception ex)
                {
                    StreamWriter sw = null;
                    try
                    {
                        sw = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "\\LogFile.txt", true);
                        sw.WriteLine(DateTime.Now.ToString() + ": " + ex.Source.ToString().Trim() + "; " + ex.Message.ToString().Trim());
                        sw.Flush();
                        sw.Close();
                    }
                    catch
                    {
                    }
                }
    
                public static void WriteErrorLog(string Message)
                {
                    StreamWriter sw = null;
                    try
                    {
                        sw = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "\\LogFile.txt", true);
                        sw.WriteLine(DateTime.Now.ToString() + ": " + Message);
                        sw.Flush();
                        sw.Close();
                    }
                    catch
                    {
                    }
                }
            }
        }
    

    and I use it simply

    MyLogger.WriteErrorLog("Test window service started!");
    

    Simply write it when service is stopped. You can identify the error/reason.