Contextual logger injection for Autofac

TL;DR: install package AutofacSerilogIntegration, call builder.RegisterLogger(), and use constructor injection of ILogger to get source type tagging.

When I use Serilog, I more often than not embrace the static Log class and use a pattern like:

class Example
{
    readonly ILogger _log = Log.ForContext<Example>();

    public void Show()
    {
        _log.Information("Hello!");
    }
}

Notice in this example the ForContext<Example>() call. This creates a contextual logger that will tag all of the events created through it with the name of the specified class, in this case Example. Serilog does this by attaching a property called SourceContext.

SourceContext can be very useful for filtering. Here, using the Seq sink, we can see all events raised by PreadmissionController:

SourceContext

Logger Injection

When using an IoC container like Autofac, it’s quite common to inject a logger into the class using it. In this case our example would follow a slightly different pattern:

class Example
{
    readonly ILogger _log;

    public Example(ILogger log)
    {
        _log = log;
    }

    public void Show()
    {
        _log.Information("Hello!");
    }
}

Here, instead of calling ForContext<Example>(), the expectation is that the appropriate logger will be passed into the constructor by the caller.

Though I’ve tended to move away from this pattern in recent years (if for nothing else, just to cut down the number of parameters on constructors!) I’ve encountered lots of codebases that use this tactic and find it works well.

Unfortunately, despite founding both the Autofac and Serilog projects, when asked how to set this up I’ve had to point people to Google and newsgroup posts, and don’t think there’s been a comprehensive example online showing how it can work efficiently and reliably. So, finally, I’ve posted a working integration on GitHub and published it as the AutofacSerilogIntegration NuGet package. Here’s how it’s used.

Setting up the Autofac/Serilog Integration

As with all good stories, this one begins at the NuGet package manager command-line:

Install-Package AutofacSerilogIntegration

(This assumes you’ve installed both the Autofac and Serilog packages already.)

The first thing you should do in your application is configure a logger. I recommend assigning this to Log.Logger even if you don’t intend to use the static Log class, just in case any calls to the static logger slip in accidentally.

Log.Logger = new LoggerConfiguration()
    .WriteTo.ColoredConsole()
    .CreateLogger();

Next up, where your Autofac ContainerBuilder is configured, call RegisterLogger(). This is an extension method in the AutofacSerilogIntegration namespace..

var builder = new ContainerBuilder();
builder.RegisterLogger();

And, that’s everything. For components created by the container, parameters of type ILogger will now be fulfilled using a logger instance tagged with the correct type.

The RegisterLogger() method accepts a couple of parameters – an ILogger can be specified if you want to use a root logger separate from the static one, and property injection can be optionally enabled.

Closing thoughts…

This is a shiny new slice of code and there may still be scenarios it doesn’t handle so well. The beauty of getting it to GitHub is that over time it can be improved through use – pull requests appreciated! I hope it saves you some searching next time you hit File > New Project.

Autofac update 2.6.1 published

The eagle-eyed may have already spotted Autofac 2.6.1 lurking in the NuGet feed.

