iuliu.net
iuliu.net

Reputation: 7145

How can I see/use/log the exception when "customErrors mode="On""?

I have this in my web.config:

<customErrors mode="On" defaultRedirect="~\Error" >
</customErrors>

And a simple ErrorController:

public class ErrorController : Controller
{
  public ViewResult Index()
  {
    return View("Error");
  }
}

What I want to do is to log the error to some file, before returning the Error view. However I cannot seem to find an example on how to access the thrown exception inside my controller. Any help appreciated.

Upvotes: 1

Views: 176

Answers (1)

Hypnobrew
Hypnobrew

Reputation: 1140

I would suggest using something like an external library for this, for example Elmah. Elmah intercepts all exceptions and log them, so you should never have to register an exception by your self (If you don't want to log something very specific). Elmah can be configured to save to file, in memory, database etc.

Download the nuget-package. To configure Elmah to use file instead of in memory:

<elmah>
  <errorLog type="Elmah.XmlFileErrorLog, Elmah" logPath="~\App_Data\" />
</elmah>

If you want to use the dashboard to see the error logs instead of looking directly in your logfile, you should reach it on yourdomain/elmah.axd

Don't forget to activate authentication for the dashboard, before deploying to production.

Upvotes: 1

Related Questions