我试图在我的应用程序中“注入”自定义跟踪方法。
我想使其尽可能优雅,而无需修改许多现有代码,并可以轻松启用/禁用它。
我想到的一个解决方案是创建一个自定义Attribute
,将其附加到我要跟踪的方法上。
基本思路:
public class MethodSnifferAttribute : Attribute
{
private Stopwatch sw = null;
public void BeforeExecution()
{
sw = new Stopwatch();
sw.Start();
}
public void ExecutionEnd()
{
sw.Stop();
LoggerManager.Logger.Log("Execution time: " + sw.ElapsedMilliseconds);
}
}
public class MyClass
{
[MethodSniffer]
public void Function()
{
// do a long task
}
}
是否有任何现有.NET
属性可为调用/结束方法提供回调?
除非手动调用属性的方法,否则不会调用该方法。有一些由CLR调用的安全属性,但这超出了此问题的范围,并且无论如何都不会有用。
有一些技巧可以在不同级别上重写代码。源代码编织,IL编织等。
您需要研究一下修改IL并重写它以定时执行的方法。不用担心,您不必编写所有这些内容。人们已经做到了。例如,您可以使用PostSharp。
这是提供示例的文章
[Serializable]
[DebuggerStepThrough]
[AttributeUsage(AttributeTargets.Method)]
public sealed class LogExecutionTimeAttribute : OnMethodInvocationAspect
{
private static readonly ILog Log = LogManager.GetLogger(typeof(LogExecutionTimeAttribute));
// If no threshold is provided, then just log the execution time as debug
public LogExecutionTimeAttribute() : this (int.MaxValue, true)
{
}
// If a threshold is provided, then just flag warnning when threshold's exceeded
public LogExecutionTimeAttribute(int threshold) : this (threshold, false)
{
}
// Greediest constructor
public LogExecutionTimeAttribute(int threshold, bool logDebug)
{
Threshold = threshold;
LogDebug = logDebug;
}
public int Threshold { get; set; }
public bool LogDebug { get; set; }
// Record time spent executing the method
public override void OnInvocation(MethodInvocationEventArgs eventArgs)
{
var sw = Stopwatch.StartNew();
eventArgs.Proceed();
sw.Stop();
var timeSpent = sw.ElapsedMilliseconds;
if (LogDebug)
{
Log.DebugFormat(
"Method [{0}{1}] took [{2}] milliseconds to execute",
eventArgs.Method.DeclaringType.Name,
eventArgs.Method.Name,
timeSpent);
}
if (timeSpent > Threshold)
{
Log.WarnFormat(
"Method [{0}{1}] was expected to finish within [{2}] milliseconds, but took [{3}] instead!",
eventArgs.Method.DeclaringType.Name,
eventArgs.Method.Name,
Threshold,
timeSpent);
}
}
注意:我修改了本文中的示例,以使用它,StopWatch
而不是DateTime
因为DateTime
它不准确。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句