我想知道当创建一个系统事件日志时,如何在C#中捕获事件。此外,我还想知道如何使用C#从事件日志中只获得错误日志。我有以下代码,但它只返回我所有的日志。我只需要错误日志:
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;
}发布于 2014-10-01 19:55:01
您可以使用CreateEventSource静态EventLog类方法来创建类似于
EventLog.CreateEventSource("MyApp","Application");EventLog类存在于System.Diagnostics命名空间中。
可以使用WriteEntry()方法写入事件日志。EventLogEntryType枚举可用于指定要记录的事件的类型。下面是一个例子
EventLog.WriteEntry(sSource, sEvent, EventLogEntryType.Warning, 234);请参阅如何使用VisualC#写入事件日志
如果您只希望读取ERROR级别日志,则可以使用下面的代码块。您只需检查事件日志条目的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);
}
}
}https://stackoverflow.com/questions/26149405
复制相似问题