read
One final piece of information I wanted to add to my last blog post about setting up ELMAH for ASP.NET MVC is how to log Client Side/JavaScript errors with ELMAH.
To do that we need to:
- Because ELMAH works with exceptions we will need to define a *JavaScriptErrorException *to represent our client side errors in the logs:
using System;
namespace Triply.Extensions
{
[Serializable]
public class JavaScriptErrorException : Exception
{
public JavaScriptErrorException (string message) : base(message)
{
}
}
}
- Define a Controller Action to be invoked by the client side that takes in an error message:
using System;
using System.Web.Mvc;
using Elmah;
using Triply.Extensions;
namespace Triply.Controllers
{
public class HomeController : Controller
{
[HttpPost]
public void LogJavaScriptError(string message)
{
ErrorSignal.FromCurrentContext().Raise(new JavaScriptErrorException(message));
}
}
}
- Use an AJAX post with a message to log the error
function logError(details) {
$.post("/Home/LogJavaScriptError", { message: details });
}
Done!