1

I have a windows service. It was working fine until I added code for it to begin logging. Now when I try to start the service I receive the following error:

The GBBService service on local computer started then stopped. Some services stopped automatically if they have no work to do, for example, the performance logs and alert service

Here is the code for my service: - From inside project installer

public partial class ProjectInstaller : System.Configuration.Install.Installer
{
    string eventSource = "GBBServiceLog";

    public ProjectInstaller()
    {
        InitializeComponent();
        EventLogInstaller installer = FindInstaller(this.Installers);
        if (installer != null)
        {
            installer.Source = eventSource;
            installer.Log = "My GBBServiceLog";
        }
    }

    private EventLogInstaller FindInstaller(InstallerCollection installers)
    {
        foreach (Installer installer in installers)
        {
            if (installer is EventLogInstaller)
            {
                return (EventLogInstaller)installer;
            }
            EventLogInstaller eventLogInstaller = FindInstaller(installer.Installers);
            if (eventLogInstaller != null)
                return eventLogInstaller;
        }
        return null;
    }

    protected override void OnCommitted(IDictionary savedState)
    {
        base.OnCommitted(savedState);
        // Start the service after installation
        using (ServiceController sc = new ServiceController(this.serviceInstaller1.ServiceName))
        {
            sc.Start();
        }
    }
}

From within my service:

public GBBService()
    {
        InitializeComponent();
        EventLog.Source = eventSource;
        EventLog.Log = "My GBB Service Log";

    }

    protected override void OnStart(string[] args)
    {
        EventLog.WriteEntry("GBBService Service Started");
    }

Did I do something wrong in my code?

sa_ddam213
  • 42,848
  • 7
  • 101
  • 110
user1270384
  • 711
  • 7
  • 24
  • 53
  • is it becaue you created the eventlog `My GBBServiceLog` in the installer and in the Service you are tring to write to `My GBB Service Log` – sa_ddam213 Mar 25 '13 at 03:44
  • so it needs to be the same in both places? this is the url I referenced when working on it: http://blog.embrodesign.com/2012/05/event-log-from-windows-service/ wasn't sure whether the log names were to be the same or different – user1270384 Mar 25 '13 at 03:47
  • 1
    I'm pretty sure you need the same name, `My GBB Service Log` does not exist so you need to create it in the service or use the one you created in the Installer. – sa_ddam213 Mar 25 '13 at 03:52
  • @sa_ddam213 that was it please add it as an answer so I can close this! – user1270384 Mar 25 '13 at 04:02
  • no problems :), glad you got it working :) – sa_ddam213 Mar 25 '13 at 04:22

1 Answers1

2

I think the problem is you are trying to write to a EventLog that does not exist.

In the ServiceInstaller you create the EventLog My GBBServiceLog

if (installer != null)
{
    installer.Source = eventSource;
    installer.Log = "My GBBServiceLog";
}

But in the Service you try to write to My GBB Service Log

public GBBService()
{
    InitializeComponent();
    EventLog.Source = eventSource;
    EventLog.Log = "My GBB Service Log"; <--------------
}

I think it should be:

public GBBService()
{
    InitializeComponent();
    EventLog.Source = eventSource;
    EventLog.Log = "My GBBServiceLog";
}
sa_ddam213
  • 42,848
  • 7
  • 101
  • 110