![](/img/trans.png)
[英]How can I listen for events in the Microsoft-Windows-NetworkProfile/Operational log?
[英]How to read Windows events from Microsoft-Windows-Ntfs/Operational
我想知道是否有人可以幫助解決這個問題。
我知道如何使用 c# .net core 6.0 讀取應用程序、安全性、系統等下的 Windows 事件。 但我無法在“應用程序和服務日志/Microsoft/Windows/Ntfs/Operational”下讀取任何 Windows 事件。 這是我使用的代碼
var log = new EventLog("Application"); // This works fine
foreach( var entry in log.Entries)
{
// Do something with the event
System.Console.WriteLine("\tEntry: " + entry.ToString());
}
var log2 = new EventLog("Microsoft-Windows-Ntfs/Operational"); // This does not work
foreach( var entry in log2.Entries)
{
// Do something with the event
System.Console.WriteLine("\tEntry: " + entry.ToString());
}
我可以使用這個 powershell 命令查詢事件:Get-WinEvent -LogName "Microsoft-Windows-Ntfs/Operational" 基於這個線程 - https://serverfault.com/questions/1067287/get-eventlog-log-microsoft-windows -ntfs-operational-fails-with-does-not-exis ,但我需要一種在 c# 代碼中讀取它們的方法。
謝謝。
彼得
System.Diagnostics.EventLog
類可以追溯到 2001 年的 .NET Framework 1.x,並且只能讀取 Windows 2000、Windows XP 和 Windows Server 2003 使用的舊式 Windows 事件日志。
Application
、 Security
性和System
事件日志。Windows Vista 引入了一個功能更強大的新事件日志系統(例如,它可以使用過時且受限制的 XPath 1.0 子集直接查詢事件)和其他功能。 * (然而,基於 WinForms 的事件查看器 MMC 管理單元仍然非常無法用於重要任務,並且自從 Vista 出現以來它還沒有得到修復, le sigh )
要訪問 Vista 后的事件日志,您需要使用System.Diagnostics.Eventing.Reader.EventLogReader
而不是System.Diagnostics.EventLog
。
像這樣的東西在 LinqPad 中對我有用(使用 .NET 6):
EventLogRecord
是IDisposable
我認為將每個EventLogRecord
的標量數據值(枚舉、字符串等)復制到自定義對象中並立即處置每個EventLogRecord
更為謹慎。using System;
using System.Diagnostics.Eventing.Reader;
public record class EventLogRecordData(
String LogPath,
DateTime Created,
String Level,
String Description
)
{
public String DescriptionTruncated => this.Description.Length >= 50 ? ( this.Description.Substring( 0, 50 ) + "..." ) : this.Description;
}
public static IEnumerable<EventLogRecordData> GetEventLogRecords( String logPath )
{
// First, ensure the log exists:
try
{
EventLogSession.GlobalSession.GetLogInformation( logName: logPath, PathType.LogName ).Dump();
}
catch( EventLogNotFoundException )
{
yield break;
}
//
using( EventLogReader rdr = new EventLogReader( path: logPath ) )
{
for( EventRecord? e = rdr.ReadEvent(); e != null; e = rdr.ReadEvent() )
{
try
{
EventLogRecord rec = (EventLogRecord)e;
yield return new EventLogRecordData(
LogPath : logPath,
Created : rec.TimeCreated ?? default,
Level : rec.LevelDisplayName,
Description: rec.FormatDescription()
);
}
finally
{
e.Dispose();
}
}
}
}
EventLogReader.ReadEvent()
方法在讀取最后一條記錄后返回null
, 但這目前沒有記錄- 事實上,該方法根本不會返回null
。像這樣使用:
public static void Main()
{
foreach( EventLogRecordData e in GetEventLogRecords( "Microsoft-Windows-Ntfs/Operational" ) )
{
Console.WriteLine( "[{0:yyyy-MM-dd HH:mm:ss}] {1} - {2}", e.Created, e.Level, e.DescriptionTruncated );
}
}
...給我這個輸出(顯示的前 2 行):
[2022-04-14 10:00:42] Information - IO latency summary common data for volume...
[2022-04-14 10:00:44] Information - IO latency summary common data for volume...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.