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

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

问题描述

我想在远程计算机上读取事件日志以检查测试期间的错误.这是一些相关的代码:

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:
" + entry.Message + "
");
       errorFound = true;
    }
  }
  return errorFound;
}

当前,此代码引发异常(尝试执行未经授权的操作).根据 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 语句抛出的.具体来说,当单步执行代码时,它在 in 突出显示后的步骤中抛出.我似乎能够创建事件日志对象,但我无法访问事件日志中的条目.

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.

我的帐户无权读取目标系统上的事件日志,但我有一个可以读取的帐户凭据.通过事件查看器手动连接时,可以选择以其他用户身份连接.手动执行此操作后,我的代码运行没有问题.但是,每次运行该程序时,我都不能依赖手动执行此操作.我需要的是一种以编程方式连接为另一个用户的方法.我认为 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"时,事情运行得很好.在从事件日志读取之前从代码中调用它很简单.

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天全站免登陆