Where would I need to hook in (at what event) to be able send myself every 500 error that happens on my current ASP.NET project. Something inside the global.asax? And I'd like to send myself the output that the user would see. The white/yellow/black 500 standard ASP.NET error HTML.
My users only see the errors about 10% of the time. The other 90% these are Ajax responses.
You can do this in global.asax.cs using Global Error Handling in ASP.Net using the Application_Error(...) method
If you're more interested in the end result than in the process of making it work yourself, you might like to take a look at the Elmah ("Error Logging Modules and Handlers") project - http://code.google.com/p/elmah/
It's allows you to do many things with errors - send email notifications, log them to a database, browse the error log via a web interface, and even crazy things like an RSS feed of errors or using Twitter to tweet errors.
It's quite easy to install and configure.
Related
I'm looking for a correct way to handle the expected errors in my web API.
For example, i have a endpoint with 2 possible responses.
200 OK, with a result object
404 Not Found, with a ApiError object, containing a small (localized) message and title.
Currently i'm throwing an custom exception at the place in my code where i know if the resource is a Ok or Not found. I got a custom middleware in place to catch these exceptions and create a nice formatted ApiError.
This works perfect, except all these (known) messages are also logged inside my log files/Application Insights. It feels weird to add a boolean to this exception where i can specify if the exception should be logged. But currently this feels like the only possible way.
Is there any advice that could make this implementation beter or maybe i should change the implementation?
Well, the issue is not .NET 5 - it is application insight.
Your middleware basically should tell AI that those specific errors ARE NOT ERRORS.
https://peter.intheazuresky.com/2020/08/21/let-application-insights-focus-on-real-problems-and-not-missing-data-404-in-your-apis/
Basically, get the ITelemetryInterface from ApplicationInsight and tell of that despite the error code, the request was a success.
Voila, problem solved. AI can not magically know that specific 404 errors are "expected" as normal operations - you will have to help it out.
It makes sense to track them on a website / web application level. It does not make sense to treat them as errors on the backend level. The response (not found) is perfectly valid, from the point of an API. Just configure AI accordingly.
Currently, our ASP.NET Core 3.1 web api's error handling sends everything to our error controller using app.UseExceptionHandling('/error'). This controller logs the error and sends the error response our clients expect. Unfortunately, our logs contain each error twice because Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware logs the error before our error controller. How can we completely remove/replace Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware such that we (a) log the error only once and (b) completely replaces the response with our content?
We decided to use app.UseExceptionHandling('/error') over app.Use<CustomMiddlewareErrorHandler>() because changing the content in middleware always produced the "response already started" error that we never see when we use the error controller. I've found tutorials that advice testing whether the response has already started, but we always want to replace it. I suppose we need to flush the response or something like that.
Can someone point me to a practical example that (a) prevents double logging and (b) allows me to completely replace the response after an error?
I can get Detailed Error logging to screen (local/remote etc). But what I really want is to log "Detailed Error" to file.
Does this exist?
I checked all the documentation that I can find, and for the life of me i can't see how to do this.
I don't understand why this wouldn't be a requested feature - something that sits halfway between "no logs" and processor-intensive "failed request tracing".
If you are working with asp.net and want a drop in "write my errors to a log file" without making code changes, take a look at elmah
The "pure" IIS solution is Failed Request Tracing.
I have deployed the web application and customer accessing web application publically in between my databse down then I want show the custom message to the user. In my web application 50 pages, and I want to handle message commonly for all pages. Please help how to do this.
You can configure a section in web.config that tells ASP.NET/IIS to redirect users to a set of static or dynamic pages in case of common errors.
Check out this article - http://www.asp.net/hosting/tutorials/displaying-a-custom-error-page-cs
The scenario is pretty similar - something goes wrong with the SQL DB call and you dont want to display an ugly error.
Make sure to include some sort of logging somewhere so that you know these errors are happing, however. One plus side of ugly error messages are that you'll hear about them right away ;)
I've just starting looking at a site in place at work. They're using Elmah to log errors.
It seems to be logging fine, but in the web interface, when I tell Elmah to show me 100 errors per page, it only shows the most recent 15.
Then when I click on "Download Log" I only get shown 15 errors in the CSV.
Anyone know how I can configure it to keep all the errors?
Or can someone point me to some docs on how to do this?
Thanks a lot!
-Evan
Looks like there is no database configured to log errors to.
By default it only logs the last 15 errors to memory if it has no where to write it to...