![](/img/trans.png)
[英]Task Scheduler - When the event is added to the event log the created task not running
[英]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);
如果您只想讀取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.