PostSharp Diagnostics Now Supports Loupe in the Box

Adding logging to your application has never been easier than with PostSharp Diagnostics. You can extensively instrument an entire assembly with a single instruction, capturing application flow, parameter values, errors, and timing. We’ve been using PostSharp within Loupe itself for years to handle a wide swath of general logging needs and use it on nearly every consulting project we do as well.

// Add logging to a whole namespace in just one line.
[assembly: Log(AttributePriority = 1, AttributeTargetTypes = "BusinessLayer.*")]
                    
[Log(AttributeExclude = true)]
class Program
{
    static void Main(string[] args)
    {
        // Configure logging at run time.
        var backend = new LoupeLoggingBackend();
        LoggingServices.DefaultBackend = backend ;
                    
        // Your whole application, instrumented!
    }
}

How it Works

PostSharp is an Aspect Oriented Programming framework that weaves in additional libraries and behaviors (called “aspects”) at compile time. These aspects are often written at a very low level - even below C# - to ensure they are not just provably correct but the fastest possible implementation in .NET. We’ve always thought we were performance focused but the PostSharp team is performance obsessed.

It pays off with PostSharp Diagnostics because you can log a positively crazy amount of detail without hurting the performance of your application, and faster than the code you might write yourself to do the same job.

By injecting this code during compilation it:

  • Keeps your source code clean: No gratuitous log statements that are patently obvious when looking at the code.
  • Ensures all your code has logging: You don’t have to worry about the new team member leaving it out or a hasty change having no diagnostics.

Go Deeper with PostSharp Diagnostics

While we’ve offered PostSharp Aspects for years tuned to produce data for Loupe the PostSharp team has gone a lot deeper with using PostSharp for general logging. Our emphasis has largely been on using PostSharp for high-level structural logging (like process entrance and exit) and hand-written messages for execution details. With the new PostSharp Diagnostics you can go a lot further with the automated logging, narrowing down where you need to add manual log messages to convey the intent of what the code is doing, not the execution.

PostSharp Diagnostics safely adds in data that’s otherwise hard to get like the exact parameter values on a methods when an exception is thrown. Plus, you can turn on and off capturing this data at runtime to ensure there is no performance impact even if you have very detailed logging compiled in.

Combine with Loupe For the Best of Both Worlds

You could combine auto-generated PostSharp logging with manual PostSharp logging and never directly touch any logging framework if you wanted to, but even better is to take advantage of the Loupe Logging API for your hand-crafted logging messages that communicate the meaning of what your application is doing. These will be correctly integrated with the auto-generated logging from PostSharp to give you everything you need to diagnose problems after the fact.

PostSharp and Loupe have always been a great fit: PostSharp lets you rapidly instrument your application for logging and Loupe provides all the infrastructure to gather those logs, manage them, and view them - on your desktop and on the web. We’d encourage you to give PostSharp a try if you haven’t. We even have compatible pricing models:

  • Developers have Free Options: Combine the free Loupe Desktop viewer with PostSharp Diagnostics and you can instrument and view all the code you build locally for free. PostSharp logging will deactivate a few days after you compile the app, something that’s no problem for local development.
  • Deploy Anywhere Affordably: Loupe Cloud-Hosted plans start below $50/month and Postsharp Diagnostics is just a few hundred for a team of five developers.

For details on how to try out PostSharp Diagnostics with Loupe, check out Adding Detailed Logging to your Solution and then configure the Loupe Backend. Of course, if you haven’t already be sure to download Loupe Desktop to have a great local log viewing experience!

Rock solid centralized logging

Unlimited applications, unlimited errors, scalable from solo startup to enterprise.