In my previous post I mentioned that I have been using both Open Rasta and ELMAH so this post explains how to make them play nicely together. To get started you will need to set up both libraries in your project as per their documentation, then follow along below.
Open Rasta actually attempts to handle its errors rather than letting them fall out to ASP.Net’s normal unhandled exception events. This means that ELMAH never hears about them and doesn’t log them by default. Fortunately both Open Rasta and ELMAH provide easy hooks for us to easily set this up.
Manually Logging With ELMAH
Sometimes you have exceptions which don’t represent a complete application meltdown but you would like to know about anyway. ELMAH has an API set up for just such an occasion:
ErrorSignal
.FromCurrentContext()
.Raise(ex);
This simple call grabs the current ELMAH setup from HttpContext and passes it any exception we want.
Injecting A Logger Into Open Rasta
Open Rasta logs exceptions and lots of other useful information while it is running. By default this information is logged to .Net trace output but we’re going to replace that. Here’s a simple logger that will do the job:
public class ElmahLogger : OpenRasta.Diagnostics.ILogger
{
public void WriteException(Exception ex)
{
ErrorSignal
.FromCurrentContext()
.Raise(ex);
}
public void WriteDebug(string message, params object[] f) { }
public void WriteWarning(string message, params object[] f) { }
public void WriteError(string message, params object[] f) { }
public void WriteInfo(string message, params object[] f) { }
public class OperationContext : IDisposable
{
public void Dispose() { }
}
public IDisposable Operation(object source, string name)
{
return new OperationContext();
}
}
The important lines are in the WriteException method at the top. You can use the rest of the methods to gather a full log from Open Rasta’s pipeline to include in the exception details if you want.
Since Open Rasta has a simple dependency injection container built right in, telling it to use our logger simply requires adding these lines to your normal configuration:
ResourceSpace.Uses
.CustomDependency<ILogger, ElmahLogger>
(DependencyLifetime.Singleton);
That wasn’t so bad now was it?
06/09/2009 10:48 AM (UTC -07:00)
It’s been a while so I thought I’d start my return to blogging on a happy note. Here’s a few products I’ve used lately that have made me happy.
Open Rasta
I’ve tried a couple of times to implement a simple REST API using WCF lately and it always frustrates me. It’s not really hard, it’s just harder than it needs to be. That’s why I ended up looking for alternatives.
OpenRasta is a resource-oriented framework for .NET enabling easy ReST-ful development of web sites and services.
If you’re a fan of simple convention based frameworks then this is one to keep an eye on. The framework is built around a customisable pipeline which allows you to easily drop in your own components using a simple fluent API and built in dependency injection.
It’s currently in it’s second beta and is still a bit rough around the edges but I had no problems putting together a simple API and customising it to suit. The documentation is still lacking but the driver behind the project, Sebastien Lambla, is active on the mailing list and has been a big help getting me off the ground.
The best thing about Open Rasta? It’s distributed under the very friendly MIT license.
ELMAH
I look after a lot of different ASP.Net web sites sites so it can be weeks before I discover problems with them on my own. ELMAH (Error Logging Modules and Handlers), has been a great tool for quickly and consistently setting up basic error handling.
Personally I just use it to send all errors as emails to myself but there’s all sorts of features to keep you busy if you have larger requirements. My favourite feature is the lovingly preserved Yellow Screen Of Death that arrives in my inbox full of details about the failed request.
Composite WPF (Prism)
Why do I like the Composite WPF library so much? Mostly because I am unfortunate enough that I have to work one of that teams previous products, the Composite Application Block. This time around the team have managed to present their concepts in a clear and understandable manner that doesn’t make my head hurt.
In particular the complex work items system of previous versions is gone and replaced with a simple application of their IoC container, Unity. Whether or not you actually need such a framework is up to you to decide but if you have been scared off by CAB in the past don’t be afraid to take another look.
Keep smiling until next time!
03/09/2009 02:29 AM (UTC -07:00)