如何使用 C# 在 Windows Server 2008 中写入事件日志?

发布于 2024-09-25 11:50:45 字数 504 浏览 3 评论 0原文

我有以下一段在 Windows Server 2003 中运行良好的代码。它写入 EventViewer 中的应用程序事件日志。相同的代码在 Windows 2008 中不起作用。应用程序崩溃。请求有关如何在 Windows Server 2008 中写入事件日志的帮助。

if (!EventLog.SourceExists("MyServiceLog"))
{
    EventLog.CreateEventSource("MyServiceLog", "Application");
}
//Create an EventLog instance and assign its source.
EventLog eventLog = new EventLog();
eventLog.Source = "MyServiceLog";
//Write an informational entry to the event log.
eventLog.WriteEntry(Header + ": " + FailureReason);

I have the following piece of code that works well in Windows Server 2003. It writes to the Application Event Log in EventViewer. The same code doesn't work in Windows 2008. The application crashes. Request to help on how to write to event log in Windows Server 2008.

if (!EventLog.SourceExists("MyServiceLog"))
{
    EventLog.CreateEventSource("MyServiceLog", "Application");
}
//Create an EventLog instance and assign its source.
EventLog eventLog = new EventLog();
eventLog.Source = "MyServiceLog";
//Write an informational entry to the event log.
eventLog.WriteEntry(Header + ": " + FailureReason);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

久夏青 2024-10-02 11:50:46

您需要成为本地管理员组的成员才能创建新的事件源。源可能存在于 Server 2003 上,或者您已经拥有该操作系统所需的权限。在 Server 2008 上,即使您是管理员,默认情况下也是在没有提升权限的情况下运行。在这种情况下,您必须右键单击您的应用程序并选择“以管理员身份运行”。

You need to be member of the local Administrators group in order to create a new event source. The source probably exists on Server 2003 or you already have the required permissions on that operating system. On Server 2008 the default is to run without elevated privileges even though you are an administrator. In that case you will have to right click your application and select "Run as Administrator".

段念尘 2024-10-02 11:50:46

这与 Windows 2008 中的新权限集有关,并且您的帐户没有创建新事件日志源的特权访问权限。

当您为应用程序创建安装程序时,最好是创建这些事件日志源,因为通常您必须以特权运行安装程序。

It has to do with the new permission sets in Windows 2008 and your account doesn't have the privileged access to create new event log sources.

When you create an installer for your application, best will be to create those event log sources then, because normally you have to run the installers with privileged rights.

飞烟轻若梦 2024-10-02 11:50:46

关于“应用程序崩溃” - 这不应该在托管环境中发生。也许在这种情况下它与权限相关,但是除非添加逻辑来处理错误(即异常),否则您将永远处于黑暗中并重新启动应用程序。

将其更改为

try 
{ 
    /* put your event log code here */ 
} 
catch (Exception e) 
{ 
    /* new code to gracefully handle errors */ 
}

并查看 Exception 类和字段(例如 e.Messagee.StrackTrace),您将准确找出错误所在以及发生位置。

re 'the application crashes' - this should not happen in managed environment. Maybe in this case it's permissions-related, but you will be forever in the dark and restarting your app unless you add logic to handle errors (i.e. exceptions).

Change this to

try 
{ 
    /* put your event log code here */ 
} 
catch (Exception e) 
{ 
    /* new code to gracefully handle errors */ 
}

and look at the Exception class and fields (such as e.Message, e.StrackTrace) that you are getting to work out exactly what's wrong and where it happened.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文