在WEB项目下面建立一个Global.asax(全局应用程序类),这样就可以在你的WEB程序出现错误的时候记录下来,并保存成文本文档(txt)。排除404和403错误。完整代码如下:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.IO;
using System.Globalization;
using Web.Comman;
using Web.Comman.Helper;
using Web.Comman.Utility;

namespace Web.WebPage
{
   
public class Global : System.Web.HttpApplication
    {
       
protected void Application_Start(object sender, EventArgs e)
        {
           
//在应用程序启动时运行的代码
        }

       
protected void Application_End(object sender, EventArgs e)
        {
           
//在应用程序关闭时运行的代码
        }

       
protected void Application_Error(object sender, EventArgs e)
        {
           
// 在出现未处理的错误时运行的代码
            Exception objErr = Server.GetLastError().GetBaseException();
           
if (objErr.GetType() == typeof(HttpException))
            {
               
int i = ((HttpException)objErr).GetHttpCode();
               
if (i == 404)
                {
                    Response.Redirect(
"~/ErrorPage/FileNotFind.html");
                }
               
else if (i == 403)
                {
                    Response.Redirect("
~/ErrorPage/NoAccess.html");
                }
            }
           
else
            {
               
string error = string.Empty;
               
string errortime = string.Empty;
               
string erroraddr = string.Empty;
               
string errorinfo = string.Empty;
               
string errorsource = string.Empty;
               
string errortrace = string.Empty;

                error
+= string.Format("发生时间:{0}
"
, DateTime.Now.ToString());
                errortime
= string.Format("发生时间:{0}", DateTime.Now.ToString());

                error
+= string.Format("发生异常页: {0}
"
, Request.Url.ToString());
                erroraddr
= string.Format("发生异常页: {0}", Request.Url.ToString());

                error
+= string.Format("异常信息: {0}
"
, objErr.Message);
                errorinfo
= string.Format("异常信息: {0}", objErr.Message);

                errorsource
= string.Format("错误源:{0}", objErr.Source);
                errortrace
= string.Format("堆栈信息:{0}", objErr.StackTrace);
                error
+= "--------------------------------------
"
;
                Server.ClearError();
                Application["
error"] = error;

               
//独占方式,因为文件只能由一个进程写入.
                StreamWriter writer = null;
               
try
                {
                   
lock (this)
                    {
                       
//写入日志
                        string year = DateTime.Now.Year.ToString();
                       
string month = DateTime.Now.Month.ToString();
                       
string path = string.Empty;
                       
string filename = string.Format("{0}.txt", DateTime.Now.ToString("yyyyMMdd", DateTimeFormatInfo.InvariantInfo));
                        path
= string.Format("{0}{1}/{2}", Server.MapPath("~/Log-File/"), year, month);
                       
//如果目录不存在则创建
                        if (!Directory.Exists(path))
                        {
                            Directory.CreateDirectory(path);
                        }
                        FileInfo file
= new FileInfo(string.Format("{0}/{1}", path, filename));
                        writer
= new StreamWriter(file.FullName, true);//文件不存在就创建,true表示追加
                        writer.WriteLine(string.Format("用户IP:{0}", Request.UserHostAddress));
                        writer.WriteLine(errortime);
                        writer.WriteLine(erroraddr);
                        writer.WriteLine(errorinfo);
                        writer.WriteLine(errorsource);
                        writer.WriteLine(errortrace);
                        writer.WriteLine("
-----------------------------------------------------------------------------------------------------------------");
                    }
                }
               
finally
                {
                   
if (writer != null)
                    {
                        writer.Close();
                    }
                }
                String GoUrl
= String.Format("{0}/ErrorPage/ShowMsg.aspx?Msg={1}", ConstantConfig.SiteDomain, StringHelper.EncodeString(HtmlCodeChange.HtmlEnCode("你访问的页面发生未知错误!")));
                Response.Redirect(GoUrl);
            }
        }

       
protected void Session_Start(object sender, EventArgs e)
        {
           
//在新会话启动时运行的代码
        }

       
protected void Session_End(object sender, EventArgs e)
        {
           
//在会话结束时运行的代码。 
           
//注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
           
//InProc 时,才会引发 Session_End 事件。如果会话模式 
           
//设置为 StateServer 或 SQLServer,则不会引发该事件。
        }
    }
}