查看事件日志远程使用.NET [英] Read Event Log Remotely with .NET

查看:241
本文介绍了查看事件日志远程使用.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读的事件日志在远程计算机上的测试过程中,以检查是否有错误。下面是一些相关的code:

I want to read the Event Log on a remote computer to check for errors during testing. Here's some relevant code:

public bool CheckEventLogs(DateTime start)
{
  EventLog myEventLog = new EventLog("CustomLog", "ServerName");
  bool errorFound = false;
  foreach (EventLogEntry entry in myEventLog.Entries)
  {
    if (entry.EntryType == EventLogEntryType.Error && entry.TimeGenerated >= start)
    {
       Console.WriteLine("Error in Event Log:\n" + entry.Message + "\n");
       errorFound = true;
    }
  }
  return errorFound;
}

目前,该code抛出一个异常(试图执行未经授权的操作)。据 MSDN ,我需要EventLogPermission,但我一直在努力寻找如何将任何实例使用此权限。有没有人对如何做到这一点的例子吗?

Currently, this code throws an exception (Attempted to perform an unauthorized operation). According to MSDN, I need EventLogPermission, but I have been struggling to find any examples of how to use this permission. Does anyone have an example of how to do this?

编辑:回应评论

感谢大家的评论 - 这里是要求的补充资料:

Thank you all for the comments - here is the additional information requested:

唯一的例外是从的foreach 语句抛出。具体来说,通过code步进,当它扔进了一步,当被突出显示后。看来,我是能够创造事件日志对象,但我不能访问该条目在事件日志中。

The exception is thrown from the foreach statement. Specifically, when stepping through the code it thrown in the step after when in is highlighted. It seems that I was able to create the event log object but I'm not able to access the entries in the event log.

我的帐户没有权限读取目标系统上的事件日志,但我有这确实的帐户凭据。当手动通过事件查看器连接有一个选项,以作为连接其他用户。手动操作后,那么我的code运行没有任何问题。但是,我不能靠手工每到这个程序运行的时间这样做。我需要的是一种连接作为另一个用户programmaticly。我认为 EventLogPermission 将是这样做的,但也许还有另一种途径。如果有人知道如何连接到远程日志在C#​​中的不同的用户,这将是正是我所期待的。

My account does not have permission to read the event log on the target system, but I have credentials for an account which does. When connecting manually through the event viewer there is an option to connect as another user. After doing this manually, then my code ran without a problem. However, I cannot rely doing it manually every time this program runs. What I need is a way to connect as another user programmaticly. I thought that the EventLogPermission would be the way to do that, but maybe there is another way. If anyone knows how to connect to a remote log as a different user in C#, that would be exactly what I was looking for.

推荐答案

感谢大家谁提供了在这个问题上的意见。当我意识到的权限可能不是.NET Windows和事件查看器本身,而是部分的一部分,我有一些新的方向,我自己的调查。

Thanks to everyone who provided comments on this question. Once I realized that the permissions might not be a part of .NET but part of Windows and the Event Viewer itself, I had some new direction for my own investigations.

它看起来像一个NET USE命令是所有需要建立我的本地计算机和远程计算机之间的连接。当调用NET USE使用code我张贴的问题之前,事情的来龙去脉精美。这是很简单的调用从code从事件日志读取之前。

It looks like a "net use" command was all that was needed to establish the connection between my local computer and the remote computer. When calling "net use" before using the code I posted in the question, things worked beautifully. It is simple enough to call that from the code before reading from the event log.

再次感谢您的帮助!

这篇关于查看事件日志远程使用.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