【发布时间】:2013-03-21 15:45:45
【问题描述】:
我正在尝试将一些消息写入 Windows 事件日志。
调用“SourceExists()”函数时会抛出(安全)异常。
private bool CheckIfEventLogSourceExits()
{
try
{
if (!EventLog.SourceExists(this.BaseEventLog))
{
return false;
}
return true;
}
catch (System.Security.SecurityException)
{
return false;
}
}
此问题的所有答案都在说明如何手动解决问题。 喜欢这里:Stackoverflow Thread。解决方案是更改一些注册表项
但您不能指望使用您的应用程序的每个人都知道这些变化。
所以我的问题是,我们如何以编程方式解决这个问题?
在我的代码下面:
try
{
string sLog = "Application";
if (CheckIfEventLogSourceExits())
{
EventLog.CreateEventSource(this.BaseEventLog, sLog);
}
EventLog.WriteEntry(this.BaseEventLog, message, eventLogEntryType);
}
catch (Exception ex)
{
ex.Source = "WriteToEventLog";
throw ex;
}
【问题讨论】:
标签: event-log