Loupe - Log - Monitor - Resolve
Loupe / Developer's Guide / For .NET Framework / Third Party Integration / Third Party Integration - Using PostSharp with Loupe
In This Topic
    Third Party Integration - Using PostSharp with Loupe
    In This Topic

    PostSharp is an aspect-oriented programming tool that helps make your code easier to understand and maintain by encapsulating repeating code patterns as .NET custom attributes.  These repeating patterns, called aspects, can then be applied wherever you want in your software by just tagging an appropriate line of code with the attribute.  Loupe includes a set of aspects that can quickly create log messages and metrics.  To get started:

    Using NuGet

    Search for the Loupe Agent for PostSharp 3.0 and install.  This agent works with PostSharp 3.0 through 4.1.  All dependencies are automatically installed however you will need to go to PostSharp's site to request a (free!) license to PostSharp Express.

    The Loupe Agent for PostSharp does not require any commercial distribution license.  This is a great way to experience the benefits of logging and metrics quickly with Loupe.  If you want to create your own aspects for use with PostSharp you will need an appropriate license to PostSharp.  We recommend you get started with the free PostSharp Express license.
     How PostSharp and Loupe Work Together

    When you tag methods with aspects PostSharp inserts itself in the build process to post-process compiled assemblies adding code at the MSIL level incorporating the aspect with your code.  Each time you build your project the aspect code is inserted in the right places.  With this approach you don't have to worry about autogenerated code getting in the way of your ability to maintain and work with your application. 

    For example, in the code snippet below, the SearchGoogle method is tagged with the GTrace aspect. This would result in Loupe logging every call to this method including arguments, return value and execution time. Alternately, if [GTrace] were applied to the the SearchWrapper class, calls to all methods in the class would be logged.

    Adding Tracing to One Method
    Copy Code
    public class SearchWrapper
    {
        [GTrace]
        public Results SearchGoogle(string search)
        {
            // ...
        }
        public Results SearchBing(string search)
        {
            // ...
        }
    }
    

    Aspects can also be assigned to whole assemblies with flexible filtering so that you can instrument whole programs with just a few attribute tags.  For more details, see PostSharp Tips & Tricks.

    For ideas on how to effectively use Loupe and PostSharp together see Using PostSharp with Loupe - Best Practices.

     Aspects Provided with Loupe

    The Loupe Agent for PostSharp contains a set of PostSharp aspects that integrate with Loupe.  These aspects make it easy to add as much or as little code instrumentation as needed to debug and support your application during testing and production quickly and easily.

    Aspect Purpose Description
    GException Record exceptions at the point they are thrown. Ensures that any exception thrown by a method is logged as a warning even if the exception is handled somewhere higher in the call stack. Log messages include full exception details.
    GTrace Log method execution. Logs method entry and exit including parameters and return value. In addition, it records execution time and logs exception details if the method exits abnormally.
    GTimer Profile method execution time. Used for performance monitoring. It records method execution duration each time a method is called. When debugging, GTimer can be used to identify performance hot spots. In production, GTimer can be applied to critical methods such as database, network operations or transactions to continuously monitor system performance under real-world conditions.
    GField Monitor changes to any field in a class.

    Logs a message every time a field changes value. For numeric fields, this value can be graphed and correlated with graphs of system performance. 

    GFeature Track feature usage. Combines capabilities of GTrace, GTimer, GField, and GException to give you a comprehensive view of method execution. Typically, GFeature is applied to high-level methods that implement user requests. But, GFeature is also a very handy debugging tool during development.

    For ideas on how to use these different aspects in combination see Third Party Integration - Using PostSharp with Loupe - Best Practices

    Where To Next?

    See Also