如何在主代码

时间:2016-03-09 09:27:11

标签: asp.net

在普通的c#代码中,一旦我们添加了下面提到的代码,就可以处理可能发生崩溃的异常。

但是在asp.net中是否有任何代码可以使用单个函数处理任何类型的崩溃。

注意:这是针对C#的,我想知道在asp.net的情况下有任何替代选项

static void Main()
    {
        AppDomain app = AppDomain.CurrentDomain;
        app.UnhandledException += new UnhandledExceptionEventHandler(app_UnhandledException);
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
    static void app_UnhandledException(object sender, UnhandledExceptionEventArgs e)
    {
        MessageBox.Show("MyHandler caught : ");
        MessageBox.Show("Runtime terminating:");
        Environment.Exit(1);
    }

1 个答案:

答案 0 :(得分:0)

aspx.cs code:
    protected void btnadd_Click(object sender, EventArgs e)
    {
        try
        {
            //code goes here

        }
        catch (exception ex)
        {
            ExceptionHandle(ex);
        }
    }

    public void ExceptionHandle(Exception ex)
    {
        lblErrorMesg.InnerText = Messages.ApplicationError();
        LoggerHandler.ExceptionWrite(ex, this.GetType().FullName.ToString());


    }
    class file:
    using System.IO;
    using Microsoft.Practices.EnterpriseLibrary.Logging;
    public static class LoggerHandler
    {
         public static void ExceptionWrite(Exception ex, string strClass)
                {
                    System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace(ex);
                    LogEntry Log = new LogEntry();
                    Log.Severity = System.Diagnostics.TraceEventType.Error;
                    Log.Title = strClass + ": " + st.GetFrame(st.FrameCount - 1).GetMethod();
                    Log.Message = ex.Message;
                    //write error here in database wherever you want
                }
    }