简体   繁体   English

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

[英]c# Reading event log of remote server

I'm following this post and trying to run the code (copied below), and I'm having a slightly different problem. 我正在关注这篇文章,并尝试运行代码(复制在下面),而我遇到了一个稍微不同的问题。 I can remote connect to the server and view the event log through the Event Viewer program, but I can't iterate through the events in the code. 我可以远程连接到服务器并通过Event Viewer程序查看事件日志,但无法遍历代码中的事件。 I get an InvalidOperationException saying "Cannot open log EventLogName on machine . Windows has not provided an error code." 我收到一个InvalidOperationException消息,说“无法在计算机上打开日志EventLogName。Windows未提供错误代码。” There is also an inner exception of type System.ComponentModel.Win32Exception that says "Access is denied." 还存在类型为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;
}

Any ideas? 有任何想法吗?

EDIT: 编辑:

The exception data is as follows. 异常数据如下。 I can't post the server name as it is company information. 我无法发布服务器名称,因为它是公司信息。 I receive the error when trying to read the event log. 尝试读取事件日志时收到错误消息。 I am absolutely sure I can read the log because I can remote connect with my account and read the log using the Event Viewer. 我绝对确定我可以读取日志,因为我可以远程连接我的帐户并使用事件查看器读取日志。

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: 

This is most likely a policy permission issue. 这很可能是策略许可问题。 See the Giving Non Administrators permission to read Event Logs Windows 2003 and Windows 2008 blog entry on TechNet. 请参阅授予非管理员权限,以阅读 TechNet上的Windows 2003和Windows 2008博客事件日志 I was able to make your code work once I did this. 完成此操作后,我就能使您的代码正常工作。 In the case of Windows 2008 Server and higher, it's simply a matter of adding the user to the Event Log Readers local security group. 对于Windows 2008 Server和更高版本,只需将用户添加到“事件日志读取器”本地安全组即可。 Until I did that I was getting the same error. 在我这样做之前,我一直遇到相同的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM