Showpitch Advanced Part 2: Using Neat Architecture for Logging

Last week we reviewed using Neat Architecture for Showpitch‘s Domain Services. This week we’re going to go in to Neat and Logging on Showpitch.

At Showpitch we’ve built on the Logging Interceptor that’s offered in Neat. But it’s a fantastic starting point! Using this we get a lot of functionality right out of the box for each new piece of code we add to Showpitch. So let’s talk about Logging and how Neat Logging solves some pretty common problems!

Logging, A Necessary Evil

As app developers, we all eventually reach the same conclusion, one famously posited by Murphy

If something can go wrong, it eventually will go wrong.

In the face of the inevitable we then have to acknowledge that we will need to understand what’s going wrong, when, and how. And so we start adding Logging code to our app.

So why do I call Logging a “Necessary Evil”?

This is because, as app developers, we are not writing apps for Logging. We’re not designing, coding, and hacking for the sake of Logging. We build apps to solve problems. If we didn’t need to write code, reserve systems resources, and spend database and disk space for Logging we wouldn’t. And yet time and again we spend hours of our time, countless CPU cycles, and endless bytes of storage on Logging.

So great is the tax that is levied by Logging that it could easily be framed as an app’s equivalent of that dastardly Sheriff of Nottingham. We hate to pay its tax, but there’s no getting out of it.

And without Logging we can’t know what’s going wrong, when, and how. It’s Evil, but it’s Necessary, hence a “Necessary Evil”.

Logging, A Cross Cutting Concern

Once we accept the need for Logging, the next step is to add it to our app. And this is where, for many app developers, the pain begins. How familiar does this code block look to you?

public class AThingDoingClass : IAThingDoingClass
{
    public void DoesOneThingAndOneThingOnly()
    {
        try
        {
            DoSomethingThatCouldThrowAnError();
        }
        catch(Exception ex)
        {
            Logger.Log(ex);
        }
    }
}

What can we learn from this example?

First, we’re using 8 lines of code, for something that should only require 1. That’s a lot of extra code, just to accommodate Logging.

Second, we can probably expect to see this code duplicated about 500 times in our application. Not very DRY, is it? We need Logging all over the place, but we’re reproducing the same code over and over!

Finally, let’s talk SOLID, specifically Single Responsibility. Where does our method say it’s a Logging method? It “Does One Thing And One Thing Only” from what we can see. It would seem this method doesn’t live up to its Single Responsibility.

This is where such a Cross Cutting Concern comes from. All over the app we’re going to need Logging, but this concern isn’t specific to any given method, nor is it the responsibility of any of those methods. And yet it shows up all over!

Logging, A Decorator Approach

So could we at least address the Single Responsibility issue?

Yes, by introducing a Decorator we can separate these responsibilities. For example:

public class AThingDoingClass : IAThingDoingClass
{
    public void DoesOneThingAndOneThingOnly()
    {
        DoSomethingThatCouldThrowAnError();
    }
}

public class AThingDoingClassLoggingDecorator : IAThingDoingClass
{
    private read only IAThingDoingClass _aThingDoingClass;
    
    public AThingDoingClassLoggingDecorator(IAThingDoingClass aThingDoingClass)
    {
        _aThingDoingClass = aThingDoingClass;
    }
    public void DoesOneThingAndOneThingOnly()
    {
        try
        {
            _aThingDoingClass.DoesOneThingAndOneThingOnly();
        }
        catch(Exception ex)
        {
            Logger.Log(ex);
        }
    }
}

Ok, now we only have one responsibility per method. Great, but this is still a lot of code to write! And it’s still not DRY! So is there an even better way?

Logging, An Aspect and Neat

What if there were a way to wrap every class and every method in your application with one of these Decorators, automatically, without having to repeat the same code all over?

That’s what Neat does! Using Interceptors built in to Microsoft Practices Unity 3 we’ve built a Logging Interceptor in Neat, which does exactly this.

This is evocative of Aspect-Oriented Programming (AOP). .Net doesn’t lend itself well to AOP, but using Interceptors we can achieve something very close to it! And this is exciting, because it means we can now assign behavior in our app simply by ascribing an Aspect to our code! And once we have that, we can make these Aspects a convention of our code. This gets us a lot of behavior for a much smaller investment!

We even built a Decorator around the Microsoft Practices Unity 3 container that automatically registers this Interceptor with every class registration. This makes the Logging Aspect of our app a convention that every class and every method uses. We’ve also included a Mongo Logging Provider, built on top of nLog, that’s already wired up in the Neat example solution. This way you get Logging right away, no muss, no fuss!

So what does this mean for a developer? It means you can simplify and reduce code, like the example code, to only what it does, and let the Architecture take care of Logging for you. For example:

public class AThingDoingClass : IAThingDoingClass
{
    public void DoesOneThingAndOneThingOnly()
    {
        DoSomethingThatCouldThrowAnError();
    }
}

That’s it! The Logging is there, but it’s intrinsic to the app now.

If you want to explore how this has been done, take a look under the hood in the Neat example solution. Get started at the GitHub Page for Neat.

The following objects comprise this Logging solution in Neat and will be interesting to look at:

  • Neat.Infrastructure.Unity.Interceptor.LoggingInterceptor
  • Neat.Infrastructure.Unity.LoggingUnityContainer
  • Neat.Infrastructure.Logging.ILogProvider
  • Neat.Infrastructure.Logging.NLogProvider

