Reputation: 1402
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>
Upvotes: 3
Views: 4873
Reputation: 101150
"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".
That information can be used by a hacker. Why don't you just log the information or email it yourself?
You could also use my (free) service which takes care of everything for you: https://coderr.io
Upvotes: 1
Reputation: 434
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>
Upvotes: 2
Reputation: 11
You just can create Db for your errors and using filters add them into Db.
public class ExceptionDetail
{
public int Id { get; set; }
public string ExceptionMessage { get; set; }
public string ControllerName { get; set; }
public string ActionName { get; set; }
public string StackTrace { get; set; }
public DateTime Date { get; set; }
}
public class LoggerContext : DbContext
{
public LoggerContext() : base("DefaultConnection")
{
}
public DbSet<ExceptionDetail> ExceptionDetails { get; set; }
}
public class HomeController : Controller
{
LoggerContext db = new LoggerContext();
public ActionResult Index() // here we have our errors
{
return View(db.ExceptionDetails.ToList());
}
[ExceptionLogger]// our filter
public ActionResult Test(int id)// just random action
{
if (id > 3)
{
int[] mas = new int[2];
mas[6] = 4;
}
else if (id < 3)
{
throw new Exception("id cann`t be < 3");
}
else
{
throw new Exception("id must be a number");
}
return View();
}
}
public class ExceptionLoggerAttribute : FilterAttribute, IExceptionFilter
{
public void OnException(ExceptionContext filterContext)
{
ExceptionDetail exceptionDetail = new ExceptionDetail()
{
ExceptionMessage = filterContext.Exception.Message,
StackTrace = filterContext.Exception.StackTrace,
ControllerName = filterContext.RouteData.Values["controller"].ToString(),
ActionName = filterContext.RouteData.Values["action"].ToString(),
Date = DateTime.Now
};
using (LoggerContext db = new LoggerContext())
{
db.ExceptionDetails.Add(exceptionDetail);
db.SaveChanges();
}
filterContext.ExceptionHandled = true;
}
}
You can create View by right click on "Index" and select template = List, model = ExceptionDetail, all of the code will be generated automatically.
Upvotes: 1