Haoest
Haoest

Reputation: 13896

catching specific error in global.asax

I am using Application_Error event in global asax to catch errors and write them to a file. I can see the specific exception in the webpage, but in the log file all I see is a generic error like this:

Exception of type 'System.Web.HttpUnhandledException' was thrown.
   at System.Web.UI.Page.HandleError(Exception e)
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
   at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
   at System.Web.UI.Page.ProcessRequest()
   at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context)
   at System.Web.UI.Page.ProcessRequest(HttpContext context)
   at ASP.review_aspx.ProcessRequest(HttpContext context) in c:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\...\ff7eee7c\ff24ade1\App_Web_tddyd4bt.3.cs:line 0
   at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

How can I write the errors as I see in the yellow page to log file instead of this generic error?

Upvotes: 2

Views: 1512

Answers (2)

A Ghazal
A Ghazal

Reputation: 2813

In the Global.asax, find the Application_Error method and add the following:

 protected void Application_Error(object sender, EventArgs e)
    {
       Exception ex = Server.GetLastError();
        LogHelper.WriteLog(ex);
    }

You need to create a class to write the log..

public class LogHelper
{
    public static void WriteLog(Exception exception)
    {
       //write to text file the exception.Message ...
    }
}

Upvotes: 0

ssilas777
ssilas777

Reputation: 9804

Is this what you looking for?

 Exception ex = Server.GetLastError();    
 String ExceptionMessage = (ex.InnerException != null) ? ex.InnerException.Message : ex.Message;

Upvotes: 3

Related Questions