If you want to build a new LogProvider to use a different Logging system, or even a custom Logging system, then just build a new class that implements Neat.Infrastructure.Logging.ILogProvider and register it with the container. Your Logging system will now be used!

That’s Logging in Neat! Showpitch started with this solution and with it and its evolution, we’ve saved our team literally hundreds of hours of Production troubleshooting and writing and maintaining Logging code for our platform!

Is there something you want to see me cover in the next post? Is there a part of Neat or Showpitch you want to see more on, or something you want to see added to Neat? Please add a comment below! And keep checking back to learn more!

<< Back to Showpitch Advanced Part 1
On to Showpitch Advanced Part 3 >>

Advertisement

Showpitch 102: Let’s Review the Tech

So where we left off we covered what Showpitch is and what we’re setting out to accomplish. Now I want to review what technologies we’re using to accomplish our goals, and highlight a few of them in particular.

In the last post I had said I wanted to share what I’ve been working on for the past two years. Well Showpitch is not the entirety of that two year period. To start with, I only joined the Showpitch team about a year and a half ago. Even then, I didn’t step in to the architectural role for the product for another 3 months or so after that.

Before that was even the case though I had been working on other projects, exploring and experimenting with some of the technologies, patterns, and practices that we ultimately put in to place at Showpitch. For the first 3 months I was there, I was there as a Front End team member, helping to build some of the components that we were going to use for the UI at that time.

But then there was change in the team structure, and direction of the company, and as I had experience leading, architecting, and developing projects before I was asked to take over as the Senior Software Architect overseeing and implementing a from scratch reboot of the entire Showpitch platform, along with my good friend and colleague Eric Johnson (better known as EJ!) as a peer and partner in charge of the Front End team. (And lots of other stuff as well! In fact, if you’re all lucky, EJ may be convinced to add a guest post to this blog some day!)

By this point Showpitch had been developing this platform for quite a while. So with an entire reboot from scratch upon us, and expectations from stakeholders, partners, and other industry members ahead of us, one thing was very clear: We needed to move very fast. But with the vision that was in mind, we also needed to do things right, and keep where we were going in mind. We had to follow of the wisdom of two great minds: to be mindful of the future, but not at the expense of the moment.

This lead us to the list of modern approaches that appeared in the first post. Let’s dive in a bit more now and talk about the technologies, practices, and partners that we choose to implement and support each of those approaches.

Being Cloud First, Cloud Native

When we talk about being Cloud First, Cloud Native, what does that mean? For us it meant that we simply didn’t have the budget, people, or time to buy our own hosting hardware and software, nor to setup our own company computing environment. And that’s been to our benefit. Everything we do at Showpitch is on the Cloud; collaboration, development, management, and especially hosting.

We work with, leverage, and consume Cloud services from providers such as:

This is not an all inclusive list of the Cloud technologies and partners we leverage, but it should server as an example of just how cloud oriented Showpitch is.

Leveraging partner solutions where possible and economically viable

In addition to the Cloud First, Cloud Native approach, we also wanted to focus on delivering features and functionality with our platform, and not spend time building what others have already built, and probably built better. This stands in contrast to what I consider the NIH anti-pattern (Not Invented Here).

To that end we looked outside our team first for implementations of many of the features we have, including:

Again, this is not an exhaustive list. But the purpose of this approach was to start up quickly, and use the expertise and tools that each of these partners made available to support our development. We could have easily gotten caught up in building solutions for each of these components ourselves, but instead we were able to focus on developing our own features.

Leveraging technologies that allow for Rapid Application Development and Iteration

This was a very important approach to consider, perhaps the most important, as we had to demonstrate feature progress immediately. To the that end we needed to select application components that supported building this system very quickly. What we decided on were things like:

These are some of the technologies that have allowed us to quickly prototype, apply proof of concept, development, test, refine, iterate, and successfully deliver. I’d especially like to call out Unity and ReSharper here. Unity has been massively important to the design of our application and has allowed us to accomplish in handfuls of code what would have been entire projects otherwise. I will be posting specifically on a couple examples of this throughout this series. And ReSharper has increased productivity for our team across the board. Its Code Navigation and Refactoring capabilities alone have saved us dozens if not a couple hundred man hours across the team, and this includes the Front End Team who works largely in the realm of JavaScript (Yeah, ReSharper does that!).

Running on a very Lean and Agile Management approach

All of these technologies, tools, and partners would amount to naught though if we were constantly getting in our own way, or if we were constantly in a state of analysis and requirements gathering. As such we’ve had to find a fine line between staying organized and getting work done. So while our team oscillates between a Scrum-like approach and a KanBan-like approach, we still attribute a couple of key technologies to our success:

This combination of technologies has allowed us to work while distributed geographically, on a Cloud Oriented basis, without having to invest significantly in office management nor technology management.

Using only what we need, when we need it, and being ready to scale when the time comes

Through the use of everything we’ve already discussed we’ve been able to design for and adhere to this last approach. Almost of the Cloud partners and technologies we use are built to scale on demand, or near enough, that we don’t need to make huge up front commitments or investments. In fact we’ve switched providers and partners several times during the past year, sometimes in as little as day, with no disruption to our ongoing development!

All of this ties together in to a success story culminating in the live Showpitch site today, and continues to enable and empower our success moving forward.

Do you want me to focus on any of these technologies in future posts? If so, please leave a comment below! And stay tuned for more posts that will go in to greater detail about these technologies, practices, and approaches in particular!

<< Back to Showpitch 101
On to Showpitch 103 >>