Ivan's Brain Dump




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:

  1. 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)
		{
		}
	}
}
  1. 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));
		}
	}
}
  1. Use an AJAX post with a message to log the error
function logError(details) {
    $.post("/Home/LogJavaScriptError", { message: details });
}

Done!

Blog Logo

Ivan Zlatev


Published

Image

Ivan's Brain Dump

Back to Overview