如何将网页错误信息写入事件日志

本文发布于 16 年前,部分内容可能已经失去参考价值。

    System.Diagnostice 命名空间提供了写入Windows事件日志的类。
    一般情况下把程序代码放在 Global.asax 的 Application_Error 事件中。代码如下:
 
Title    void Application_Error(object sender, EventArgs e)
    {
        //定义写入日志的信息
        string Message = "\n\nURL:\n http://localhost/" + Request.Path + "\n\nMESSAGE:\n" + Server.GetLastError().Message+"\n\nSTACK TRACE:\n"+Server.GetLastError().StackTrace;
        //如果事件日志不存在,则在此创建
        string LogName = "Application";
        if (!EventLog.SourceExists(LogName))
        {
            EventLog.CreateEventSource(LogName, LogName);
        }
        //插入事件日志
        EventLog Log = new EventLog();
        Log.Source = LogName;
        //写入日志
        Log.WriteEntry(Message, EventLogEntryType.Error);
    }
转自 网络(如侵权请联系删除) 16 年前
云服务器 精选特惠
可能相关的内容