我很难追踪锁定问题,所以我想记录每个方法调用的进入和退出。我以前用 C++ 做过这个,而不必为每个方法添加代码。这可以用 C# 实现吗?
6 回答
最好的选择可能是使用 AOP(面向方面编程)框架在方法执行之前和之后自动调用跟踪代码。AOP 和 .NET 的一个流行选择是PostSharp。
如果您的主要目标是记录函数入口/出口点和介于两者之间的偶尔信息,那么我使用Disposable记录对象取得了不错的结果,其中构造函数跟踪函数 entry,而Dispose() 跟踪 exit。这允许调用代码简单地将每个方法的代码包装在单个using语句中。还为中间的任意日志提供了方法。这是一个完整的 C# ETW 事件跟踪类以及一个函数进入/退出包装器:
using System;
using System.Diagnostics;
using System.Diagnostics.Tracing;
using System.Reflection;
using System.Runtime.CompilerServices;
namespace MyExample
{
// This class traces function entry/exit
// Constructor is used to automatically log function entry.
// Dispose is used to automatically log function exit.
// use "using(FnTraceWrap x = new FnTraceWrap()){ function code }" pattern for function entry/exit tracing
public class FnTraceWrap : IDisposable
{
string methodName;
string className;
private bool _disposed = false;
public FnTraceWrap()
{
StackFrame frame;
MethodBase method;
frame = new StackFrame(1);
method = frame.GetMethod();
this.methodName = method.Name;
this.className = method.DeclaringType.Name;
MyEventSourceClass.Log.TraceEnter(this.className, this.methodName);
}
public void TraceMessage(string format, params object[] args)
{
string message = String.Format(format, args);
MyEventSourceClass.Log.TraceMessage(message);
}
public void Dispose()
{
if (!this._disposed)
{
this._disposed = true;
MyEventSourceClass.Log.TraceExit(this.className, this.methodName);
}
}
}
[EventSource(Name = "MyEventSource")]
sealed class MyEventSourceClass : EventSource
{
// Global singleton instance
public static MyEventSourceClass Log = new MyEventSourceClass();
private MyEventSourceClass()
{
}
[Event(1, Opcode = EventOpcode.Info, Level = EventLevel.Informational)]
public void TraceMessage(string message)
{
WriteEvent(1, message);
}
[Event(2, Message = "{0}({1}) - {2}: {3}", Opcode = EventOpcode.Info, Level = EventLevel.Informational)]
public void TraceCodeLine([CallerFilePath] string filePath = "",
[CallerLineNumber] int line = 0,
[CallerMemberName] string memberName = "", string message = "")
{
WriteEvent(2, filePath, line, memberName, message);
}
// Function-level entry and exit tracing
[Event(3, Message = "Entering {0}.{1}", Opcode = EventOpcode.Start, Level = EventLevel.Informational)]
public void TraceEnter(string className, string methodName)
{
WriteEvent(3, className, methodName);
}
[Event(4, Message = "Exiting {0}.{1}", Opcode = EventOpcode.Stop, Level = EventLevel.Informational)]
public void TraceExit(string className, string methodName)
{
WriteEvent(4, className, methodName);
}
}
}
使用它的代码将如下所示:
public void DoWork(string foo)
{
using (FnTraceWrap fnTrace = new FnTraceWrap())
{
fnTrace.TraceMessage("Doing work on {0}.", foo);
/*
code ...
*/
}
}
使用Red Gate 的ANTS Profiler将是您的最佳选择。如果做不到这一点,请查看Castle Windsor中的拦截器。不过,这确实假设您正在通过 IoC 加载您的类型。
反射是另一种方式,您可以使用System.Reflection.Emit方法将代码“写入”到内存中。该代码可以替换您的方法的代码,并执行它但使用适当的日志记录。不过,祝你好运……更容易的是使用像Aspect#这样的面向方面的编程框架。
它可能正在等待锁定问题发生,执行内存转储并分析各种线程上的调用堆栈。您可以使用DebugDiag或Windows 调试工具附带的 adplus 脚本(在本例中为挂起模式)。
Tess Ferrandez还有一个关于学习使用 .NET 内存转储调试各种问题的优秀实验系列。我强烈推荐它。
你怎么知道它正在发生?如果这是一个多线程应用程序,我建议测试条件并在运行时检测到它时调用 System.Diagnostics.Debugger.Break()。然后,只需打开线程窗口并单步执行每个相关线程上的调用堆栈。