This seems simple enough. Elmah logs the errors, I see the error in the elmah.axd file, I then want to grab the data out into a new custom view.
I can't figure out how to grab the Detail Page out of Elma. Thus far all ive been able to do is ge开发者_如何学Ct the ErrorLogEntry I want but cannot figure out how to get the actual XML/Exception error the server spit out.
Elmah is set up normally. But I have:
<customErrors mode="On" defaultRedirect="~/Error/Index">
<error statusCode="404" redirect="~/Error/NotFound"></error>
</customErrors>
Which goes to my simple ErrorController.cs class:
public virtual ActionResult Index( )
{
Exception ex = null;
try
{
Elmah.ErrorLogDataSourceAdapter eldsa = new Elmah.ErrorLogDataSourceAdapter( );
ErrorLogEntry[] errors = eldsa.GetErrors( 0, 1 );
foreach ( ErrorLogEntry ele in errors )
{
ViewData[ "Description" ] += ele.Error.Message + "<br />";
ViewData[ "Description" ] += ele.Error.WebHostHtmlMessage + "<br />";
Elmah.ErrorLogPageFactory pf = new ErrorLogPageFactory( );
}
}
catch
{
ViewData[ "Description" ] = "An error occurred.";
}
ViewData[ "Title" ] = "Oops. We're sorry. An error occurred and we're on the case.";
//Email.SendException( ex, "Error in PRISM.NET" );
return View( );
}
The problem that occurs is that WebHostHtmlMessage is empty. So I cant get the Exception message.
I've been looking inside Elmah code all day and I can't figure out how to manually create a page just like it Elmah does when elmah.axd is called.
Assuming you store the data in the default table ELMAH_Error
SELECT AllXml
FROM ELMAH_Error
The solution ended up being as follows:
public virtual ActionResult Index( )
{
Exception ex = null;
try
{
Elmah.ErrorLogDataSourceAdapter eldsa = new Elmah.ErrorLogDataSourceAdapter( );
ErrorLogEntry[] errors = eldsa.GetErrors( 0, 1 );
foreach ( ErrorLogEntry ele in errors )
{
ErrorLogEntry thisError = ErrorLog.GetError(ele.Id);
ex = new Exception( thisError.Error.WebHostHtmlMessage );
}
}
catch
{
ViewData[ "Description" ] = "An error occurred.";
}
ViewData[ "Description" ] = "Oops. We're sorry. An error occurred and we're on the case.";
Email.SendException( ex, "Error in PRISM.NET" );
return View( );
}
精彩评论