我正在使用PostSharp 6.4.5。我需要为现有项目添加方法级别跟踪。我想记录输入和退出方法以及参数类型和值的时间。我只能重建项目/解决方案,而不能在代码中进行任何更改。我遇到了一种通过使用XML添加方面来实现此目的的方法。
https://doc.postsharp.net/xml-multicasting
https://doc.postsharp.net/configuration-system
https://doc.postsharp.net/logging-customizing
使用此方法以及PostSharp的其他一些配置,我创建了一个如下所示的postsharp.config。
<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>
我还在csproj存在的同一目录中创建了一个psproj文件。以下是psproj文件的内容。
<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>
然后,我重建项目并运行应用程序,但是我看不到任何跟踪信息。如果我缺少某些东西,请让我知道。
[第1步:我们构建了一个库,该库将使用Postsharp记录方法的进入和退出。例如:名称-> Assembly1。命名空间-> PostSharp.Samples.CustomLogging
步骤2:我们将此库引用添加到需要跟踪的所有项目中。我们还添加了postharp依赖项。所有这些更改都是以编程方式对csproj文件进行的。我们还在csproj文件中添加了对PostShrap.targets的引用,如下所示。
<Import Project="path to PostSharp.targets" />
[步骤3:在与csproj文件相同的目录中创建一个* .psproj文件,其名称与csproj文件相同。该文件的内容如下所示。程序集名称和名称空间从步骤1中引用。在AttributeTargetTypes中,可以指定正则表达式。所有与正则表达式匹配的方法都会在进入和退出时进行登录]
<?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>
步骤4:重建项目。
对于许可证,需要创建一个包含许可证密钥的postsharp.config文件。该文件必须与csproj和psproj文件位于同一目录中。
现在所有方法都将具有跟踪,而无需对源文件进行任何更改。所有所做的更改将在csproj文件中。
希望这会有所帮助。