gpt4 book ai didi

c# - 为什么我的 Windows 服务日志没有出现在应用程序事件日志中?

转载 作者:太空宇宙 更新时间:2023-11-03 19:37:02 45 4
gpt4 key购买 nike

我使用 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。

最佳答案

如果你想让它记录到默认日志使用

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

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

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

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

关于c# - 为什么我的 Windows 服务日志没有出现在应用程序事件日志中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/885608/

45 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com