Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何为特定方法中调用的所有方法启用PostSharp?_C#_Logging_Aop_Postsharp_Aspect - Fatal编程技术网

C# 如何为特定方法中调用的所有方法启用PostSharp?

C# 如何为特定方法中调用的所有方法启用PostSharp?,c#,logging,aop,postsharp,aspect,C#,Logging,Aop,Postsharp,Aspect,我最近开始在我们的一个项目中使用PostSharp。其目的是记录在特定方法中调用的所有方法的方法执行时间(表示特定功能) 到目前为止,我所做的是创建了一个方面(比如TimingSpect),并在一个方法上进行了测试(通过在方法定义上面写“[TimingSpect]”)。它工作正常,将该方法的执行时间记录在单独的日志文件中 据我所知,如果我在方法定义上写“[TimingSpect]”,它只记录该方法的日志,而不记录从该方法调用的其他方法的日志。我说得对吗?因此,现在我想知道是否有任何方法可以实现目

我最近开始在我们的一个项目中使用PostSharp。其目的是记录在特定方法中调用的所有方法的方法执行时间(表示特定功能)

到目前为止,我所做的是创建了一个方面(比如TimingSpect),并在一个方法上进行了测试(通过在方法定义上面写“[TimingSpect]”)。它工作正常,将该方法的执行时间记录在单独的日志文件中


据我所知,如果我在方法定义上写“[TimingSpect]”,它只记录该方法的日志,而不记录从该方法调用的其他方法的日志。我说得对吗?因此,现在我想知道是否有任何方法可以实现目标,即记录在特定方法中调用的所有方法的方法执行时间

您可以通过实现将方面应用于从目标方法调用的方法。要查找所有被调用的方法,可以使用PostSharp API中的类

下面您可以找到此类方面提供程序的示例

[PSerializable]
public class TimingAspectProvider : MethodLevelAspect, IAspectProvider
{
    public IEnumerable<AspectInstance> ProvideAspects(object targetElement)
    {
        MethodBase targetMethod = (MethodBase) targetElement;

        IAspectRepositoryService aspectRepositoryService = PostSharpEnvironment.CurrentProject.GetService<IAspectRepositoryService>();
        TimingAspect aspect = new TimingAspect();

        MethodUsageCodeReference[] usages = ReflectionSearch.GetDeclarationsUsedByMethod(targetMethod);
        foreach (MethodUsageCodeReference codeReference in usages.Where(u => u.UsedDeclaration.MemberType == MemberTypes.Method))
        {
            if (!aspectRepositoryService.HasAspect(codeReference.UsedDeclaration, typeof(TimingAspect)))
            {
                yield return new AspectInstance(codeReference.UsedDeclaration, aspect);
            }
        }
    }
}
[可序列化]
公共类TimingSpectProvider:MethodLevelAspect,IAspectProvider
{
公共IEnumerable ProvideSpects(对象targetElement)
{
MethodBase targetMethod=(MethodBase)targetElement;
IAspectRepositoryService aspectRepositoryService=Post锐化环境.CurrentProject.GetService();
TimingSpect方面=新的TimingSpect();
MethodUsageCodeReference[]usages=ReflectionSearch.GetDeclarationsUsedByMethod(targetMethod);
foreach(MethodUsageCodeReference在usages.Where中的codeReference(u=>u.UsedDeclaration.MemberType==MemberTypes.Method))
{
如果(!aspectRepositoryService.HasAspect(codeReference.UsedDeclaration,typeof(TimingSpect)))
{
返回新AspectInstance(codeReference.UsedDeclaration,aspect);
}
}
}
}

请注意,这不包括从被调用方法调用的方法的日志记录等。我的理解是,您希望记录直接从目标方法调用的方法。

@Ronit Singh:您找到解决方案或解决方法了吗?