MethodLogger 修改dll 记录方法的开始和结束(Hook into a method)
2012-09-21 07:02
337 查看
如果你想记录一个方法的开始和结束,那么该怎么做呢?
最简单的方法:
public class MyClass
{
public void MyMethod()
{
Log.EnterMethod();
Console.WriteLine("MyMethod");
Log.ExitMethod();
}
}
可是,如果已经有了一个程序集,想在每个方法的开始和结束记录方法的执行,那么该如何做呢?
在CodeProject 上有篇文章就介绍了如何处理这种情况:
MethodLogger - Hook into method calls in .NET binaries
本文不打算做翻译,只是随便介绍下:How to hook into a method.
打开原文的project:
ModifyCode static void ModifyCode(ClassDef classDef, MethodDef methodDef, Method startLogMethod, Method endLogMethod)
{
string classNameString = MethodLoggerUtil.GetQualifiedClassName(classDef);
string methodNameString = methodDef.Name();
string paramsString = MethodLoggerUtil.GetParamsAsString(methodDef.GetParams());
Param[] parms = methodDef.GetParams();
// We'll be pushing typeName, methodName and parameters as string parameters, so set max stack size to 3.
if (methodDef.GetMaxStack() < 3)
{
methodDef.SetMaxStack(3);
}
CILInstructions instructions = methodDef.GetCodeBuffer();
instructions.StartInsert();
instructions.ldstr(classNameString);
instructions.ldstr(methodNameString);
instructions.ldstr(paramsString);
instructions.MethInst(MethodOp.call, startLogMethod);
instructions.StartBlock();
instructions.EndInsert();
while (instructions.GetNextInstruction().GetPos() < instructions.NumInstructions() - 2) ;
instructions.StartInsert();
TryBlock tryBlock = instructions.EndTryBlock();
instructions.StartBlock();
instructions.ldstr(classNameString);
instructions.ldstr(methodNameString);
instructions.ldstr(paramsString);
instructions.MethInst(MethodOp.call, endLogMethod);
instructions.EndFinallyBlock(tryBlock);
instructions.EndInsert();
}
[/code]
主要也是使用PERWAPI 来读写IL,然后在每个方法中写如BeginMethod和EndMethod。
修改后的MyClass.MyMethod() 大致是这个样子的:
个人建议:如果可以用第一种方法的话,就使用第一种方法,后面的修改IL的方式在某些情况下也很有用,如果有需要的同学,可以参考原文。
methodlogger.zip
methodlogger_src.zip
my_methodlogger.zip
最简单的方法:
public class MyClass
{
public void MyMethod()
{
Log.EnterMethod();
Console.WriteLine("MyMethod");
Log.ExitMethod();
}
}
可是,如果已经有了一个程序集,想在每个方法的开始和结束记录方法的执行,那么该如何做呢?
在CodeProject 上有篇文章就介绍了如何处理这种情况:
MethodLogger - Hook into method calls in .NET binaries
本文不打算做翻译,只是随便介绍下:How to hook into a method.
打开原文的project:
ModifyCode static void ModifyCode(ClassDef classDef, MethodDef methodDef, Method startLogMethod, Method endLogMethod)
{
string classNameString = MethodLoggerUtil.GetQualifiedClassName(classDef);
string methodNameString = methodDef.Name();
string paramsString = MethodLoggerUtil.GetParamsAsString(methodDef.GetParams());
Param[] parms = methodDef.GetParams();
// We'll be pushing typeName, methodName and parameters as string parameters, so set max stack size to 3.
if (methodDef.GetMaxStack() < 3)
{
methodDef.SetMaxStack(3);
}
CILInstructions instructions = methodDef.GetCodeBuffer();
instructions.StartInsert();
instructions.ldstr(classNameString);
instructions.ldstr(methodNameString);
instructions.ldstr(paramsString);
instructions.MethInst(MethodOp.call, startLogMethod);
instructions.StartBlock();
instructions.EndInsert();
while (instructions.GetNextInstruction().GetPos() < instructions.NumInstructions() - 2) ;
instructions.StartInsert();
TryBlock tryBlock = instructions.EndTryBlock();
instructions.StartBlock();
instructions.ldstr(classNameString);
instructions.ldstr(methodNameString);
instructions.ldstr(paramsString);
instructions.MethInst(MethodOp.call, endLogMethod);
instructions.EndFinallyBlock(tryBlock);
instructions.EndInsert();
}
[/code]
主要也是使用PERWAPI 来读写IL,然后在每个方法中写如BeginMethod和EndMethod。
修改后的MyClass.MyMethod() 大致是这个样子的:
个人建议:如果可以用第一种方法的话,就使用第一种方法,后面的修改IL的方式在某些情况下也很有用,如果有需要的同学,可以参考原文。
methodlogger.zip
methodlogger_src.zip
my_methodlogger.zip
相关文章推荐
- MethodLogger 修改dll 记录方法的开始和结束(Hook into a method)
- 如何管理和记录 SSIS 各个 Task 的开始执行时间和结束时间以及 Task 中添加|删除|修改的记录数
- 修改系统DLL文件实现禁用的方法
- 修改多对多的中间表的记录的方法
- 学习Discuz! X3.2记录:修改标签“Powered by Discuz!”的一种方法
- 数据不变,修改行记录标志为add的方法
- DLL文件常识and基本原理及修改方法
- IOS 根据日期,获取该日期所在周,月,年的开始日期,结束日期 的方法
- C#引用动态连接库dll时出现 System.MissingMethodException: 找不到方法的解决方案
- PHP获取自然周列表,周开始结束日期,月开始结束时间方法类
- db2取前十条记录 db2修改字段长度 db2增加字段方法
- 生产订单修改记录的跟踪方法!
- YII模型更新/修改记录方法
- java 获取本年 本月 本周开始时间(在网上搜的周日被定义为一周的开始,修改后周日为一周的结束)
- php获取开始时间结束时间方法总汇
- ASP.NET2.0中插入、删除、查询、修改记录这么多方法,究竟哪种最通用,最常用?
- 用vbs记录屏幕保护程序的开始时间和结束时间
- asp中用insert into语句向数据库插入记录(添加信息)的方法
- “Cannot load php5apache2_4.dll into server”问题的解决方法
- 错误记录:使用了mysql_affected_rows修改方法的返回值判断要>0