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
Related
I'm working on an ASP.NET MVC 5 website hosted as an Azure website and I have included ELMAH for error logging. All seems to work fine except I find numerous errors like this one:
A public action method '...' was not found on controller ...
These errors appear mostly when the request comes from a spider bot but they don't seem to produce any errors in the response. Also, all logged controller/action names are totally valid.
E.g. when I run a tool like deadlinkchecker.com on my site, I find hundreds of errors in ELMAH while the tool produces a report with 100% valid links. The path info in ELMAH is also correct.
Why am I seeing these errors?
If you add Head as well as Get then this error will go away and the spider will read your site correctly. As Nathan said, you could turn them off, but you're not fixing the issue really, just ignoring it and you might lose out if spiders cannot read your site 100%
This should work, get fiddler out and try it!
[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Head)]
public ActionResult Test()
{
return View();
}
I'm converting an application which uses MVC1 , Visul Studio 2008 and .Net 3.5 to an MVC3, .Net4 vs2010 application.
I got it basically working but I have to implement a few thing out to get it going along.
To force a logout when the user expired I used to have this code in Global.asax.cs
void Session_End(object sender, EventArgs e)
{
//Todo make this work with mvc3/.net4
//IFormsAuthentication FormsAuth = ObjectFactory.GetInstance<IFormsAuthentication>();
//FormsAuth.SignOut();
//Server.Transfer("~/AccessControl/AdLogin.aspx");
}
But it crashed after converting to .Net4/MVC3, looked like something it depended upon was not initialized yet.
I assume that I need to move it to a new event, any idea which? If not that how do I do this in the new environment.
It is an ugly solution but I wound up wrapping it in a structure map exception Try/Catch and swallowing the exception. I'd really like to find a better solution if anyone has one.
Cal-
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.
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
Unfortunately, I need to do this. I'm using ELMAH for my error log. Before I route to my error.aspx view, I have to grab the default ELMAH error log so I can log the exception. You used to be able to use
Elmah.ErrorLog.Default
However, this is now marked as obsolete. The compiler directs me to use the method
Elmah.ErrorLog.GetDefault(HttpContext context)
MVC's context is of type HttpContextBase, which enables us to mock it (YAY!). How can we deal with MVC-unaware libraries that require the old style HttpContext?
Try System.Web.HttpContext.Current. It should do the trick.
Gets HTTP-specific information about an individual HTTP request.
MSDN
this.HttpContext.ApplicationInstance.Context