Navish Rampal
Navish Rampal

Reputation: 482

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.

  1. I want to know is there any other better way to log errors.
  2. 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

Upvotes: 2

Views: 1034

Answers (4)

Miroslav Holec
Miroslav Holec

Reputation: 3217

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.

Upvotes: 1

Francis Shanahan
Francis Shanahan

Reputation: 2063

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/

  1. Setup Nuget.
  2. In vstudio -> View -> Other Windows -> Package Manager
  3. List-Package -filter elmah
  4. 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

Upvotes: 0

hunter
hunter

Reputation: 63502

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);            
    }
}

Upvotes: 2

Vishal
Vishal

Reputation: 12369

You could try elmah for error logging if that is what you are looking for.

Upvotes: 2

Related Questions