我正在尝试将一些消息写入 Windows 事件日志。
调用函数“SourceExists()”时将抛出(安全)异常。
private bool CheckIfEventLogSourceExits()
    {
        try
        {
            if (!EventLog.SourceExists(this.BaseEventLog))
            {
                return false;
            }
            return true;
        }
        catch (System.Security.SecurityException)
        {
            return false;
        }
    }
这个问题的所有答案都在解释如何手动解决问题。像这里:Stackoverflow 线程。解决方案是更改一些注册表项
但是您不能期望使用您的应用程序的每个人都知道这些更改。
所以我的问题是,我们如何以编程方式解决这个问题?
在我的代码下面:
 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;
            }