写例外Windows的日志文件 [英] Writing Exceptions to the Windows Log File

查看:152
本文介绍了写例外Windows的日志文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想抓住我的异常,并在Windows日志文件记录它们。怎样去开,写入Windows登录

I'd like to catch my exceptions and log them in the Windows log file. How do I go about opening and writing to the Windows log?

推荐答案

您可以使用System.Diagnostics.EventLog.WriteEntry 函数写条目到事件日志。

You can use the System.Diagnostics.EventLog.WriteEntry function to write entries to the event log.

System.Diagnostics.EventLog.WriteEntry("MyEventSource", exception.StackTrace,                  
                                       System.Diagnostics.EventLogEntryType.Warning);

要读取事件日志可以使用的 System.Diagnostics.EventLog.GetEventLogs 功能。

To read event logs you can use the System.Diagnostics.EventLog.GetEventLogs function.

//here's how you get the event logs
var eventLogs = System.Diagnostics.EventLog.GetEventLogs();

foreach(var eventLog in eventLogs)
{    
    //here's how you get the event log entries
    foreach(var logEntry in eventLog.Entries)
    {
        //do something with the entry
    }    
}

这篇关于写例外Windows的日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