【问题标题】:In .net what are the difference between Eventlog and ManagementObject for retriving logs from remote server?在 .net 中,Eventlog 和 ManagementObject 用于从远程服务器检索日志有什么区别?
【发布时间】:2023-03-24 09:31:01
【问题描述】:

我发现了以下两种从远程服务器获取应用程序事件日志条目的方法。

1.使用 EventLog 对象

 string logType = "Application";
 EventLog ev = new EventLog(logType,"rspl200");

 EventLogEntryCollection evColl =  ev.Entries

2。使用 ManagementObjectSearcher 对象

ConnectionOptions co = new ConnectionOptions(); co.Username = "testA"; co.Password = "testA"; ManagementScope 范围 = new ManagementScope(@"\" + "machineName"+ @"\root\cimv2", co); scope.Connect();

SelectQuery query = new SelectQuery(@"select * from Win32_NtLogEvent"); EnumerationOptions opt = new EnumerationOptions(); opt.BlockSize = 1000;

using (ManagementObjectSearcher searcher = new  ManagementObjectSearcher(scope, query,opt))
   {

     foreach (ManagementObject mo in searcher.Get())
                {

                       // write down log entries
                       Console.Writeline(mo["EventCode"]);

                }

   }

我可以使用方法 #1(使用 EventLog 对象)轻松获取远程事件日志,而不会出现任何安全访问被拒绝异常。但是使用方法#2(使用 ManagementObjectSearcher 对象)我得到拒绝访问异常。

实际上我希望远程事件日志(仅应用程序和最新日志而不是所有应用程序日志)显示在树形视图中,如下所示

 -  ServerName
   - Logs
     + Error
     + Information
     + Warning

任何人都可以帮助我从这个或任何其他方面找出最好的方法吗?

Also the main thing is that user who reads remote logs may be in different domain than server.

谢谢 米特什·帕特尔

【问题讨论】:

    标签: .net event-log


    【解决方案1】:

    我看到这是一篇旧帖子...

    我认为您对#2 的唯一问题是您的范围应该是:

    ManagementScope scope = new ManagementScope(@"\\" + "machineName"+ @"\root\cimv2", co);
    

    您缺少双反斜杠。

    使用方法#2 需要注意的一点是,它正在执行 wmi 查询,因此远程机器上的 wmi 主机进程将受到重击。从我看到的情况来看,它在 wmi 查询完成之前以 %45 的 CPU 使用率运行。

    我不知道方法 #1 是如何实现的,所以不能确定。

    如果您担心性能,我会远离 .Net 事件日志 API 并使用 win32 api(s):

    pre-vista/2008:http://msdn.microsoft.com/en-us/library/aa363657(v=VS.85).aspx

    vista/2008 后:http://msdn.microsoft.com/en-us/library/aa385785(v=VS.85).aspx

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-28
      • 1970-01-01
      • 2011-03-19
      • 1970-01-01
      • 2010-10-30
      相关资源
      最近更新 更多