1

我有一个 Windows 服务。在我为其添加代码以开始记录之前,它工作正常。现在,当我尝试启动服务时,我收到以下错误:

本地计算机上的 GBBService 服务启动然后停止。一些服务如果没有工作会自动停止,例如性能日志和警报服务

这是我的服务的代码: - 从项目安装程序内部

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();
        }
    }
}

在我的服务范围内:

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

    }

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

我的代码做错了吗?

4

1 回答 1

2

我认为问题在于您正在尝试写入EventLog不存在的内容。

ServiceInstaller您创建EventLog My GBBServiceLog

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

但是在Service你尝试写给My GBB Service Log

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

我认为应该是:

public GBBService()
{
    InitializeComponent();
    EventLog.Source = eventSource;
    EventLog.Log = "My GBBServiceLog";
}
于 2013-03-25T03:49:56.317 回答