The last release, the venerable 2.5.2, served us very well (maturity is a feature!) and this release adds a little more polish, without any known breaking changes.

  • The ASP.NET MVC integration package gets a bug fix for recursive views, UrlHelper and RequestContext registered by AutofacWebTypesModule, and an explicit method for registering model binders.
  • The WCF integration package gets much improved support for services with InstanceContextMode.Single.
  • A new helper InstancePerOwned<T>() makes it easier to control lifetime when using Owned<T>.
  • Autofac can now be used with .NET Portable Class Libraries (download only on the project site. (The Windows Phone 7 build now uses this configuration too.)
  • Autofac.dll is now clear of any issues found by SecAnnotate.exe.

Extra thanks to Alex and Travis for the majority of these improvements.

Autofac 2.5 Released

Autofac 2.5 is now available on the project site and via NuGet. The new version includes a range of great contributions.

After incubation as an external project, Windows Phone 7 now joins ranks of officially-supported targets (just install the Autofac package via NuGet and you’re up and running!) For some tips on getting started see Brendan Kowitz’s notes.

The new release is highly-compatible with Autofac 2.4 (best used with the VS2010 compiler when targeting .NET 3.5.)

Included changes are:

  • Issue 329 – types are filtered by compatibilty before invoking key mapping functions when scanning assemblies
  • RegisterAssemblyTypes(...).WithMetadataFrom<T>() simplifies extraction of metadata from attribute properties
  • Module.ThisAssembly simplifies implementation of modules that scan their own assembly
  • Issue 333 – enable RegisterAssemblyTypes(...).PreserveExistingDefaults()
  • Windows Phone 7 and 7.1 support
  • Issue 312 – allow ContainerBuilder.Update() on the ComponentRegistry of any
    ILifetimeScope without affecting the parent scope
  • Issue 327 – allow autowiring of enum properties
  • Issue 319 – support list and dictionary literals in XML configuration
  • Issue 330 – corrections to generic type constraint checking where the constraint type is an interface
  • Issue 331 – fix build warnings and runtime issues using Moq integration
  • Introduced ContainerBuildOptions to control invocation of IStartable.Start() under unit tests
  • Container.Empty is now a property rather than a field (protection from inadvertent updates
  • Removed broken/obsolete examples from source tree (better examples are available elsewhere online)
  • Host configuration features for AutofacHostFactory in WCF integration
  • ComponentRegistry.Register() is now safe for use under multiple threads
  • Issue 227 – breaking change additional options (and enum rather than boolean parameter) for PropertiesAutowired()
  • Additional web abstractions are now registered by the MVC3 AutofacWebTypesModule
  • Issue 311 – fixed potential lifetime issues when injecting into MVC filter attributes
  • Tightened up behavior when correct constructor to use under reflection activator is ambiguous (now throws rather than choosing nondeterministically)
  • Issue 309 – fixed some issues in TypeExtensions.IsClosedTypeOf()
  • Improved exception messages

Many thanks to the Autofac project team and collaborators across the web for making this release happen!

Designing a Short Course on IoC: Day 5

The last of this curious series of journal entries today. As I mentioned previously, today is going to be about finalising the end-to-end flow of topics.

I think the first session – Fundamentals – is finished. I’m really excited and even a little nervous about putting together a new container on the fly! As a way of bedding down the terminology, mechanics and challenges, I think it is going to be great.

The layout for the remainder of the day still needs work, so I’d better get stuck into it! Those who’ve already signed up, I’ll see you soon – if you haven’t put your name down yet for either Brisbane, Sydney or Melbourne, this is probably the last reminder you’ll get from me.

Designing a Short Course on IoC: Day 4

A brief update today. Yesterday I think I covered everything I need to as far as demo app material goes, but tidying up, styling and (egads!) unit testing will have to wait for a rainy day.

I spent a fair bit of time yesterday looking at how I can use Whitebox to give an x-ray view of the composition process as the application is built. The information it provides is great – often revealing little issues that would otherwise go unnoticed – but the usability leaves me deeply unsatisfied. Still, it is a start, and by chipping away at it things are improving. Just having a simple view of component details is an improvement:

Whitebox component detail view

Today I’m concentrating on the flow of the day. I still have some questions to answer around the introductory session – how theoretical do we want to get? Does it make sense to examine the problems of composition in OO languages, or can we take that as a given for people interested in learning more about IoC? My tendency in the past would have been to go into depth about this, but by starting with “you’re going to use IoC – here’s how to do it” I think the practical side of the Dev Day will be emphasised.

Designing a Short Course on IoC: Day 3

Is it just me, or am I slowing down? Yesterday was a busy day of programming, but there’s still a long way to go before this application is going to cut the mustard as an example for the course.

In the end, I went with the idea of a timesheeting application. Simple material keeps out of the way, and to be honest, there is so little logic or fancy presentation in this app that it could be changed from timesheets to another example in a relatively short space of time if need be.

Up and running, we have:

  • A basic MVC3 app layout
  • IoC set up to create controllers
  • Data access with NHibernate
  • Transaction management integrated with MVC
  • Logging with the venerable log4net
  • A skeleton for ‘pick-me-up’ domain events

It is the last item that I’ll be working on in depth today. ‘Pick-me-up’ domain events integrate domain models with external services in a way that plays better with IoC than the thread-static DomainEvents.Raise() pattern commonly seen elsewhere.

Dispatching events to handlers is also a good example of the ‘generic handler’ pattern that’s very widely used, and registering handlers will provide some further examples for convention-based/scanning registration.

To complete the day I’m aiming to polish up the last details of the basic MVC site (for example, error handling) and bed down an example of processing work in a background process to use in the “Resources and Units of Work” session. That leaves tomorrow and Friday for more detailed work on slides, some code samples for the “Containers” session, and some reworking of the example to fit into the flow of the course.

Designing a Short Course on IoC: Day 2

So, yesterday, a lot of thinking. Today it’s time to start fleshing out some of the things we’ll need.

The course is going to be split into four sections, to match the natural division of the day by morning tea, lunch and afternoon tea:

  1. Fundamentals
  2. Effective IoC
  3. Resources and Units of Work
  4. Containers and Tooling

Session 1: Fundamentals

First up, we’ll set the theme of the day with some collaborative development. To introduce the vocabulary and concepts of IoC, we’ll write a simple IoC container in C# and integrate it into an ASP.NET MVC application. There are plenty of interesting design issues along the way that we’ll come back to in our discussion of containers and tooling.

Session 2: Effective IoC

The second session will introduce a ‘real’ IoC container. The focus will be on techniques and features that allow IoC to support large, complex applications in the real world. We’ll touch on all of the typical components of MVC web applications as we migrate from the simple container we built ourselves, but we’ll pay special attention to the integration between IoC and domain models because of the challenges and opportunities they present.

Session 3: Resources and Units of Work

Lifetime, AKA “resource management” can be the toughest aspect of IoC to master. We’ll take an excursion outside of the comfort of the web tier, to develop a thorough understanding of the issues involved and their solutions.

Although it is far from being ready for prime time, Whitebox will be a great way to explore the runtime behaviour of lifetime scenarios, so sometime over the coming week I’ll look at filling remaining gaps there.

Session 4: Containers and Tooling

To round out the day, we’ll use the last session to take a leisurely tour of the sights, sounds and smells of the .NET IoC ecosystem. Choosing an IoC container can be tricky business, especially when the easiest metrics to grasp, for example throughput performance, are often misleading or irrelevant. In addition to looking at some of the popular offerings and their relative strengths, I think this is where a discussion of MEF and its role will fit in nicely.

Session 4 will also give us an opportunity to examine debugging and diagnostics issues.

Today

So, what do you think?

Today’s focus for me will be to take a first cut at building the example application. I’m going to leave the ‘simple container’ design task for the event, so that we can truly do it on the fly! I want the day to be heavy on code and light on slides, so dressing up a slide deck with some guiding material will wait for later in the week.

Choosing what kind of application to build is tricky. I want attendees to be able to take something away, ideally a sample that isn’t too far from production readiness. In that respect, simpler will be better, though it can be hard to get enough breadth out of something like a ‘to-do’ or ‘timesheeting’ application. There’s some high-quality code on the web that might be reused for this purpose (e.g. FunnelWeb) so building something that mimics an existing open source application has advantages.

I’ll update you tomorrow on where things land.

Designing a Short Course on IoC: Day 1

Today’s the start of a really exciting week for me!

I’ve been working with Inversion of Control (IoC) Containers and related technologies since picking up Castle Windsor in 2006, actively developed Autofac for much of that time, and helped shape the first release of the Managed Extensibility Framework. In that period IoC has grown and truly blossomed in the .NET world: from its “enterprise Java” roots we’ve collectively created something that plays to the unique strengths of .NET and C#. Rather than being viewed as an esoteric fringe technology, as it was just a few years ago, IoC is firmly in the .NET mainstream and is increasingly being baked into the platform itself.

Along the way there have been many difficult questions posed, and I’ve had to ask and answer my share. It can be easy to go wrong with IoC – remember the “sharp tools” analogy of the ALT.NET movement? – especially when finding the right approach to IoC in a new situation can mean finding several wrong ways first!

Last year at TechEd Australia I presented a breakout session on IoC in .NET. Though the feedback I received was positive, I was left feeling that an hour was too little time to get many of the concepts across in enough depth. So, this year courtesy of Readify I’m going to be presenting a short course on IoC from the ground, up.

The course will be a full day, with dates in May, 2011 set for:

The unifying theme will be ASP.NET MVC, but don’t be discouraged if this is not your primary platform. Much of what we cover will be independent of MVC and even of web programming in general. If you’re unfamiliar with ASP.NET MVC then it might help to brush up on that area if you want to keep up with some of the examples.

In terms of level of technical difficulty, I hope there will be something for everyone. You’ll get the most out of the day if you’re a confident .NET developer with beginner-to-intermediate experience with IoC, or if you’re an experienced user of IoC looking for broader and deeper perspectives on its use.

This week I’m preparing material for the course, and while I have a fair idea of the ground to cover, I’m interested in your ideas. I’ll be posting a progress update each day to keep you informed of where things are heading. I hope in the process I can convince you to come along, and bring your team!

Autofac 2.4.5 Release

More than a month has passed since the last patch release of Autofac 2.4, and enough has changed in trunk to warrant a new one. You can download Autofac 2.4.5 from the project site, or preferably, update your NuGet packages within Visual Studio.

In this release you’ll find:

  • Several bug fixes and improved exception messages
  • Travis and Alex have been busy making the Multi-tenant contrib package work better with the new ASP.NET MVC3 integration
  • The awkward but very useful IContainerAwareComponent introduced in 2.4 has been deprecated and morphed into a much friendlier and more useful Startable implementation
  • Autofac now consistently throws DependencyResolutionException (or a subclass) whenever fatal errors occur during composition

Please feel free to drop in and share your experiences on the discussion forum. For help with using Autofac, including new features, the preferred medium is Stack Overflow.

Autofac 2.4 is here – come and “NuGet” it!

The latest release of the Autofac IoC container is now available on the project site and via NuGet.

ASP.NET MVC Integration Changes

The best thing in the new release, and possibly since sliced bread, is the new Autofac ASP.NET MVC integration. Gone are special interfaces on HttpApplication, gone are the Web.config file edits, static properties in the application class and multiple different extension points to configure. It couldn’t get any simpler than this.

Here’s how you create a new Autofac-enabled ASP.NET MVC3 project:

1. Create a new MVC3 web application

2. Install the Autofac.Mvc3 Package

3. Set up the Container and DependencyResolver

protected void Application_Start()
{
    var builder = new ContainerBuilder();
    builder.RegisterControllers(typeof(MvcApplication).Assembly);
    var container = builder.Build();
    DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

    // Other MVC setup...

That’s it!

That’s all you need to do to enable dependency injection into your controllers. There’s a lot more you can do with Autofac and ASP.NET MVC3 – I highly recommend checking out some of Alex’s recent posts on the subject.

The Autofac ASP.NET MVC integration now targets ASP.NET MVC version 3 exclusively. Don’t worry if you’re unable to move to MVC3 right away, see the “Using Autofac 2.4 with ASP.NET MVC2” below.

Complete NuGet Packages

With Autofac 2.4, the recommended way to add Autofac and its sub-features to your applications is to use the NuGet package manager. You can see some of the available packages in the screen shot at step 2 above. Just search for “Autofac” and you’re away.

In the previous Autofac NuGet package, we included the MEF and WCF integrations by default. To keep clutter away, these are now in their own packages Autofac.Mef and Autofac.Wcf respectively.

Improved Decorator Support

The new and improved decorator support makes it much easier to wire up generic decorators with Autofac, and also provides a nice boost when using decorators in other scenarios.

Removed Older Silverlight Versions from Supported Builds

To keep the maintenance burden of the project as light as practical, it is occasionally necessary to discontinue ‘official’ support for older framework versions. In Autofac 2.4, the Silverlight 3 configuration is no longer available. Users of Silverlight 3 should continue to run the Autofac 2.3 builds (bug fixes will be provided for these whenever necessary.)

Events and Interfaces to Support Tracing

Like all IoC containers, Autofac’s internals can be somewhat opaque to follow at runtime. For the occasions that diagnostics are necessary, Autofac 2.4 supports more events that can be used to trace the operations of the container. As an example, ILifetimeScope now supports LifetimeScopeBeginning, ResolveOperationBeginning and other related extension points.

It is intended that these will be used more by Autofac integration and tooling developers than in general application development.

Other Release Notes

The community has been busy as usual, and you’ll find quite a few other enhancements in this release.

  • .AsImplementedInterfaces() is now supported on non-scanning registrations
  • IDisposable is no longer considered a service by .AsImplementedInterfaces()
  • Additional eager checks for generic type/service compatibility at registration time
  • An additional .WithParameter() overload has been added, accepting predicate and value accessor like ResolvedParameter
  • Extension methods for common predicates on System.Type are now public for use with scanning, e.g. Except(t => t.IsClosedTypeOf(x))
  • MVC ExtensibleActionInvoker does not inject action method parameters by default
  • ILifetimeScope rather than IContainer is now accepted by ServiceHostBase (WCF integration)
  • AutofacInstanceContext has been made public so that current scope can be retrieved when needed (WCF integration)
  • Bug fixes and enhancements in AutofacContrib.Multitenant and AutofacContrib.Attributed
  • Dependencies upgraded e.g. Castle Core and NHibernate
  • Fixed bug #288 – resolve all failing in customised lifetime scopes when one component supports multiple interfaces

Using Autofac 2.4 with ASP.NET MVC2

If you’re still using ASP.NET MVC2 in your application, the simplest course of action is to continue using the Autofac 2.3 release series and the appropriate integration bundled with that, until you’re able to upgrade wholesale to ASP.NET MVC3.

If you would like to use Autofac 2.4 with ASP.NET MVC, there is an updated Autofac.Mvc2 NuGet package now on the feed. If you’re already using the MVC2 NuGet package, update the package and you should be fine. If not, first remove all Autofac*.dll references from your project and then install the Autofac.Mvc2 package using the “Add Library Reference” dialog.

Edit: You need to add assembly binding redirects in order for the MVC2 package to work. The appropriate redirects are specified below (you may need to update them with the current Autofac version number) or you can use the commands bundled with NuGet.

<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly>
      <assemblyIdentity name="Autofac" publicKeyToken="17863af14b0044da" />
      <bindingRedirect oldVersion="2.3.2.632" newVersion="2.4.3.700" />
    </dependentAssembly>
    <dependentAssembly>
      <assemblyIdentity name="Autofac.Integration.Web" publicKeyToken="17863af14b0044da" />
      <bindingRedirect oldVersion="2.3.2.632" newVersion="2.4.3.700" />
    </dependentAssembly>
  </assemblyBinding>
</runtime>

Summing Up

I hope that with this release, Autofac is now easier to get and set up than ever before. It has once again been a team effort – thanks especially to the contributors and everyone who has submitted issue reports or patches.