Question

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?

Was it helpful?

Solution

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";
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top