【问题标题】:Service on local computer started then stopped error本地计算机上的服务已启动然后停止错误
【发布时间】:2013-03-25 03:36:14
【问题描述】:

我有一个 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");
    }

我的代码做错了吗?

【问题讨论】:

  • 是不是因为您在安装程序和服务中创建了事件日志My GBBServiceLog,所以您想写信给My GBB Service Log
  • 所以两个地方都需要相同吗?这是我在处理它时引用的 url:blog.embrodesign.com/2012/05/event-log-from-windows-service 不确定日志名称是相同还是不同
  • 我很确定您需要相同的名称,My GBB Service Log 不存在,因此您需要在服务中创建它或使用您在安装程序中创建的名称。
  • @sa_ddam213 就是这样,请将其添加为答案,以便我可以关闭它!
  • 没问题 :),很高兴你成功了 :)

标签: c# logging windows-services event-log custom-eventlog


【解决方案1】:

我认为问题在于您正在尝试写一个不存在的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";
}

【讨论】:

    猜你喜欢
    • 2012-08-25
    • 1970-01-01
    • 2016-10-21
    • 2016-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多