c# - 订阅 Windows 事件日志?

标签 c# event-log event-viewer

我正在开展一个项目,需要经常检查 Windows 事件日志中的某些事件。我想知道 - 有没有办法为某些事件创建对 Windows 事件日志的订阅?

那么,当事件发生时(例如事件 id = 00001),我可以在代码中收到通知吗?

如果这做不到,那么我将不得不继续搜索事件日志,这样效率不高。

最佳答案

当您使用 C# 时,我认为您应该使用 Windows API 来订阅某些 Windows 事件。您可以使用 EventLogWatcherEventLog 类来完成此操作。您可以在 MSDN 上找到使用 EventLog 创建 Windows 事件日志订阅的示例。 .

如果您更喜欢EventLogWatcher,请引用其有限的documentation .

这是我的例子:

public static void subscribe()
{
    EventLogWatcher watcher = null;
    try
    {
        EventLogQuery subscriptionQuery = new EventLogQuery(
            "Security", PathType.LogName, "*[System/EventID=4624]");

        watcher = new EventLogWatcher(subscriptionQuery);

        // Make the watcher listen to the EventRecordWritten
        // events.  When this event happens, the callback method
        // (EventLogEventRead) is called.
        watcher.EventRecordWritten +=
            new EventHandler<EventRecordWrittenEventArgs>(
                EventLogEventRead);

        // Activate the subscription
        watcher.Enabled = true;

        for (int i = 0; i < 5; i++)
        {
            // Wait for events to occur. 
            System.Threading.Thread.Sleep(10000);
        }
    }
    catch (EventLogReadingException e)
    {
        Log("Error reading the log: {0}", e.Message);
    }
    finally
    {
        // Stop listening to events
        watcher.Enabled = false;

        if (watcher != null)
        {
            watcher.Dispose();
        }
    }
    Console.ReadKey();
}

// Callback method that gets executed when an event is
// reported to the subscription.
public static void EventLogEventRead(object obj,
    EventRecordWrittenEventArgs arg)
{
    // Make sure there was no error reading the event.
    if (arg.EventRecord != null)
    {
        //////
        // This section creates a list of XPath reference strings to select
        // the properties that we want to display
        // In this example, we will extract the User, TimeCreated, EventID and EventRecordID
        //////
        // Array of strings containing XPath references
        String[] xPathRefs = new String[9];
        xPathRefs[0] = "Event/System/TimeCreated/@SystemTime";
        xPathRefs[1] = "Event/System/Computer";
        xPathRefs[2] = "Event/EventData/Data[@Name=\"TargetUserName\"]";
        xPathRefs[3] = "Event/EventData/Data[@Name=\"TargetDomainName\"]";

        // Place those strings in an IEnumerable object
        IEnumerable<String> xPathEnum = xPathRefs;

        // Create the property selection context using the XPath reference
        EventLogPropertySelector logPropertyContext = new EventLogPropertySelector(xPathEnum);

        IList<object> logEventProps = ((EventLogRecord)arg.EventRecord).GetPropertyValues(logPropertyContext);
        Log("Time: ", logEventProps[0]);
        Log("Computer: ", logEventProps[1]);
        Log("TargetUserName: ", logEventProps[2]);
        Log("TargetDomainName: ", logEventProps[3]);
        Log("---------------------------------------");

        Log("Description: ", arg.EventRecord.FormatDescription());
    }
    else
    {
        Log("The event instance was null.");
    }
}

关于c# - 订阅 Windows 事件日志?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33510244/

相关文章:

c# - 错误的 .wav 格式录制音频 NAudio 库

c# - 以编程方式将图像附加到椭圆

xpath - Windows 事件日志,您可以对不相等的字符串进行 xpath 过滤吗?

c# - c#中的引用类型和值类型有什么区别?

windows - Powershell - 尾 Windows 事件日志?可能吗?

wmi - 什么是 win32_NTLogEvent 时间生成格式

open-source - 是否有任何开源 Windows 事件日志分析器应用程序?

c# - Windows 事件查看器中未显示新的 Windows 日志

c# - 事件源与事件提供者

c# - 使 Enter 键像提交按钮一样