程序中错误信息的处理办法

    技术2022-05-11  94

    将错误发送给开发者

     

    using  System; using  System.Collections; using  System.ComponentModel; using  System.Web; using  System.Web.SessionState; using  System.Web.Mail ; using  System.Diagnostics ; namespace  WebApplicationI  {    /// <summary>    /// Global 的摘要说明。    /// </summary>    public class Global : System.Web.HttpApplication    {        /// <summary>        /// 必需的设计器变量。        /// </summary>        private System.ComponentModel.IContainer components = null;        public Global()        {            InitializeComponent();        }                    protected void Application_Start(Object sender, EventArgs e)        {        }         protected void Session_Start(Object sender, EventArgs e)        {        }        protected void Application_BeginRequest(Object sender, EventArgs e)        {        }        protected void Application_EndRequest(Object sender, EventArgs e)        {        }        protected void Application_AuthenticateRequest(Object sender, EventArgs e)        {        }        protected void Application_Error(Object sender, EventArgs e)        {                        //把错误信息发送到作者            string strPageUrl = Request.Path;            Exception ErrorInfo =Server.GetLastError();            //Server.ClearError();            string strMessage = "Url:" + strPageUrl + "</br>";            strMessage = strMessage + " Error: ";            strMessage = strMessage + ErrorInfo.ToString() + "</br>";            MailMessage Mymessage = new MailMessage();            Mymessage.To = "***@***.com.cn";            Mymessage.From = "***@***.com.cn";            Mymessage.Subject = "ASP.NET Error";            Mymessage.BodyFormat = MailFormat.Text;            Mymessage.Body = strMessage;             //SmtpMail.SmtpServer = " ";             SmtpMail.Send(Mymessage);        }        protected void Session_End(Object sender, EventArgs e)        {        }        protected void Application_End(Object sender, EventArgs e)        {        }                    Web 窗体设计器生成的代码    }}

     

     

    将错误写入系统日志

     

     

         string  strMessage  =  Server.GetLastError().Message;     // Response.Write(strMessage);     Server.ClearError();     // 以下把信息写入windows日志     // 要把aspnet用户添加到管理员组中,以便有写注册表权限      if ( ! EventLog.SourceExists( " mySource " ))        EventLog.CreateEventSource( " mySource " , " myLog " );    EventLog Event  =   new  EventLog();    Event.Source  =   " mySource " ;    Event.WriteEntry( System.DateTime .Now .ToString (),EventLogEntryType.Warning);     // EventLog.Delete("myLog");     // throw new Exception("我处理不了!");

     

    将错误信息写入输出窗口

    System.Diagnostics .Debug .WriteLine( 想要输出的值);

     

     

    将错误信息写入目录下的log

                System.Diagnostics.Trace.Listeners.Clear();             System.Diagnostics.Trace.AutoFlush=true;             string    strPath    =    string.Empty;             strPath    =     Page.Server.MapPath (".") ;             strPath    +=    @"/app.log";             FileStream fs = new FileStream( strPath, FileMode.Append , FileAccess.Write );             System.Diagnostics .Trace.Listeners.Add( new System.Diagnostics .TextWriterTraceListener( fs ) );                         System.Diagnostics .Trace .WriteLine("star");             System.Diagnostics .Trace .WriteLine(System.DateTime.Now.ToLongTimeString () );             System.Diagnostics .Trace .WriteLine(    Page.ToString());             System.Diagnostics .Trace .WriteLine("end");             fs.Close ();                         

     


    最新回复(0)