网站准备放外网了。我怕代码中有错误。请问怎么获取网站错误。
然后插入数据库。让我查看方便修改。

解决方案 »

  1.   

    写个错误日志
    http://www.cnblogs.com/ringwang/archive/2007/12/12/992023.html
      

  2.   

    这个是把错误信息保存到Xml文件中的,可以改一下保存到数据库中
    WebLogs.csusing System;
    using System.Data;
    using System.Data.SqlClient;
    using System.IO;
    using System.Xml;namespace Wg
    {
    /// <summary>
    /// WebLogs 的摘要说明。
    /// </summary>
    public class WebLogs
    {
    private string logPath = string.Empty; public WebLogs(string LogPath)
    {
    //
    // TODO: 在此处添加构造函数逻辑
    //
    logPath = LogPath;
    } public string FloderPath
    {
    get
    {
    return logPath;
    }
    set
    {
    logPath = value;
    }
    } public void Add(string Content,string IP,string Agent,string Url,string UrlReferrer,string SessionXML)
    {
    string filename = DateTime.Now.ToShortDateString()+".xml";
    string filepath = logPath + filename;
    LogWrite(filepath,Content,IP,Agent,Url,UrlReferrer,SessionXML);
    } private void LogWrite(string filepath,string Content,string IP,string Agent,string Url,string UrlReferrer,string SessionXML)
    {
    XmlDocument xdoc = new XmlDocument();
    if(!File.Exists(filepath))
    {
    xdoc.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?>"+
    "<item></item>");
    }
    else
    {
    xdoc.Load(filepath);
    } XmlDocument xmldoc = new XmlDocument();
    xmldoc.LoadXml(SessionXML);
    XmlElement root = xmldoc.DocumentElement; // 创建一个新的日志节点并将它添加到根节点下
    XmlElement parentNode = xdoc.CreateElement("ErrorLog");
    xdoc.DocumentElement.PrependChild(parentNode);
    // 创建所有用于存储信息的节点
    XmlElement timeNode = xdoc.CreateElement("Time");
    XmlElement ipNode = xdoc.CreateElement("IP");
    XmlElement agentNode = xdoc.CreateElement("Agent");
    XmlElement urlNode = xdoc.CreateElement("Url");
    XmlElement referrerNode = xdoc.CreateElement("Referrer");
    XmlElement sessionXML = xdoc.CreateElement("sessionXML");
    // 获取文本信息
    XmlText timeText = xdoc.CreateTextNode(DateTime.Now.ToString());
    XmlText ipText = xdoc.CreateTextNode(IP);
    XmlText agentText = xdoc.CreateTextNode(Agent);
    XmlText urlText = xdoc.CreateTextNode(Url);
    XmlText referrerText = xdoc.CreateTextNode(UrlReferrer);
    XmlText sessionText = xdoc.CreateTextNode(SessionXML);
    // 将上面创建的各个存储信息的节点添加到guest节点下但并不包含最终的值
    parentNode.AppendChild(timeNode);
    parentNode.AppendChild(ipNode);
    parentNode.AppendChild(agentNode);
    parentNode.AppendChild(urlNode);
    parentNode.AppendChild(referrerNode);
    parentNode.AppendChild(sessionXML);
    // 将上面获取的文本信息添加到与之相对应的节点中
    timeNode.AppendChild(timeText);
    ipNode.AppendChild(ipText);
    agentNode.AppendChild(agentText);
    urlNode.AppendChild(urlText);
    referrerNode.AppendChild(referrerText);
    sessionXML.AppendChild(sessionText); // 保存存储信息的XML文件
    xdoc.Save(filepath);
    xdoc = null;
    }
    }
    }Global.asax.cs protected void Session_Start(Object sender, EventArgs e)
    {
    WebVisitor visitor = new WebVisitor(Server.MapPath(null).Substring(0,Server.MapPath(null).ToLower().LastIndexOf("wg")+2)+"//logs//count.xml");
    visitor.addCount();
    } protected void Application_Error(Object sender, EventArgs e)
    {
    WebLogs Logs = new WebLogs(Server.MapPath(null).Substring(0,Server.MapPath(null).ToLower().LastIndexOf("wg")+2)+"//logs/");
    string SessionXML  = this.Session["xml"].ToString();
    //string SessionXML  = Server.MapPath(null).Substring(0,Server.MapPath(null).ToLower().LastIndexOf("wg")+2)+"//temp/"+this.Session["xml"]+".xml";
    string referrerUri = (Request.UrlReferrer != null) ? Request.UrlReferrer.AbsoluteUri : string.Empty;
    Logs.Add(Server.GetLastError().ToString(),Request.UserHostAddress,Request.UserAgent,Request.Url.AbsoluteUri,referrerUri,SessionXML); Server.Transfer("../Error.aspx");
    }
      

  3.   

    Application_Error(object sender, EventArgs e) 
    中把错误写入磁盘
      

  4.   

    外挂个webserveice来接受错误数据。
      

  5.   

    Application_Error(object sender, EventArgs e) 恩,在这个glob.asx文件方法中确实可以捕获你这个站点的所有错误