How to implement custom Error handling in asp.net? - asp.net

I am Implementing Social Login with our application using asp.net. For social login, we are using Okta. We are using a third-party SDK. There is an error throwing from third-party SDK. When Error occurs from a third-party SDK it's not logging into our application, Directly display the error on the requested page. When exceptions occur from the third-party SDK, There is no clue to the application to manage the exception. Rather than display errors on the page we want them to display a custom error page. I have tried following none of them worked.
Webconnfig Custom error mode on. default URL is given.
In global.ascx Application_Error we try to log error. It's hitting this event as well.
In Page_Load we have implemented try{} catch{} blocks. There is no hit to Page_Load as well.
Please help to handle this type of error

Related

How to override page render for an error page?

We have an ASP.NET MVC application, and when it encounters an error you are not redirected to an erro rpage, instead the content of that page is replaced with the content of the error page. You go fix your code and press refresh and you're done.
We have another application that's written in WebForms and I'd like to get the same behavior out of it. Right now the current behavior is that when an error occurs you get redirected to ~/Error.aspx. Is it possible to make webforms behave this way? Perhaps override the page render event somehow?
This is just for development right? Displaying the error.aspx on production is much better both from a security and user experience perspective.
In order to turn off custom errors, you need to know how to turn them on. Yes? There are several places custom errors can be configured.
in IIS Create a Custom HTTP Error Response (IIS 7)
an error handler on the page, in global.asax, or in a class defined elsewhere (app_code folder perhaps)
in web.config Web.config customErrors mode
Web.config is probably the most common place. Start there.

How to get rid of YSOD but keep the standard logging functionality?

The default options of <system.web><customErrors> don't work very well in an ASP.NET MVC app if you want to return the actual HTTP status codes like 404, 500 etc. instead of 302 (because it works only with Web Forms).
No matter where and how I catch exceptions (like in Global.asax or in a global error handler) and "swallow" the exception (e. g. using Server.ClearError()), I have full control of what is rendered to the client but I loose the default logging (Web Events written to the Windows Event Log). This kind of logging works fine for me, it's very robust and I have Event Log monitoring anyway, so that's exactly what I want for logging.
Is there anything I can do to e. g. in Global.asax to simply force ASP.NET to log the current error as a Web Event and then clear the error?
A solution that requires ASP.NET 4 and IIS7.5 Integrated Mode would be fine.
One option that kind of works would be the use of <system.webServer><httpErrors>, however this works only with existingResponse="Replace", not Auto, because ASP.NET seems to automatically set TrySkipIisCustomErrors=true, and I would need this on Auto since a few controllers of my application need to return custom error pages (XML).
Thank you very much in advance!

Handle unhandled exceptions in asp.net

How can we prevent page crash in asp.net? Is there any generic function or place like global.asax where we specify a file to redirect to when an unhanded exception occurs? (like we redirect to a specified page when 404 page not found exception occurs?
<customErrors> doesn't prevent your pages from "crashing". It just allows you to apologize to your users when the page does crash.
You should be examining the Application event log for messages from the source "ASP.NET <version>". These will include details of any exceptions your code has been generating and not handling. You need to fix these, as every one of these indicates that your users were not able to do what they came to your site to do.
You can do it in Global.asax or you can set up custom error pages in web.config.
ASP.NET Custom Error Pages
How can we prevent page crash in asp.net?
As Mal said, "If it crashes, you crashed it". The only way to "prevent" crashes is by carefully writing your code, testing, etc, as with any program.
To display a nice error page, check out the <customErrors> element in your web.config. MSDN docs here. Plenty of articles around if you google for 'customErrors'.

Asp.Net error reporting with stacktrace

I have an Asp.Net web site set up to log errors using Log4Net (in global.asax) and redirect users to a custom error page (set up in web.config with: ). On this error page the users have the opportunity to write about what they did when the error occurred and post the description back to us to help fix the problem.
My question is this: How do i connect the error stacktrace to the users error report?
It appears that .Net has handled the error when it was written to the log file and the custom error page has no information about the stacktrace. If only I could see the stacktrace from the custom error page code-behind my problem would be solved.
It sounds like you need to generate an ID when you log the error, and pass that ID to the custom error page, where it can be stored with the user's report.

How do you do Error handling (especially SQL) in an ASP.NET project right

I have a another ASP.Net web project that I am working and I have approached error handling from different angles. I was just wondering if any seasoned and experienced developers out there had any recommendations.
Bonus points for discussions on a nice logging framework for ASP.net:)
I've used the ELMAH, (Error Logging Modules and Handlers) is an application-wide error logging facility that is completely pluggable, on several projects and it works great:
http://code.google.com/p/elmah/
I've also used the built in Health Monitoring in ASP.NET 2.0:
http://msdn.microsoft.com/en-us/library/ms998306.aspx
It depends on the environment for which your application is targeted. But what I do is have a generic ErrorHandler.aspx page that I redirect to in the customErrors section of the web.config, and then any exception that I don't handle and hide causes the page to redirect to the error handler which lets the user know what happened and gives them the option to report it.
I would suggest deriving from System.Exception to make something that allows you to specify more specifics for the ErrorHandler.aspx page such as a FriendlyMessage property.
I use the Application_Error() event to grab unhandled exceptions. I write them to a log using the Application.Log() method, then I redirect them to a user-friendly error page.
The Application.Log() can be configured using the web.config file to direct the output to a file, the event log on the server, etc.
In some circumstances, I have also implemented my own logging using a generic TraceSource and custom Listener that logged the messages to a database table.

Resources