PassionateDeveloper
PassionateDeveloper

Reputation: 15158

ASP.Net Global Error Handling and Error Site?

I am having a huge website with over 100 single sites in ASP.Net. Of course I try to catch every action with a try-catch block or with other things like validation-controls.

But I want, IF a error happens which I get not catched to happens following:

1) Write the Error in Database
2) Show user a specific site instead the errorsite from asp.net

How to do that?

Upvotes: 0

Views: 87

Answers (3)

Joel Lee
Joel Lee

Reputation: 3884

You can use the Application_Error event handler in in Global.asx to handle any exceptions that are not caught at the page level. See, for example,

http://msdn.microsoft.com/en-us/library/24395wz3(v=vs.100).aspx

It's kind of up to you what you do in the event handler, so you can log the error to a database if you want to. You can also redirect to another page of your choosing to display the error however you want.

Note that the Application_Error event will be raised for all uncaught exceptions, including Http exceptions (e.g. 404 Not found). You probably don't want to log those.

Upvotes: 1

gdp
gdp

Reputation: 8252

If you not into trying elmah, which is a breeze to setup. It depends on how your 100 sites are setup. But you possibly could look into using the global Application_Error event in global.asax.cs and add you own handling code in there.

protected void Application_Error(object sender, EventArgs e)
{
    var lastException = Server.GetLastError();

    //log it to db, re-route the request to an alternate location ... etc
}

Another option, again it depends on how your sites are setup/hosted would be to read the event_log on the server, and check for ASP.NET errors saving the relevant details to the db.

Upvotes: 1

Nilesh Thakkar
Nilesh Thakkar

Reputation: 2895

You should try ELMAH

Check out the blog post of Scott Hanselman on how to integrate it in asp.net website and make it work.

Below is the article from Scott Mitchel on how to log errors with Elmah and how to show custom error page to user:

http://www.asp.net/web-forms/tutorials/deployment/deploying-web-site-projects/logging-error-details-with-elmah-cs

http://www.asp.net/web-forms/tutorials/deployment/deploying-web-site-projects/displaying-a-custom-error-page-cs

Upvotes: 1

Related Questions