Skip to content

Instantly share code, notes, and snippets.

@atifaziz
Created November 7, 2015 10:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atifaziz/ffe8c304d0ba556784d2 to your computer and use it in GitHub Desktop.
Save atifaziz/ffe8c304d0ba556784d2 to your computer and use it in GitHub Desktop.
Demo showing how to log Jayrock JSON-RPC service method errors in ELMAH
<Query Kind="Program">
<Reference>&lt;RuntimeDirectory&gt;\System.Web.dll</Reference>
<NuGetReference>elmah</NuGetReference>
<NuGetReference>jayrock</NuGetReference>
<Namespace>Elmah</Namespace>
<Namespace>Jayrock</Namespace>
<Namespace>Jayrock.Json</Namespace>
<Namespace>Jayrock.Json.Conversion</Namespace>
<Namespace>Jayrock.JsonRpc</Namespace>
<Namespace>Jayrock.Services</Namespace>
</Query>
void Main()
{
// During initialization, you register your dispatcher subclass
// like below and Jayrock will use it instead. In ASP.NET, this
// would, e.g., be done during the Application_Start event in
// Global.asax.
JsonRpcDispatcherFactory.Current = svc => new MyJsonRpcDispatcher(svc);
var service = new MySerivce();
var dispatcher = JsonRpcDispatcherFactory.CreateDispatcher(service);
// Call the foo method that throws...
var response = dispatcher.Process("{ id: 1, method: foo }");
Console.WriteLine(response);
// See what ELMAH logged...
var errors = new ArrayList();
ErrorLog.GetDefault(null).GetErrors(0, 10, errors);
errors.Dump();
}
class MySerivce : JsonRpcService
{
[JsonRpcMethod]
public void Foo() { throw new NotImplementedException(); }
}
class MyJsonRpcDispatcher : JsonRpcDispatcher
{
public MyJsonRpcDispatcher(IService service) :
base(service) {}
protected override object OnError(Exception e, IDictionary request)
{
// In ASP.NET better to signal the error like this:
// ErrorSignal.FromCurrentContext().Raise(e, HttpContext.Current)
// but here we log directly...
ErrorLog.GetDefault(null).Log(new Error(e));
return base.OnError(e, request);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment