Created
November 7, 2015 10:25
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Query Kind="Program"> | |
<Reference><RuntimeDirectory>\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