ELMAH not logging exceptions if Custom error page is enabled - asp.net-mvc

I have this path /test/account and it has a code
throw new Exception("error");
in my web.config
<customErrors mode="On">
<error statusCode="404" redirect="~/404.html" />
</customErrors>
When it's mode is On, it shows custom error page instead of exception and elmah doesn't log it. When it's Off, it displays the exception and it's logged.
My understanding is that we need to log exceptions and we have elmah for that, however we don't want to display those detailed exceptions in production so we have custom error page.
I want that even though I display custom error page when there's an exception, I still want elmah to log it.

Custom error pages do actually "swallow" exceptions and as a result, ELMAH is never notified about the exception. There's a nice little trick to log handled exceptions, taken from ELMAH and custom errors. Create this class:
public class ElmahExceptionLogger : IExceptionFilter
{
public void OnException (ExceptionContext context)
{
if (context.ExceptionHandled)
{
ErrorSignal.FromCurrentContext().Raise(context.Exception);
}
}
}
Then configure the filter:
protected void Application_Start()
{
GlobalConfiguration.Configuration.Filters.Add(new ElmahExceptionLogger());
}

Related

ASP.NET MVC 5 application on Azure won't log exceptions

I'm having difficulties getting uncaught exceptions logged when deploying my ASP.NET MVC 5 application to an Azure App Service slot. When an uncaught exception occurs, the app rightly returns a 500 and the standard "Error. An error occurred while processing your request" Error.cshtml page, but never records any mention of the exception in any of my logs.
Current Setup:
Azure Settings:
Application Logging: On, logging to both filesystem and storage blobs, at the information level
Detailed error messages: On
Failed request tracing: On
Application Setup:
Using NLog to write to both a file and Trace destinations, which is working fine elsewhere and on caught exceptions
Haven't explicitly set <customErrors /> in my Web.configs, but they are showing as off locally and on remotely, as expected and desired
Added an Application_Error override to Global.asax (see below)
Added a custom ExceptionLoggingFilter (see below) and registered it in my FilterConfig.cs pipepline as the first item
Application_Error override in Global.asax:
protected void Application_Error(Object sender, EventArgs e)
{
Trace.TraceError("Uncaught exception bubbled up. Details: {0}", e);
Logger log = LogManager.GetCurrentClassLogger();
Exception ex = Server.GetLastError();
log.Fatal(ex, "Uncaught exception bubbled up to MVC root.");
}
Customer ExceptionLoggingFilter registered in GlobalFilters:
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
public void OnException(ExceptionContext filterContext)
{
var ex = filterContext.Exception;
var request = filterContext.HttpContext.Request;
Logger.Fatal(ex, "Uncaught exception bubbled up to MVC root.");
}
FilterConfig.cs:
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new ExceptionLoggerFilter());
filters.Add(new HandleErrorAttribute());
}
NLog.config:
...
<targets async="true">
<target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log" layout="${longdate} ${uppercase:${level}} ${message} ${exception:format=tostring}" />
<target xsi:type="Trace" name="trace" layout="${logger} ${message} ${exception:format=tostring}" />
<!-- another target for sending data to Slack here... -->
</targets>
...
<rules>
<logger name="*" minlevel="Debug" writeTo="f" />
<logger name="*" minlevel="Trace" writeTo="trace" />
</rules>
...
What I've tried:
I created an action that purposely triggers an uncaught exception (var neverResult = Convert.ToInt32("a");) and when I navigate to that page I get the custom error page. Implementing <customErrors mode="Off" /> in production on Azure does what I'd expect: I know see the full error message and stack trace when navigating to that example action BUT I do not get any logging going on, not from NLog, traces, watching the streaming logs, nada.
I've tried adding the additional handlers and filters for logging errors as explained above, and still nothing, though I know by remote debugging that the code is all being run in those additional handlers. Locally everything gets logged just fine.
Help!
I'm at a bit of a loss here even where to look next. Not sure if I need to look at Azure setting, how ASP.NET is handling things, or if NLog is off (even though it logs everything else fine). Any direction is greatly appreciated.
Update: Added actual filters config and NLog config.
I actually tried this with your configuration and found the problem.
If you use Logger.Fatal, the Trace message gets written in Verbose level for some reason. Change your logging filter to use the Error function:
Logger.Error(ex, "Uncaught exception bubbled up to MVC root.");
And also, exception filters run last-to-first, so in your case HandleErrorAttribute runs first. It however doesn't mean your filter won't run (ASP.NET Core is a bit different in this regard).
The Fatal function of NLog calls the Fail function of the Trace class, which is supposed to be used to show a message box to the user in Windows environments. For some reason that is on Verbose level on IIS.
TL;DR Don't use Fatal(), use Error().

