簡體   English   中英

事件日志(事件日志創建事件)

[英]Event Log(Event log created event)

我想知道在創建系統事件日志時如何在C#中捕獲事件。 我也想知道我如何才能使用C# 從Windows事件日志中僅獲取錯誤日志 我有以下代碼,但它只返回所有日志。 我只需要錯誤日志:

System.Diagnostics.EventLog eventLog1 = new System.Diagnostics.EventLog("Application", Environment.MachineName);

            int i = 0;
            foreach (System.Diagnostics.EventLogEntry entry in eventLog1.Entries)
            {
                Label1.Text += "Log is : " + entry.Message + Environment.NewLine;

            }

您可以使用EventLog類的CreateEventSource靜態方法創建事件日志,例如

EventLog.CreateEventSource("MyApp","Application");

System.Diagnostics命名空間中存在EventLog類。

您可以使用WriteEntry()方法寫入事件日志。 EventLogEntryType枚舉可用於指定要記錄的事件的類型。 下面的例子

EventLog.WriteEntry(sSource, sEvent, EventLogEntryType.Warning,  234);

請參閱如何使用Visual C#寫入事件日志

如果您只想讀取ERROR級別的日志,則可以使用以下代碼塊。 您只需要檢查EntryType條目的EntryType ,然后相應地打印/顯示即可。

    static void Main(string[] args)
    {
        EventLog el = new EventLog("Application", "MY-PC");
        foreach (EventLogEntry entry in el.Entries)
        {
            if (entry.EntryType == EventLogEntryType.Error)
            {
                Console.WriteLine(entry.Message);
            }
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM