Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I display the stack trace in a error view?

I have a website with customErrors enabled which directs me to a self made view. But in case of a server error I want to show the stack trace on the SorryPage view. Why? Because then the user can copy paste the error and send it to the IT Helpdesk of our firm and this way we know what went wrong and where the error came from.

I've seen this before but can't figure out anymore how to do this. Any help is much appreciated!

Controller:

public ActionResult SorryPage()
    {
        return View("Error", new ErrorModel(ErrorMessages.SorryPage, "General", "Home", Labels.GoBackToPortal));
    }

View:

@using ILVO.Web.Resources
@model ILVO.Web.Areas.General.Error._Error.ErrorModel
@{
ViewBag.Title = "Error pagina";
Layout = "~/Views/Layouts/_Layout.cshtml";
}

<h1>@Html.Label("Error", Labels.TitleErrorPage)</h1>
<br/>
<h2>@Model.ErrorMessage</h2>

<br/>
<div class="margin-bottom5px">
@Html.ActionLink(Model.Button, "Index", Model.ToController, new { Area = Model.ToArea }, new { @class = "button" })
</div>

Model:

public class ErrorModel
{
    public ErrorModel(string errorMessage, string toArea, string toController, string button)
    {
        ErrorMessage = errorMessage;
        ToArea = toArea;
        ToController = toController;
        Button = button;
    }

    public string ErrorMessage { get; set; }
    public string ToArea { get; set; }
    public string ToController { get; set; }
    public string Button { get; set; }
}

My cfg file:

<customErrors mode="RemoteOnly" xdt:Transform="Replace" defaultRedirect="~/General/Error/SorryPage">
  <error statusCode="500" redirect="~/General/Error/SorryPage"/>
  <error statusCode="404" redirect="~/General/Error/NotFound"/>
</customErrors>
like image 308
Nanou Ponette Avatar asked Mar 10 '26 23:03

Nanou Ponette


1 Answers

The easiest way to go about it is to disable Custom Errors. You'll get the classic "Yellow Screen of Death" which includes the exception message and stack trace. It's supposed to look ugly so that you don't show it to the world.

Inside the web.config under the <system.web> section add the following:

<customErrors mode="Off" />

You can also set this in the web.Debug.config so that it's only deployed to your test environments. You will need to have publishing or a build server set up and your test environment gets the Debug configuration. Note the use of the Insert transform.

<system.web>
  <customErrors mode="Off" xdt:Transform="Insert" />
</system.web>
like image 197
Greg Avatar answered Mar 12 '26 14:03

Greg