现在的位置: 首页 > 综合 > 正文

C#读取Windows日志

2012年10月04日 ⁄ 综合 ⁄ 共 2855字 ⁄ 字号 评论关闭

可以再global中记录日志到数据库中
   //记录ASP.NET站点中未处理的异常
        protected void Application_Error(object sender, EventArgs e)
        {
            Exception ex = Server.GetLastError().GetBaseException();
            LogEntry log = new LogEntry();
            log.Categories.Add("General");
            log.Title = "未处理的异常";
            log.EventId = 1;
            log.Message = ex.Message +
                            "\r\nSOURCE: " + ex.Source +
                            "\r\nFORM: " + Request.Form.ToString() +
                            "\r\nQUERYSTRING: " + Request.QueryString.ToString() +
                            "\r\nTARGETSITE: " + ex.TargetSite +
                            "\r\nSTACKTRACE: " + ex.StackTrace;
            Logger.Write(log);
        }

测试环境:.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} 传输上捕获备份列表。备份浏览器已经停止。

抱歉!评论已关闭.