.netlogginglog4netcastleetw-eventsource

Migrating to semantic logging with Castle ILogger and log4net


What's the best way to start using the new .NET 4.5 EventSource class for logging for an application that's currently using Castle's Logging Facility combined with log4net.

Initially I thought that it might be possibly to extend Castle's ILogger interface to support the richer semantic-style logging that EventSource facilitates (Issue raised), but now I'm not so sure whether they could really work that well together.

I note that the Semantic Logging Application Block provides one implementation of using EventSource, but that's of course not using ILogger.


Solution

  • If you want to move to semantic logging in general, not just the SLAB implementation, you might find it easier to move to Serilog. (Disclaimer - I started the project, though it's now well-established).

    Serilog implements the same semantic logging concepts as EventSource and friends, but uses the classic "ILogger"-style API you get from Castle.

    log.Information("The cart contains {CartSize} items", cart.Count);
    

    This example associates a structured property called CartSize with the event, just as you'd get passing a parameter to an EventSource method. (You can even use classic {0} numeric format specifiers, and still get structured events with numbered, rather than named, properties!)

    Because the APIs are compatible, moving existing apps from one style of ILogger to another is less friction than porting everything to EventSources. There are several ways to approach it, if you decide its an option I'll keep an eye out for any follow-up questions.