• Asp.Net 2.0 中错误处理的几种方法(发邮件及记录信息到系统事件中)


    一般用到文件Global.asax,但也有一些问题,详见:asp.net 2.0 中Global.asax 使用小記(http://www.cnblogs.com/cnaspnet/articles/521045.html)
    因此,代码分离:
    <%@ Application Inherits="FileECR.Webs.FileECRHttpApplication" Language="C#"  %>

    using System;
    using System.Web;
    using System.Diagnostics;

    namespace FileECR.Webs
    {
        
    /// <summary>
        
    /// FileECRHttpApplication 的摘要说明
        
    /// </summary>

        public class FileECRHttpApplication : HttpApplication
        
    {
            
    public void Application_Start(object sender, EventArgs e)
            
    {
                
    // 在应用程序启动时运行的代码
                if (System.Diagnostics.EventLog.Exists("MyNewLog"))
                
    {
                    System.Diagnostics.EventLog.Delete(
    "MyNewLog");
                }


            }


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

            }


            
    public void Application_Error(object sender, EventArgs e)
            
    {
                
    // 在出现未处理的错误时运行的代码
                string strPageUrl = Request.Path;
                
    string struserIP = System.Web.HttpContext.Current.Request.UserHostAddress;

                Exception strErrorInfo 
    = Server.GetLastError().GetBaseException();

                
    string strMessage = "Url:" + strPageUrl + "</br>";
                strMessage 
    = strMessage + "Time:" + DateTime.Now.ToString() + "</br>";
                strMessage 
    = strMessage + "UserIP:" + struserIP + "</br>";
                strMessage 
    = strMessage + " Error: ";
                strMessage 
    = strMessage + strErrorInfo.ToString() + "</br>";

                Common.Errors.LogSystem(strMessage);
            }


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

            }


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

            }

        }

    }

    一些用到的,需要注意的一些实用代码:

    直接写入到系统日志中:
     myLog.WriteEntry(strMessage, EventLogEntryType.Error);

    在服务器上独立创建一个目录,记录事件:
                // Create the source, if it does not already exist.
                if (!EventLog.SourceExists("MySource"))
                
    {
                    EventLog.CreateEventSource(
    "MySource""MyNewLog");
                }


                
    // Create an EventLog instance and assign its source.
                EventLog myLog = new EventLog();
                myLog.Source 
    = "MySource";

                
    // Write an informational entry to the event log. 
                myLog.WriteEntry(strMessage, EventLogEntryType.Error);

    有时候在服务器上并没有权限创建独立的事件,因此先在建立好的目录下写入:
    EventLog.WriteEntry("MySource", strMessage, EventLogEntryType.Error);

    删除指定目录下所有记录:
                if (EventLog.SourceExists("MySource"))
                
    {
                    EventLog.DeleteEventSource(
    "MySource""MyNewLog");
                }

    删除目录:
                if (System.Diagnostics.EventLog.Exists("MyNewLog"))
                
    {
                    System.Diagnostics.EventLog.Delete(
    "MyNewLog");
                }

    保存日志到硬盘目录上:
            public static void LogFile(string message)
            
    {
                
    if (File.Exists(FILE_NAME))
                
    {
                    StreamWriter sr 
    = File.AppendText(FILE_NAME);
                    sr.WriteLine(
    "\n");
                    sr.WriteLine(
    "======================" + DateTime.Now.ToString() + "====================");
                    sr.WriteLine(DateTime.Now.ToString() 
    + message);
                    sr.Close();
                }

                
    else
                
    {
                    StreamWriter sr 
    = File.CreateText(FILE_NAME);
                    sr.Close();
                }

            }
  • 相关阅读:
    svn命令
    Lambda 表达式
    JAVA 探究NIO
    JAVA I/O系统
    MySQL 锁
    spring boot 集成 zookeeper 搭建微服务架构
    架构演化
    JAVA 集合
    spring boot 发送邮件
    MySQL 隔离级别
  • 原文地址:https://www.cnblogs.com/cnaspnet/p/680972.html
Copyright © 2020-2023  润新知