user861768
user861768

Reputation: 175

MVC3 + ELMAH navigate to error detail?

My app: VS2010, MVC3, C#, latest ELMAH

Case: some errors happens (e.g. null reference), custom error page is shown (customErrors mode="On").

Task: allow admins (users in role Admins, ) view error detail from custom error page

Question: How to obtain/pass elmah error id to the custom error page view ?

Upd: Related answered question: ELMAH - Using custom error pages to collecting user feedback

Custom error page sample:

@model System.Web.Mvc.HandleErrorInfo
@{
  ViewBag.Title = "Error";
}
<h2>
  Error processing a request.
  @if (User.IsInRole(MyAppNamespace.Constants.ROLE_ADMIN))
  {
    // where to obtain ELMAH error id to navigate to error details ?
    // elmah/detail?id=291f5e83-5756-43bf-a889-07a548727da7
    <a href="@Url.Content("~/elmah")">View error details</a>
  }
</h2>

Upvotes: 2

Views: 759

Answers (2)

user861768
user861768

Reputation: 175

It is an event in Elmah ErrorLog module, that can be handled in Global.asax.cs:

protected void ErrorLog_Logged(object sender, ErrorLoggedEventArgs args)
{
    Session["ElmahId"] = args.Entry.Id;
}

Then we can use stored id to navigate to error (I am using Elmah.Mvc module that implements special controller instead of default elmah pages). In Error.cshtml:

@if (User.IsInRole(renweb.Constants.ROLE_ADMIN))
{
      <a href="@Url.Action("Detail","Elmah",new{id=@Session["ElmahId"]})">Error details</a>
}

Upvotes: 2

Vasile Laur
Vasile Laur

Reputation: 695

From what I know all Elmah errors are saved in a table called EMAH_Error.

So one thing you could do is to get the current date time when the error occurred and query the table for the records saved on that time give/take 1 minute. The query it will return the error saved with the appropriate time stamp that will also contain the ErrorId.

You can easily use EF or something else to create and entity ElmahError.

Upvotes: 0

Related Questions