C#读取Windows日志

测试环境:.Net Framework 2.0、Windows Server 2003 SP2、Visual Studio 2005 SP1

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

using System.Diagnostics;

public partial class Default3 : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    {
        
//Windows日志有:"Application"应用程序, "Security"安全, "System"系统
        string[] logs = new string[] { "Application""System" };

        StringBuilder result 
= new StringBuilder();

        
foreach (string log in logs)
        {
            EventLog myLog 
= new EventLog();
            myLog.Log 
= log;
            
//myLog.MachineName = "rondi-agt0qf9op";
            foreach (EventLogEntry entry in myLog.Entries)
            {
                
//EventLogEntryType枚举包括:
                
//Error 错误事件。
                
//FailureAudit 失败审核事件。
                
//Information 信息事件。
                
//SuccessAudit 成功审核事件。
                
//Warning 警告事件。
                if (entry.EntryType == EventLogEntryType.Error || entry.EntryType == EventLogEntryType.Warning)
                {
                    result.Append(
"<font color='red'>" + log);
                    result.Append(entry.EntryType.ToString() 
+ "</font>");
                    result.Append(
"<font color='blue'>(" + entry.TimeWritten.ToString() + ")</font>:");
                    result.Append(entry.Message 
+ "<br /><br />");
                }
            }
        }
        Response.Write(result.ToString());
    }
}
输出结果:

ApplicationWarning(2007-6-16 16:25:49):Event code: 3005 Event message: 发生了未处理的异常。 Event time: 2007-6-16 16:25:49 Event time (UTC)

SystemWarning(2007-6-16 10:34:15):浏览器服务不能从在网络 \Device\NetBT_Tcpip_{5AC78E63-D3B3-4246-8FF7-D8D2B41B3620} 上的主浏览器 \\QIANTING01 上检索服务器列表。 主浏览器: \\QIANTING01 网络: \Device\NetBT_Tcpip_{5AC78E63-D3B3-4246-8FF7-D8D2B41B3620} 此事件可能是由于暂时丢失网络连接造成的。如果此消息再次出现,请确认服务器仍然与网络连接。返回码在数据文本框中。

SystemError(2007-6-16 10:34:45):浏览器服务已很多次无法在 \Device\NetBT_Tcpip_{5AC78E63-D3B3-4246-8FF7-D8D2B41B3620} 传输上捕获备份列表。备份浏览器已经停止。

SystemError(2007-6-16 15:40:30):浏览器服务已很多次无法在 \Device\NetBT_Tcpip_{5AC78E63-D3B3-4246-8FF7-D8D2B41B3620} 传输上捕获备份列表。备份浏览器已经停止。

posted on 2007-07-27 18:24  吴剑  阅读(3803)  评论(0编辑  收藏  举报

导航