【问题标题】:c# Reading event log of remote serverc#读取远程服务器的事件日志
【发布时间】:2012-07-19 17:24:46
【问题描述】:

我正在关注this 的帖子并尝试运行代码(复制如下),但我遇到了一个稍微不同的问题。我可以远程连接到服务器并通过事件查看器程序查看事件日志,但我无法遍历代码中的事件。我收到一个 InvalidOperationException 说“无法在机器上打开日志 EventLogName。Windows 没有提供错误代码。”还有一个 System.ComponentModel.Win32Exception 类型的内部异常,显示“访问被拒绝”。

private static bool GetEventLogData(DateTime start)
{
    var log = new EventLog("EventLogName", "SERVER.domain.net");
    bool errorFound = false;
    foreach (EventLogEntry entry in log.Entries)
    {
        if ((entry.EntryType == EventLogEntryType.Error) && 
            (entry.TimeGenerated >= start))
        {
            Console.WriteLine("Error in Event Log:\n" + entry.Message + "\n");
            errorFound = true;
        }
    }
    return errorFound;
}

有什么想法吗?

编辑:

异常数据如下。我无法发布服务器名称,因为它是公司信息。尝试读取事件日志时收到错误消息。我绝对确定我可以阅读日志,因为我可以远程连接我的帐户并使用事件查看器阅读日志。

System.InvalidOperationException was unhandled
  Message=Cannot open log EventLogName on machine SERVER.domain.net. Windows has not provided an error code.
  Source=System
  StackTrace:
       at System.Diagnostics.EventLogInternal.OpenForRead(String currentMachineName)
       at System.Diagnostics.EventLogInternal.GetEntryAtNoThrow(Int32 index)
       at System.Diagnostics.EventLogEntryCollection.EntriesEnumerator.MoveNext()
       at System.Linq.Enumerable.<CastIterator>d__b1`1.MoveNext()
       at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
       at System.Collections.Generic.List`1.InsertRange(Int32 index, IEnumerable`1 collection)
       at System.Collections.Generic.List`1.AddRange(IEnumerable`1 collection)
       at MyApp.Program.GetEventLogData(String machineName, DateTime start) in c:\users\me\documents\visual studio 2010\Projects\MyApp\MyApp\Program.cs:line 45
       at MyApp.Program.Main(String[] args) in c:\users\me\documents\visual studio 2010\Projects\MyApp\MyApp\Program.cs:line 28
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: System.ComponentModel.Win32Exception
       Message=Access is denied
       ErrorCode=-2147467259
       NativeErrorCode=5
       InnerException: 

【问题讨论】:

  • 这可能与您在远程计算机上运行并且没有管理员帐户或访问查看/打开事件日志的权限有关吗?您是否成功连接到远程服务器..?您是否需要在服务器名称前加上@"\\" ..?这也需要用户名、密码、域..?
  • 如果你想使用win32APIpinvoke.net/default.aspx/advapi32.LogonUser查看这个网站
  • 检查 ErrorCode 属性。您可以通过在网络上查找该数字的十六进制表示来获得一些额外的信息。
  • @JamieSee,正如我上面提到的,错误表明没有错误代码。
  • @DJKRAZE,我可以很好地连接到机器(通过远程连接和代码)。问题是通过代码读取日志。

标签: c# permissions event-log


【解决方案1】:

这很可能是策略权限问题。请参阅 TechNet 上的 Giving Non Administrators permission to read Event Logs Windows 2003 and Windows 2008 博客条目。完成此操作后,我就可以使您的代码正常工作。对于 Windows 2008 Server 及更高版本,只需将用户添加到 Event Log Readers 本地安全组即可。在我这样做之前,我遇到了同样的错误。

【讨论】:

  • 我们已经为 WCF 服务和监控应用程序设置了用户。我可能可以与我们的网络团队合作,让我的程序在其中之一下运行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-10
  • 1970-01-01
相关资源
最近更新 更多