Handling UnauthorizedAccessException with custom errors in MVC 4 Application

I have enabled the global error handling for an application by applying the HandleError attribute within the filterConfig registration.
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
}
I am then using the custom errors (web.config) to hopefully display a friendly error message for each server error.
<customErrors mode="On" ></customErrors>
This seemed to be working fine for most exceptions and I was getting the expected behaviour in that the custom error page View (Error.cshtml in the shared view folder) was being displayed.
However I have recently noticed that this is not the behaviour I see if the error thrown is an UnauthorizedAccessException.
I am a bit stumped with this, as looking in fiddler I see that this UnauthorizedAccessException exception returns a plain 500 internal server error as a standard exception does.
So how come the standard exception abides by my customError setup but the UnauthorizedAccessException does not?
ANd how can I get them to behave the same, as they are both essentially an error which I want to prevent the end user from seeing.
This blog post provided me with the overview of exception handling to enable me to decide how to handle the unauthorizedAccessException, which essentially means handling them within the Application_OnStart.
http://prideparrot.com/blog/archive/2012/5/exception_handling_in_asp_net_mvc
For my purposes there doesn't seem much point in handling the errors with the HandleErrorAttribute and in the global Application_OnStart so for my purposes I decided it was best to handle everything in the Application_OnSTart,
If you just want to force 'unhandled' exceptions like UnauthorizedAccessException to go through the normal custom-error page then you can override the controller's OnException method similar to the following:
protected override void OnException(ExceptionContext filterContext)
{
base.OnException(filterContext);
if (!filterContext.ExceptionHandled && filterContext.RequestContext.HttpContext.IsCustomErrorEnabled)
{
filterContext.ExceptionHandled = true;
filterContext.HttpContext.Response.StatusCode = (int)System.Net.HttpStatusCode.InternalServerError;
filterContext.Result = View("Error",
new HandleErrorInfo(filterContext.Exception, filterContext.GetCurrentControllerName(), filterContext.GetCurrentActionName()));
}
}
The article that you referenced is an excellent resource for a more thorough explanation of error-handling techniques, though, and should be considered as well.

ASP.NET MVC 4 - Exception Handling Not Working

When an error occurs in my ASP.NET MVC 4 application, I would like to customize a view for the user depending on the type of error. For example, page not found or an exception has occurred (with some user-friendly details about the exception). I've checked other examples of how to do this on StackOverflow and other online sources, but none of the answers are working for me.
The basic [HandleError] attribute does not seem to be working in VS2012 with an MVC 4 application targeting .NET 4.5. Here is the code I have in my home controller:
[HandleError]
public ActionResult Index()
{
Response.TrySkipIisCustomErrors = true; //doesn't work with or without this
throw new NullReferenceException("Uh oh, something broke.");
}
It is just throwing an exception, and I would expect the default ~/Shared/Error.cshtml view to be returned because of the [HandleError] attribute, but all I get is an HTTP 500 Internal Server Error indicating that the page could not be displayed. I checked my web.config, and different configurations seem to be behaving weird. In the section, it currently contains:
<customErrors mode="On" />
(I've tried adding defaultRedirect and with customErrors mode="Off" as well but that didn't have any effect... neither the shared Error view or the CustomError view I have is being rendered. If I change customErrors mode to off, then I can see the exception details as expected, so it is throwing the "Uh oh, something broke" exception properly.
I've also tried adding an OnException handler to the HomeController, and although I can debug through and see that the OnException event is being raised, it doesn't make any difference:
protected override void OnException(ExceptionContext filterContext)
{
base.OnException(filterContext);
filterContext.ExceptionHandled = true;
if (filterContext == null)
{
filterContext.Result = View("CustomError");
return;
}
Exception e = filterContext.Exception;
// TODO: Log the exception here
ViewData["Exception"] = e; // pass the exception to the view
filterContext.Result = View("CustomError");
}
I have also tried changing [HandleError] to specify a view, but that doesn't seem to do anything either:
[HandleError(View="CustomError")]
Any help would be much appreciated. Please let me know if you need any more details.
I also went on a seamingly endless journey of reading SO answers and assorted blog postings trying to get custom error pages to work. Below is what finally worked for me.
The first step is to use IIS Express for debugging instead of the built-in Cassini web server to "guarantee" that the debug experience will mirror the live environment.
Create a controller to handle application errors and add an action for each custom error you will handle. The action should do any logging, set the status code, and return the view.
public class ErrorsController : Controller
{
// 404
[HttpGet]
public ActionResult NotFound()
{
Response.StatusCode = (int)HttpStatusCode.NotFound;
return View();
}
// I also have test actions so that I can verify it's working in production.
[HttpGet]
public ActionResult Throw404()
{
throw new HttpException((int)HttpStatusCode.NotFound, "demo");
}
}
Configure the customErrors section in web.config to redirect to your custom error actions.
<system.web>
<customErrors mode="RemoteOnly" defaultRedirect="Errors/InternalServerError">
<error statusCode="400" redirect="Errors/BadRequest" />
<error statusCode="403" redirect="Errors/Forbidden" />
<error statusCode="404" redirect="Errors/NotFound" />
<error statusCode="500" redirect="Errors/InternalServerError" />
</customErrors>
Add the httpErrors section to system.webServer and set the errorMode to Detailed in web.config. Why this works is a mystery to me, but this was the key.
<system.webServer>
<httpErrors errorMode="Detailed" />
Add a catchall route last to the defined routes to direct 404s to the custom page.
// catchall for 404s
routes.MapRoute(
"Error",
"{*url}",
new {controller = "Errors", action = "NotFound"});
You may also want to create a custom HandleErrorAttribute and register it as a global filter to log 500 errors.
These steps worked for me in both development (IIS Express) and production (IIS7) environments. You need to change customErrors mode="On" to see the effect in development.
I seem to recall that you had to call the page from a non-localhost IP address (typically another computer). And it has to be an IIS based server, not the built-in development server (so IIS or IIS Express, but then you have to configure IIS Express for external access, which is a pain).
You can in fact debug it, you have to configure your local server on your debug computer to accept external requests, then call your local server from the remote server.
I faced a similar problem and lost sometime trying to work out what was going on. So just in case any others face a similar problem here is what my problem was.
The error page was trying to use my _Layout page. Just ensure that you Error.cshtml page has
#{
Layout = null;
}
Try adding the following attribute to the customErrors tag:
defaultRedirect="Error"
This explicitly defines to which view MVC should redirect the user when the error is thrown and no view is specified by default in the attribute. This will of course only work if Error.cshtml exists in the Shared views folder.

MVC InvalidOperationException with custom error pages

