Search code examples
c#aoppostsharp

Add Post Sharp Aspect without changing the existing code


I am using PostSharp 6.4.5. I need to add method level tracing for existing projects. I want to log when the method is entered and exited along with parameter types and values. I can only rebuild the project/solution and cannot make any changes in the code. I came across a way to achieve this by using Adding Aspects Using XML.

https://doc.postsharp.net/xml-multicasting

https://doc.postsharp.net/configuration-system

https://doc.postsharp.net/logging-customizing

Using this method, and following few other configuration for PostSharp, I have created a postsharp.config which looks like below.

<Project xmlns="http://schemas.postsharp.org/1.0/configuration">
  <Logging xmlns="clr-namespace:PostSharp.Patterns.Diagnostics;assembly:PostSharp.Patterns.Diagnostics">
    <Profiles>
      <LoggingProfile Name="detailed" IncludeSourceLineInfo="True" IncludeExecutionTime="True" IncludeAwaitedTask="True">
        <DefaultOptions>
          <LoggingOptions IncludeParameterType="True" IncludeThisValue="True" Level="Trace"/>
        </DefaultOptions>
      </LoggingProfile>
    </Profiles>
  </Logging>
</Project>

I have also created a psproj file in the same directory where csproj exists. Following are the contents of the psproj file.

<Project xmlns="http://schemas.postsharp.org/1.0/configuration">
  <Property Name="LoggingBackend" Value="console" />
  <Using File=" absolute path to viewer dll \PostSharp.Patterns.Diagnostics.Weaver.dll"/>
  <Multicast>
    <LogAttribute xmlns="clr-namespace:PostSharp.Patterns.Diagnostics;assembly:PostSharp.Patterns.Diagnostics" ProfileName="Default" AttributeTargetTypes="Mynamepace.*" />
  </Multicast>
</Project>

Then I rebuild the project and run the application, but i cannot see any trace information. Please let me know if i am missing something.


Solution

  • Step 1: We built a library which would log the entry and exit of a method using Postsharp. Eg: Name --> Assembly1. Namespace --> PostSharp.Samples.CustomLogging

    Step 2: We added this library reference to all the projects, where tracing was required. We also added the postsharp dependencies. All these changes were made to the csproj files programatically. We also added the reference to PostShrap.targets in the csproj files as shown below.

    <Import Project="path to PostSharp.targets" />
    

    Step 3: Create a *.psproj file in the same directory as csproj file with the same name as csproj file. The contents of the file is shown below. The assembly name and namespace are referred from Step 1. In the AttributeTargetTypes one can specify regex. All the methods which match the regex will have the logging on entry and exit

    <?xml version="1.0" encoding="utf-8"?>
    <Project xmlns="http://schemas.postsharp.org/1.0/configuration">
      <Multicast xmlns:my="clr-namespace:PostSharp.Samples.CustomLogging;assembly:Assembly1">
        <my:LogMethodAttribute  AttributeTargetTypes="*" />
      </Multicast>
    </Project>
    

    Step 4: Rebuild the project.

    For license, one needs to create a postsharp.config file containing the license key. This file has to be present in the same directory as csproj and psproj files.

    All the methods will now have the tracing without any changes made to source files. All the changes made will be on the csproj files.

    Hope this helps.