如何使用C#在Windows Server 2008中写入事件日志?
我有以下一段代码,在Windows Server 2003中运行良好。它在EventViewer中写入应用程序事件日志。相同的代码在Windows 2008中不起作用。应用程序崩溃。请求帮助如何写入事件日志在Windows Server 2008中如何使用C#在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);
您必须是本地Administrators组的成员,以创建一个新的事件源。源可能存在于Server 2003上,或者您已在该操作系统上拥有所需的权限。在Server 2008上,默认情况下即使您是管理员也不会提升权限。在这种情况下,您必须右键单击您的应用程序并选择“以管理员身份运行”。
重新'应用程序崩溃' - 这不应该发生在托管环境中。也许在这种情况下,它与权限相关,但除非您添加逻辑来处理错误(即例外),否则您将永远处于黑暗之中并重新启动应用程序。
更改为
try
{
/* put your event log code here */
}
catch (Exception e)
{
/* new code to gracefully handle errors */
}
,并期待在Exception类和字段(如e.Message
,e.StrackTrace
),你得到工作了什么问题以及哪里它发生。
'except' ='catch'? – 2012-01-10 20:06:58
@Kieren - 是的,谢谢 – 2012-01-12 14:36:55
它与Windows 2008中的新权限集有关,您的帐户没有创建新事件日志源的特权访问权限。
当您为您的应用程序创建安装程序时,最好是创建这些事件日志源,因为通常您必须运行具有特权的安装程序。
感谢您的信息。 – Defendore 2010-10-04 08:37:36
+1击败我我的1分钟 – 2010-10-02 12:23:59
谢谢我以管理员模式运行我的应用程序。现在它工作正常。 – Defendore 2010-10-04 08:35:34
那么,如果我的控制台应用程序旨在作为Windows服务运行?如何在启动时提升服务的权限? – Maltrap 2012-11-30 00:22:22