C#读取Windows日志

可以再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} 传输上捕获备份列表。备份浏览器已经停止。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值