c#读取远程服务器的事件日志

c#读取远程服务器的事件日志,c#,permissions,event-log,C#,Permissions,Event Log,我正在关注这篇文章并试图运行代码(复制如下),但我遇到了一个稍微不同的问题。我可以远程连接到服务器,并通过事件查看器程序查看事件日志,但我不能遍历代码中的事件。我收到一个InvalidOperationException,它说“无法在计算机上打开日志EventLogName。Windows没有提供错误代码。”还有一个System.ComponentModel.Win32Exception类型的内部异常,它说“访问被拒绝” 有什么想法吗 编辑: 异常数据如下所示。我无法发布服务器名称,因为它是公司

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

有什么想法吗

编辑:

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

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: 
System.InvalidOperationException未处理
Message=无法在计算机服务器.domain.net上打开日志EventLogName。Windows未提供错误代码。
来源=系统
堆栈跟踪:
位于System.Diagnostics.EventLoginInternal.OpenForRead(字符串currentMachineName)
位于System.Diagnostics.EventLoginInternal.GetEntryAtNoThrow(Int32索引)
在System.Diagnostics.EventLogEntryCollection.EntriesEnumerator.MoveNext()中
在System.Linq.Enumerable.d_uB1`1.MoveNext()中
位于System.Linq.Enumerable.WhereEnumerableInterator`1.MoveNext()
位于System.Collections.Generic.List`1.InsertRange(Int32索引,IEnumerable`1集合)
位于System.Collections.Generic.List`1.AddRange(IEnumerable`1集合)
在c:\users\me\documents\visualstudio 2010\Projects\MyApp\MyApp\Program.cs中的MyApp.Program.GetEventLogData(字符串machineName,DateTime start)中:第45行
在c:\users\me\documents\visualstudio 2010\Projects\MyApp\MyApp\Program.cs中的MyApp.Program.Main(字符串[]args)处:第28行
位于System.AppDomain.\u nExecuteAssembly(RuntimeAssembly程序集,字符串[]args)
位于System.AppDomain.ExecuteAssembly(字符串汇编文件、证据汇编安全性、字符串[]args)
在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()上
位于System.Threading.ThreadHelper.ThreadStart\u上下文(对象状态)
在System.Threading.ExecutionContext.Run(ExecutionContext ExecutionContext,ContextCallback回调,对象状态,布尔ignoreSyncCtx)
在System.Threading.ExecutionContext.Run(ExecutionContext ExecutionContext,ContextCallback回调,对象状态)
位于System.Threading.ThreadHelper.ThreadStart()处
InnerException:System.ComponentModel.Win32Exception
消息=访问被拒绝
错误代码=-2147467259
NativeErrorCode=5
内部异常:

这很可能是策略权限问题。请参阅TechNet上的博客条目。一旦我这么做了,我就能让你的代码正常工作。对于Windows 2008 Server及更高版本,只需将用户添加到事件日志读取器本地安全组即可。在我这样做之前,我也遇到了同样的错误。

这可能与您在远程计算机上运行,并且没有管理员帐户或访问权限来查看/打开事件日志有关吗。。?您是否成功连接到远程服务器。。?您需要在服务器名称前面加@“\\”吗。。?这是否需要用户名、密码、域..?如果要使用win32API,请签出此站点,并检查错误代码属性。通过在web上查找该数字的十六进制表示形式,您可能可以获得一些其他信息。@JamieSee,正如我前面提到的,错误状态为没有错误代码。@DJKRAZE,我可以很好地连接到机器(通过远程连接和代码)。问题是通过代码读取日志。我们已经为WCF服务和监控应用程序设置了用户。我可能会与我们的网络团队合作,让我的程序在其中一种情况下运行。
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: