ASP.NET MVC Global Error Handling with ELMAH - best practices - asp.net-mvc

I really would like to integrate ELMAH into my MVC4 application but I'm having a few issues:
if elmah redirects all errors to a specific error page, do I only need to return success (eg for update, delete, ...) to the user?
if elmah catches all errors do i need to handle errors (e.g. use try catchblocks) in my code at all?
is there a best practise example somewhere how to use ELMAH?

1) Elmah does do any redirects, so you have handle error pages yourself in web.config (hot ot do this you can look here)
2) You should. Elmah is for logging unhandled exceptions. You can log handled exceptions like this:
try
{
.....
}
catch(Exception ex)
{
Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
}
3) There is a nice article is on Scott Hanselman blog: ELMAH: Error Logging Modules and Handlers for ASP.NET (and MVC too!)

Related

MVC customErrors with redirect to subdomain error page

I have project with set of subdomain websites (.NET MVC).
e.g.
admin.example.com
weather.example.com
test.example.com
error.example.com
All this websites have same style and work as one big project, so my idea is to create one place for error pages as subdomain website (error.example.com). I think that in that case I will get one place where I will support my error pages (design, styling and other things).
My question is how to do that using MVC tools and web.config.
P.S.
Also it will be cool to have one place to log exceptions. For now I'm using elmah to log all exception in database.
It is not a usual thing, so I have a workaround.
I used Application_Error of Global.asax to track redirect.

Globally manage unexpected exception in asp.net MVC

I'm coming at the end of the development of my application.
Everything is working fine, but if once somebody got an unexpected exception, I would like to be able to:
Redirect him to a user-friendly page explaining that we got a problem.(Specifying a controller/action)
Log every information of the stack trace, current controller/action, parameter, session data, ...
What is the best way to do it with asp.net MVC?
EDIT
In complement of the great answer:
I integrated elmah like described here: http://joel.net/logging-errors-with-elmah-in-asp.net-mvc-3--part-1--setup
And then I specified some custom error page. So now I've all I need!
I would recommend you use "Elmah" http://code.google.com/p/elmah/
This package is a godsend - does exactly what is shown on the box, with close to ZERO changes to your source code.
It is also available via NuGet at http://nuget.org/packages/elmah
Use action filters.
http://blogs.msdn.com/b/gduthie/archive/2011/03/17/get-to-know-action-filters-in-asp-net-mvc-3-using-handleerror.aspx
You can catch errors in the global.asax. There is a method protected void Application_Error(object sender, EventArgs e) where you can do what ever you want.
Take a look at PostSharp, neat and easy to use. Starter version available to use after free registration. NuGet package also available http://nuget.org/packages/PostSharp

Exceptions in N2cms page

i'm using n2cms + asp.net mvc,
when the site is uploaded to a webserver, and an exception is thrown in the aspx page, the page appear blank, and there is nothing in the page, even if i turned off the CustomErrors in web.config
but when the site is running on my computer visual studio simply show me the exception,
is there a way to catch the exception in this situation?
This may be due to your hosting configuration. You could take a look at ELMAH for an easy way of logging exceptions.
You can handle all global errors in your Global.asax's method called Application_Error - http://msdn.microsoft.com/en-us/library/24395wz3.aspx . It will work for simple cases. But I strongly recommend to use ELMAH
N2CMS makes calls to SwallowExceptions(). This is why you are receiving a blank page instead of an exception. You could look for that method call and comment it.

Asp.Net MVC Error Handling & logging

I am trying to Catch exceptions and log it.
Presently I have written a Utility method and Passing the Exception to it in the catch block and finally logging the application using MS Enterprise Library 4.0.
I want to know is there any other better way to log errors.
Show exception message in the same view for some type of exceptions for rest navigate to error page.
If someone can help me on this!
Navish
You could try elmah for error logging if that is what you are looking for.
If you were rolling your own Exception logging you could create a base Controller class that overrides the OnException method
public class BaseController : Controller
{
protected override void OnException(ExceptionContext filterContext)
{
ExceptionPolicy.HandleException(filterContext.Exception, "MyPolicy");
base.OnException(filterContext);
}
}
Elmah
Elmah could be great option for you. It is easy to use, after installing nuget package is ready for use.
Custom logging with log4net etc.
If you want to log errors by your own, I recommend this example app: https://github.com/mholec/mvcexceptionhandler where is explained global logging with your favorite log tool (log4net, nlog, etc.)
Application Insights
Another and maybe the best way is to use Application Insights. Application Insights you can install quickly using NuGet and you will get great overview of your application errors, website availability and you can get more information about your users. This service is free.
I recommend ELMAH http://code.google.com/p/elmah/
You can get started very quickly with it by using Nuget to set it up.
http://nuget.codeplex.com/
Setup Nuget.
In vstudio ->
View -> Other Windows -> Package
Manager
List-Package -filter elmah
install-package elmah and
you're all set.
You can view the log just like trace.axd by going to http://mysite/elmah.axd
and manually throw errors to elmah like this:
try {
}
catch (Exception ex)
{
ErrorSignal.FromCurrentContext().Raise(ex);
}
hope that helps,
-fs

How can I get 404 to work with Elmah and the HandleErrorAttribute written for Elmah in asp.net mvc?

I have implemented Elmah in one of my ASP.NET MVC projects. In addition I did the following:
I implemented the ElmahHandleErrorAttribute written by Atif Aziz in this post
I added the attribute to all my controllers
I have setup Elmah to filter out 404 errors since I do not want to log those
I added an Error.aspx view in the Shared folder that I wish to use for all my errors.
My customErrors in the Web.Config are turned on and I have not specifed any error pages in it as I shouldn't have to.
In one of my controllers I have the following code:
if (model == null)
throw new HttpException((int)HttpStatusCode.NotFound, "Project not found");
I simply want to display a 404 if a record can not be found in the database. For some reason my Error.aspx page will not display for a 404 error. Instead it displays the generic asp.net "the resource can not be found" page. I have tried to remove the 404 filter setup in Elmah but that does not change anything.
The interesting part is if I change the error from NotFound to say InternalServerError the Error.aspx page shows up just fine.
Does anyone have any pointers as to why this is?
I have not used Elmah beyond creating a sample app - but maybe because a 404 is not application error? If my understanding is correct, a 404 error gets pushed back to IIS which in turn displays the error message you are seeing. Also, I believe that the web.coin
If i understand correctly, you may want to amend your approach and build use this instead or set the Response.StatusCode to 404 (Check out Jeff's answer in the linked question)

Resources