【问题标题】:Write to Event Log - The source was not found, but some or all event logs could not be searched. Inaccessible logs: Security."写入事件日志 - 未找到源,但无法搜索部分或全部事件日志。无法访问的日志:安全性。”
【发布时间】: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


    【解决方案1】:

    我知道发布此帖子为时已晚,但我从类似的经验中找到的答案是,您运行的服务没有机器的管理权限,因此无法写入日志。

    很容易确定应用程序是否在管理员权限下运行。您可以在代码中添加类似这样的内容,并带有一个消息框,建议用户运行“admin”。

    private void GetServicePermissionLevel()
    {
    bool bAdmin = false;
    try {
        SecurityIdentifier sidAdmin = new SecurityIdentifier(WellKnownSidType.BuiltinAdministratorsSid, null);
        AppDomain myDomain = Thread.GetDomain();
        myDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
        WindowsPrincipal myPrincipal = (WindowsPrincipal)Thread.CurrentPrincipal;
        if (myPrincipal.IsInRole(sidAdmin)) {
            bAdmin = true;
        } else {
            bAdmin = false;
        }
    } catch (Exception ex) {
        throw new Exception("Error in GetServicePermissionlevel(): " + ex.Message + " - " + ex.StackTrace);
    } finally {
        _ServiceRunAsAdmin = bAdmin;
    }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-08-05
      • 2011-08-14
      • 1970-01-01
      • 1970-01-01
      • 2019-04-03
      • 2012-03-22
      • 2011-10-19
      • 2016-07-18
      相关资源
      最近更新 更多