Walkthrough: Tracing Parameter Values Upon Exception

When you're working with your codebase it's common to need to add logging of exceptions either as a non-functional requirement or simply to assist during the development process. In either situation you will want to include information about the parameters that were passed to the method where the exception is being caught and logged. This can be a tedious and brittle process. As you work and refactor methods the order and types of parameters may change, parameters may be added and some maybe removed. Along with performing these refactorings you have to remember to update the exception logging messages to keep them in sync. This is something that is easy to forget and once forgotten the output of the logging is much less useful.

PostSharp offers a solution to all of these problems. The logging pattern library allows you to configure where logging should be performed and the pattern library takes over the task of keeping your log entries in sync as you add, remove and refactor your codebase. Let's take a look at how you can add trace logging of exceptions that includes the parameter values that were passed to the method that is being logged.

Note Note

This procedure requires PostSharp Tools for Visual Studio to be installed on your machine. You can however achieve the same results by editing the code and the project manually.

To add trace logging of exceptions that includes the parameter values:

  1. Let's add logging to our DoStuff method.

    C#
    public void DoStuff(int i, int x) 
    { 
        Console.WriteLine(i/x); 
    }
  2. Put the caret on the DoStuff method name and expand the Smart Tag. From the list select "Add logging".

    Trace Parameters 1
  3. The first option that you need to select is the Logging Profile. Here we want to choose the "Exceptions" profile and accept its default values. Click Next.

    Trace Parameters 2
  4. The next page of the wizard gives you the opportunity to choose the logging back-end that you want to use. For this example select "System.Diagnostics.Trace" and click Next.

    Detailed Tracing 3
  5. The summary page gives you the opportunity to review the selections that you have made. If you notice that the configuration is not what you wanted you can click the Previous button and adjust your selections. If the configuration meets your needs click Next.

    Trace Parameters 3
  6. The progress page shows a progress bar and summary of what actions PostSharp is taking to add the selected logging configuration to your codebase. It's at this point that PostSharp and the logging pattern library will be downloaded from Nuget and added as references to your codebase.

    Trace Parameters 4
  7. Once the download, installation and configuration of PostSharp and the logging pattern library has finished you can close the wizard and look at the changes that were made to your codebase.

    Detailed Tracing 6
  8. You'll notice that the code you added the logging to has changed slightly - PostSharp has added a LogExceptionAttribute attribute to the method.

    C#
    [LogException] 
    public void DoStuff(int i, int x) 
    { 
        Console.WriteLine(i/x); 
    }
    Note Note

    This example has added a single attribute to one method. If you plan on adding this logging to many different locations in your codebase you will want to read about using attribute multicasting. See Adding Aspects Declaratively Using Attributes.

  9. If you were to run this method from a console application and pass in a value of zero for the second parameter it would generate a DivideByZeroException. The trace logging that you added would output a log message plus the exception's stack trace to the console. Note that the parameter values are automatically included in the log message.

    Trace Parameters 7
  10. For those of you interested in what is happening behind the scenes we can decompile the method and observe what PostSharp has done to our codebase. You'll notice two significant things when you look at the decompiled code. First, PostSharp added in a try...catch block that wraps the entirety of the original methods contents. The second thing you'll notice is that the catch block logs the exception and re-throws it. This ensures that your code execution paths will remain unchanged after you've added the logging.

    Trace Parameters 8

Now that you have logging added to the DoStuff method you are able to change the method's name as well as add and remove parameters with the confidence that your log entries will be kept in sync with each of those changes. In combination with the attribute multicasting (See the section Adding Aspects Declaratively Using Attributes), adding logging to your codebase and maintaining it becomes a very easy task.

See Also