【问题标题】:Why does my Windows Service logs not show up in the Application Event Log?为什么我的 Windows 服务日志没有显示在应用程序事件日志中?
【发布时间】:2009-05-19 23:55:07
【问题描述】:

我使用 Windows 服务项目模板创建了一个运行 C# Windows 服务。我已经能够正确安装它,并且可以毫无问题地启动和停止它。

但是,当我去我的事件查看器查看它的启动和停止日志时,我什么也得不到。

这是我正在测试的示例代码:

public MyService()
    {
        InitializeComponent();

        ServiceName = "My Data Service";
        EventLog.Log = "Application";
    }

    protected override void OnStart(string[] args)
    {
        EventLog.WriteEntry("Starting My Data Service");
    }

    protected override void OnStop()
    {
        EventLog.WriteEntry("Ending MyData Service");
    }

另外,我的操作系统是 Windows Vista。

【问题讨论】:

    标签: c# windows-services


    【解决方案1】:

    如果您希望它记录到默认日志,请使用

    EventLog.WriteEntry("Starting My Data Service", EventLogEntryType.Information);
    

    当然,您必须确保服务在具有足够权限的帐户下运行,以便写入日志并“作为服务运行”。

    在 SO 上找到了这个例子,Best Way to write to the event log

    这是一个您也指定源的示例,而不是显示为 .NET 运行时...MSDN Example

    【讨论】:

      【解决方案2】:

      EventLog 需要创建一个 EventSource 才能写入。

      【讨论】:

        猜你喜欢
        • 2011-11-24
        • 1970-01-01
        • 2013-08-10
        • 2022-01-20
        • 2021-09-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多