Announcing libraries for C# and F# to make it easier to integrate with Sentry

Here at Gamesys social team, we’re rethink­ing our cur­rent approach to log­ging in gen­er­al, from both serv­er and client’s per­spec­tive. Hav­ing looked at many dif­fer­ent alter­na­tives (it was a lit­tle hard to imag­ine how crowd­ed a space log aggre­ga­tion and visu­al­iza­tion is..) one of the ser­vices which we have decid­ed to exper­i­ment with is Sen­try.

Sen­try is a fair­ly sim­ple ser­vice, with an easy to use API and straight for­ward to inte­grate with, espe­cial­ly if you already have a client library (the Sen­try doc refers to them them as Ravens) for your lan­guage of choice. On the .Net side of things, you have a lit­tle library called SharpRaven.

As for inte­gra­tion, using cus­tom log4net appen­der such as this one is obvi­ous­ly a good way to go, but you still need to imple­ment the try-catch-log pat­tern every­where though, unless you’re hap­py for these excep­tions to bub­ble all the way up to the app domain and catch them there. And when I see imple­men­ta­tion pat­terns I see oppor­tu­ni­ties to auto­mate them with Post­Sharp!

C# custom attributes

If you grab the SharpRaven-Con­trib pack­age from Nuget you’ll have access to a pair of cus­tom attrib­ut­es – Raven­Lo­gEx­cep­tion and Raven­Lo­gEx­e­cu­tion­TimeAt­tribute – when you open the SharpRaven name­space. For exam­ple,

The attrib­ut­es does what they say on the tin, Raven­Lo­gEx­cep­tion cap­tures and logs excep­tion infor­ma­tion as errors to Sen­try whilst Raven­Lo­gEx­e­cu­tion­Time mon­i­tors exe­cu­tion time of your meth­ods and logs any method exe­cu­tion that took longer than your giv­en thresh­old as warn­ings to Sen­try.

For F# how­ev­er, whilst the attrib­ut­es would still work for meth­ods, chances are you will be spend­ing most of your time work­ing and com­pos­ing func­tions instead and these attrib­ut­es won’t help you there. So for F# I decid­ed to do some­thing slight­ly dif­fer­ent.

F# workflows

Thank­ful­ly, in F#, we have com­pu­ta­tion expres­sions* (aka work­flows) which already pow­er lan­guage fea­tures such as async work­flows and sequence com­pre­hen­sions.

Using the work­flows defined in the SharpRaven-Con­tribFs pack­age you can cre­ate blocks of code where:

  • any unhan­dled excep­tions are logged as Error in Sen­try
  • if the block of code takes longer than the spec­i­fied thresh­old to exe­cute, it’ll be logged as a warn­ing in Sen­try

and your code remains unchanged, you sim­ply wrap them in { }:

Of course, you can also just cre­ate wrap­per func­tions to achieve the same results, but I find that using work­flows in this case makes for more read­able code. Anoth­er good alter­na­tive is to use a Maybe mon­ad, which I won’t go into too much detail here as Scott Wlaschin has a great expla­na­tion for this already.

 

As always, the source code for both libraries are avail­able on github, and if you find any issues feel free to report them via the issues page.

 

* if you’re inter­est­ed in learn­ing more about com­pu­ta­tion expres­sions, I high­ly rec­om­mend Scott Wlaschin’s series on his F# for Fun and Prof­it blog, it’s by far the most com­pre­hen­sive and easy to under­stand set of arti­cles I have seen.

 

Links