1

I currently display a custom-styled error message to the users, that I've built following this post. This works fine, but I'm wondering how I should actually log these errors in the log-file. Currently we've implemented log4net for logging.

Here's my template:

@model System.Web.Mvc.HandleErrorInfo

<div class="span3"></div>
<div class="span5">
    <div class="well">
        <h3>Oops!</h3>
        <div class="alert alert-info alert-block">
            <strong>An error has occurred</strong>
            <p>
                We have logged the incident for you.
            Should it continue to occur, please contact support via one of the following options
            </p>
            <br />
            <p class="pull-right"><a href="mailto:support@company.com">support@company.com</a></p>
            <address>
                <strong>Company Inc.</strong><br>
                Address 3<br>
                0055 Somewhere, Country<br>
                <abbr title="Phone">P:</abbr>
                +47 999 99 999
            </address>
        </div>

        <div class="alert alert-error alert-block">
            <strong>Exception details (@Model.ControllerName / @Model.ActionName)</strong>
            <p>
                @Model.Exception.Message
            </p>
        </div>
    </div>
</div>

Web.config:

<customErrors mode="On">
  <error statusCode="404" redirect="~/Error404/Index" />
</customErrors>

Note that the Error404 view has its own controller and resides in its own view-folder, whereas my Error.cshtml view does not - it resides in the shared folder, together with my layouts. Any help would be appreciated :)

Community
  • 1
  • 1
Nicklas Pouey-Winger
  • 3,023
  • 6
  • 43
  • 75

1 Answers1

1

Take a look at ELMAH.

I would say continue logging with log4net. There are already log4net appenders that will log to a file or database. The only issue here is that to log it, you have to know it happened.

If you add ELMAH to your project it will capture any unhandled exceptions and log those to file/db/email/whatever you configure.

This project had nuget packages for easy integration. I can't say enough about it, I use it in every web project I write.

Fran
  • 6,440
  • 1
  • 23
  • 35