跟踪方法执行时间
Tracing methods execution time
我正在尝试 "inject" 在我的应用程序中自定义跟踪方法。
我想让它尽可能优雅,而不需要修改很多现有代码,并且可以轻松启用/禁用它。
我能想到的一个解决方案是创建一个自定义 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.
这里有一个 article 提供了一个例子
[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
不准确。
您可以使用 PostSharp(作为 NuGet 包提供)轻松跟踪方法执行时间。执行此操作的自定义方法级属性的代码(取自 here):
[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 start = DateTime.Now;
eventArgs.Proceed();
var timeSpent = (DateTime.Now - start).TotalMilliseconds;
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);
}
}
我正在尝试 "inject" 在我的应用程序中自定义跟踪方法。
我想让它尽可能优雅,而不需要修改很多现有代码,并且可以轻松启用/禁用它。
我能想到的一个解决方案是创建一个自定义 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.
这里有一个 article 提供了一个例子
[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
不准确。
您可以使用 PostSharp(作为 NuGet 包提供)轻松跟踪方法执行时间。执行此操作的自定义方法级属性的代码(取自 here):
[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 start = DateTime.Now;
eventArgs.Proceed();
var timeSpent = (DateTime.Now - start).TotalMilliseconds;
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);
}
}