on this page (jump to):

Adding detailed tracing

When you're working with your codebase it's common to need to add logging 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 passed to the method when it was called as well as the parameter values once the method call has completed. 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 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 for the start and completion of method calls.

  1. Let's add logging to our Save method.
    public void Save(string firstName, string lastName, string streetAddress, string city) 
    var customerRepository = new CustomerRepository(); 
    customerRepository.Save(firstName, lastName, streetAddress, city); 
  2. Put the caret on the Save method name and expand the Smart Tag. From the list select "Add loggging".
    PostSharp method level smart tag.
  3. The first option that you need to select is the Logging Level. For this example we want will take the default provide: it logs the method enters and exits, and include parameter values. Selecting the logging level.
  4. The next page of the wizard gives you the opportunity to choose the logging backend that you want to use. For this example select System.Diagnostics.Trace and click Next.
    PostSharp Select Back End wizard page
  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 Back button and adjust your selections. If the configuration meets your needs click Next.
    PostSharp Summary wizard page
  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.
    PostSharp Progress wizard page
  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.
    PostSharp Completed wizard page
  8. You'll notice that the code you added the logging to has changed slightly. PostSharp has added a Log attribute to the method. Since, we chose the default logging profile, there is no argument to the Log attribute.
    [Log] 
    public void Save(string firstName, string lastName, string streetAddress, string city) 
    var customerRepository = new CustomerRepository(); 
    customerRepository.Save(firstName, lastName, streetAddress, city); 

    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 the MulticastAttribute

  9. If you were to run this method the trace logging that you added would output a log message when entering the method and an entry when leaving the method. Note that the parameter values are automatically included in the log message.
    Trace Parameter logging console output

Now that you have logging added to the Save 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 attribute multicasting, adding logging to your codebase and maintaining it becomes a very easy task.



What to read next?

Tracing parameter values during exceptions or back to Diagnostics.