• Capturing Log Output in Tests with XUnit 2

    02 Jun 2015

    xunit 2.x now enables parallel testing by default. According to the docs, using console to output messages is no longer viable:

    When xUnit.net v2 shipped with parallelization turned on by default, this output capture mechanism was no longer appropriate; it is impossible to know which of the many tests that could be running in parallel were responsible for writing to those shared resources.

    The recommend approach is now to take a dependency on ITestOutputHelper on your test class.

    But what if you are using a library with logging support, perhaps a 3rd party one, and you want to capture it's log output that is related to your test?

    Because logging is considered a cross-cutting concern, the typical usage is to declare a logger as a static shared resource in a class:

    public class Foo
    {
        private static readonly ILog s_logger = LogProvider.For<Foo>();
    
        public void Bar(string message)
        {
            s_logger.Info(message);
        }
    }
    

    The issue here is that if this class is used in a concurrent way, it's log output will be interleaved, just in the same way as using console in tests.

    Read more...


  • Stepping down as NEventStore coordinator

    I can't remember exactly when it happend, but for the last few years at least, I have been the core maintainer / coordinator of NEventStore. Built by Jonathan Oliver and just known as EventStore at the time (not related to the other EventStore), it provided me and many people with a really easy way to get up and running with event sourcing. For unknown reasons the core maintainers back then stepped back and since I was heavily invested in it, I offered to take it over.

    Since then it has gone through a rename, 2 major versions and a bunch of minor releases. In that process I learned a lot about running an OSS project and connected with a lot of cool and smart people. This is something that I would highly recommend to any developer looking to get into OSS.

    In the last 12 months or so I haven't been responsive enough to the community, issues, pull-requests, google group etc., and it's time to make a statement. While I'm fairly stretched time wise, the core reason is that as I've as learned to build event sourced systems over the years, NEventStore's current design no longer works for me. While I'm using GetEventStore in some scenarios, I still, and will continue to, have a need for SQL backed event stores. How I'd like to see and interact with such stores is significantly different to how NEventStore currently works. I could mould NEventStore into how I'd like it to be but then the changes would very likely alienate people and break their stuff. Thus it's best that I head off in my own direction.

    If you are vested into NEventStore and would like to take over and run a popular OSS project (nearly 800 stars and 250 forks on github, and 10's of thousands of nuget downloads), please reach out to me :)

    Read more...


  • Testing OWIN applications with HttpClient and OwinHttpMessageHandler

    Let's take the simplest, littlest, lowest level OWIN app:

    Func<IDictionary<string, object>, Task> appFunc = env =>
    {
        env["owin.ResponseStatusCode"] = 200;
        env["owin.ResponseReasonPhrase"] = "OK";
        return Task.FromResult(0);
    }
    

    (In reality you'll probably use Microsoft.Owin or LibOwin to get nice typed wrapper around the OWIN environment dictionary instead of using keys like this.)

    How do you test this?

    Well you could create your own environment dictionary, invoke appFunc directly and the assert the dictionary.

    [Fact]
    public async Task Should_get_OK()
    {
        var env = new Dictionary<string, object>()
    
        await appFunc(env);
    
        env["owin.ResponseStatusCode"].Should.Be(200);
        env["owin.ResponseReasonPhrase"].Should.Be(OK);
    }
    

    While this would work it, it's not particularly pretty and it'll get messy fairly quickly as you may need to assert cookies, dealing with chunked responses, doing multiple requests (e.g. a login post-redirect-get), etc.

    Wouldn't it be nicer to use HttpClient and leverage it's richer API? It would also better represent an actual real-world client. Something like this is desirable:

    [Fact]
    public async Task Should_get_OK()
    {
        using(var client = new HttpClient())
        {
            var response = await client.GetAsync("http://example.com");
    
            response.Status.Should().Be(HttpStatusCode.OK)
        }
    }
    

    Read more...


  • Introducing LibLog

    14 Apr 2015

    LibLog (Library Logging) has actually been baking for a few years now, since before 2011 if I recall correctly, and the current version is already at 4.2.1. It's fair to say it's been battle tested at this point.

    As a library developer you will often want to your library to support logging. The first and easiest route is to simply take a dependency on a specific logging framework. If you work in a small company / team and ship to yourselves, you can probably get away with this. But things get messy fast when the consumers of your library want to use a different framework and now they have to adapt output of one to the other or somehow configure them both. Then things get real messy when one of the logging frameworks change their signing key in a patch release breaking stuff left, right and centre.

    I think at one point I had NLog, Log4Net (2 versions), EntLib logging be pulled into a single project. That's when I had enough.

    Read more...


  • Our Open Source policy at eVision

    27 Nov 2014

    There is a problem within our industry, particularly around .NET, known as the "no-contrib culture". That is, companies take and benefit from OSS but don't given anything back and also prevent their employees from doing so. A while ago I blogged about establishing an Open Source Software policy at a proprietary software company that happens to consume and depend on a significant amount of open source libraries. eVision was a typical company in that the standard employment contract had that catch-all clause that they own everything one does on a computer.

    With the proliferation of OSS, the fact the platform we predominately build on is going OSS, and the fact that OSS is a viable business strategy (even within proprietary businesses), this was a somewhat short-sighted. Indeed, several employees were simply ignoring this; a situation which was in neither party's interest.

    Read more...