I have custom error pages setup using
<customErrors mode="On" defaultRedirect="~/Home/Error">
<error statusCode="404" redirect="~/Home/PageNotFound" />
</customErrors>
I created a page that throws and exception and I get redirected to the correct error pages.
However I am noticing these errors in elmah on the production webserver:
System.InvalidOperationException The view 'Error' or its master was
not found or no view engine supports the searched locations. The
following locations were searched:
~/Areas/Football/Views/Draft/Error.aspx
~/Areas/Football/Views/Draft/Error.ascx
~/Areas/Football/Views/Shared/Error.aspx
~/Areas/Football/Views/Shared/Error.ascx ~/Views/Draft/Error.aspx
~/Views/Draft/Error.ascx ~/Views/Shared/Error.aspx
~/Views/Shared/Error.ascx ~/Areas/Football/Views/Draft/Error.cshtml
~/Areas/Football/Views/Draft/Error.vbhtml
~/Areas/Football/Views/Shared/Error.cshtml
~/Areas/Football/Views/Shared/Error.vbhtml ~/Views/Draft/Error.cshtml
~/Views/Draft/Error.vbhtml ~/Views/Shared/Error.cshtml
~/Views/Shared/Error.vbhtml
Why is it looking for the error page elsewhere? I deleted ~/Views/Shared/Error.cshtml and added my custom error page at ~/Home/Error since i specified a new default in my config file.
Any ideas?
Thanks.
MVC projects by default adds the HandleErrorAttribute in the Global.asax.cs file
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
This filter is executed when an unhandled exception is thrown. It sets the view as Error. Hence MVC runtime tries to render that view. But in your case, there is no such view. So it again throws another exception which is handled by ASP.NET runtime and shows your error page that you have configured in Web.Config file.
You can create your own exception filter and register it.
I ended up taking out the registration of HandleErrorAttribute in Global.asax and just using the <customErrors> section. ELMAH now properly logs errors, and I'm able to specify custom error pages.
Am I missing something?
You can also make sure that the HandleErrorAttribute is not registered by removing it from the global filters, using the Remove method:
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Remove(new HandleErrorAttribute());
/* ... your other filters */
}
}
For future readers of this post, note that Elmah adds its own HandleErrorAttribute so is also expecting the Error.cshtml view. Note that I also installed the Elmah.MVC NuGet package but that is just used to set it up properly.
Yes, just noticed on nuget.org that Elmah.MVC is responsible for adding the HandleErrorAttribute:
Painless integration of ELMAH functionality into ASP.NET MVC
Application. Just drop the package inside your ASP.NET MVC application
and access /elmah URL. It will also install global HandleError filter,
that guarantees all unhandled errors are logged (even if customError
turned "On").
To disable ELMAH's HandleErrorAttribute add the following line to the appSettings section of your Web.Config file:
<!-- language: lang-xml -->
<add key="elmah.mvc.disableHandleErrorFilter" value="true" />

How to use the [HandleError] attribute in ASP.NET MVC?

I've got some custom error handling in my ASP.NET MVC site -> seems to be working fine.
I have an Error Controller, like this :-
/Controller/ErrorController.cs
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Unknown()
{
Response.StatusCode = (int)HttpStatusCode.InternalServerError;
return View("Error");
}
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult NotFound()
{
Response.StatusCode = (int)HttpStatusCode.NotFound;
return View("Error");
}
(i've also removed some other text, like the message to display, etc..)
The view is located like..
/Views/Error/Error.aspx
Now, i'm not sure how I can 'call' the Unknown method (above) when an error occurs. I thought I could use the [HandleError] attribute, but this only allows me to specify a View (and I'm don't think i'm doing that properly, cause the error url is really weird).
The [HandleError] attribute allows you to specify error pages for unhandled exceptions in your controller classes as opposed to the default custom error handling provided by ASP.NET.
I'd rather use traditional ASP.NET custom error handling unless in some controllers you want to ovveride default custom error behaviour.
<customErrors mode="On" defaultRedirect="~/Error/Unknown">
<error statusCode="404" redirect="~/Error/PageNotFound"></error>
<error statusCode="500" redirect="~/Error/Server"></error>
<error statusCode="503" redirect="~/Error/Server"></error>
</customErrors>

Resources