global exception handling with nlog - c#

Is there a way blog can capture all exception without specifying a try catch? For example going to a page that is not available on a MVC website and picking up an error in Icontroller and capturing page not found. ?

NLog does not capture errors in your application. It allows you to log captured errors.
See Exception Handling in ASP.NET MVC or Global Error Handling in ASP.NET MVC to understand how you can capture errors. After exception is captured, you can log it with:
Logger.ErrorException(message, exception);

Related

CustomError and ConnectionStrings

I have an Error.aspx page to handle any Application_Error.
But there is one that I can't catch : When the site can't connect to the DB specified with the connectionstrings, Application_Error is called (in global.asax) but I don't reach Error.aspx even if there is no DB connection needed to display it. Instead, I have the standard ASP red message saying that there was an application Error on custom error page.
Any Idea to catch this error type ?

IdentityServer3 - Exception handling

Is there a way to handle exceptions ourselves. We have custom user service code inside our identityserver project and would like to know without diving through logs when errors occur.
We have a centralized error logger for all our apps. I'd imagine something similar needed if using 3rd party exception lib (airbrake etc).
Is there something possible at the moment or is this ticket solving my problem
https://github.com/IdentityServer/IdentityServer3/issues/2012
If you care about the error logging - the event service will log all unhandled exceptions.
The issue you mentioned would allow you to have more control over direct error handling (e.g. setting a custom http response) - but that's backlog right now.

catching request on exception in WCF

I have few webservices in my solution, they use NLOG for logging into the database, I want to catch the Request whenever there is an Exception , currently all the request and response calls are being logged into the database and I want to change it log only the Requests upon exception ..I did some research but did not find any proper answer, if you could give me some example than it could be verymuch helpful
I think you should try Application_Error method in Global.asax. It can catch any unhandled exceptions of application.
Documentation here: http://msdn.microsoft.com/ru-ru/library/24395wz3%28v=vs.100%29.aspx

Display Error Stack trace information in Custom Error page

I am using VS2010.
I log my exception in Application_Error and then get automatically redirected to Custom Error page.
On Custom Error page, I would like to display stack track of the error.
I can not implement the approach in following article because I am
using ajax controls in all the pages and I get ajax errors if I use
the solution in following article.
I updated the web.config as follows:
<customErrors defaultRedirect="~/CustomError.aspx" mode="On" redirectMode="ResponseRewrite" />
The ajax error for the web.config is as follows:
Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed. Common causes for this error are when the response is modified by calls to Response.Write(), response filters, HttpModules, or server trace is enabled.
Details: Error parsing near '
ASP.NET custom error page - Server.GetLastError() is null
I can not use the following approach because I am being told to stay
away from session and application level storage.
Asp.net 4.0 : How to get exception details in custom error page?
So what are my options?? is there any other way to store Error information in Application_Error and recover the error stack trace on Custom Error page? This page is automatically being rerouted by asp.net engine.

HttpAntiForgeryException being treated as 403 and 500 depending whether custom errors is on

We have recently implemented CSRF protection on a web site we are working on in ASP.Net MVC 3.
Using a bit of a mish mash of techniques, we have a working solution.
However our error handling part of the app is not working properly as the status code set for the exception is different depending on whether custom errors is turned on or not.
When custom errors is on, we get a 403, when off a 500 status code being set.
The exception being throw is an HttpAntiForgeryException, which should be a 500 from my inspection of the MVC source.
It's being thrown inside a ActionFilterAttribute, does this wrap the exception inside a 403?
Is IIS doing something odd?
Any thoughts would be appreciated.
Cheers,
I have the MVC 3 anti-CSRF solution implemented, and I never receive 403 code when purposely making the check to fail (by suppressing cookies on client side after the get and before the post).
Check done on an action having a ValidateAntiForgeryToken attribute. cutomErrors set with RemoteOnly, then On, then Off. Tested twice, first time on Cassini dev server, second time on IIS6.
I have always received a 500 response code.
I guess the trouble is located in the customErrors handling you are using. You should also check if you have any httpModule doing some custom logic on error events. (Unlikely if you use MVC HandleError attribute, as this MVC customError handling traps the errors before HttpModule handling, which then never see errors.)
Without more details in your question about which customError handling mechanism are you using, it is difficult to give more directions.
Mine is neither the MVC standard one (using HandleError as controller attribute, action attribute or global filter), nor the classical asp.net one. It is not ELMAH either.
I handle errors in Application_Error event due to "historical" reasons (and for I found it easier to support both ISS 6 and 7 error handling, which I need to do till the migration of our servers is done). Code of my error handling logic in Application_Error event is like :
var statusCode = 500;
var ex = Server.GetLastError();
if (ex != null)
{
var httpEx = ex as HttpException;
if (httpEx != null)
{
// HttpAntiForgeryException is HttpException and gets received here,
// so its statusCode is what I get here.
statusCode = httpEx.GetHttpCode();
}
}
// Some logging logic then
if (!Context.IsCustomErrorEnabled)
return;
Response.ClearContent();
Response.StatusCode = statusCode;
// Rendering custom error page in response then
Server.ClearError();

Categories