0
Mono.Cecil을 사용하여 실제 메서드를 편집하지 않고 해당 메서드의 진입 점을 기록 할 수 있도록 대상 메서드의 IL 코드를 편집하고 있습니다. 로깅 작업을 수행 할 수있는 메소드에 호출 명령어를 삽입 할 수 있습니다. 그러나 대상 메서드의 입력 매개 변수를 기록하는 방법을 모르겠습니다. 간단히 말해서, 대상 메서드에 명령을 삽입하려면 IL 코드를 변경하여 로그를 수행하거나 인쇄 작업을 호출하여 해당 메서드에 전달 된 입력 매개 변수 값을 기록하십시오.Mono.Cecil : 메서드 시작 부분에 log 문을 삽입하십시오.
샘플로 기본 프로그램을 사용해 보았습니다.
public class Target
{
// My target method.
public void Run(int arg0, string arg1)
{
Console.WriteLine("Run method body");
}
}
public static class Trace{
// This is my log method, which i want to call in begining of Run() method.
public void LogEntry(string methodName, object[] params)
{
System.Console.WriteLine("******Entered in "+ methodName+" method.***********")
// With params :......
//
}
}
소스 프로그램.
public class Sample
{
private readonly string _targetFileName;
private readonly ModuleDefinition _module;
public ModuleDefinition TargetModule { get { return _module; } }
public Sample(string targetFileName)
{
_targetFileName = targetFileName;
// Read the module with default parameters
_module = ModuleDefinition.ReadModule(_targetFileName);
}
public void Run(string type, string method)
{
// Retrive the target class.
var targetType = _module.Types.Single(t => t.Name == type);
// Retrieve the target method.
var runMethod = targetType.Methods.Single(m => m.Name == method);
// Get a ILProcessor for the Run method
var processor = runMethod.Body.GetILProcessor();
// get log entry method ref to create instruction
var logEntryMethodReference = targetType.Methods.Single(m => m.Name == "LogEntry");
// Import ..
//
var newInstruction = processor.Create(OpCodes.Call, logEntryMethodReference);
var firstInstruction = runMethod.Body.Instructions[0];
processor.InsertBefore(firstInstruction, newInstruction);
// Write the module with default parameters
_module.Write(_targetFileName);
}
}
이것은 가출 소리 ting,하지만 일부 샘플 코드가 없으면 우리 중 누구도 도움을 줄 수 없다고 생각합니다 :) 지금까지 가지고있는 것을 게시하십시오 (그리고 호색하지 않아도되지만, 이것은 보통 여러분이 가지고있는 모든 질문에 대해 사실입니다. 지금; 다른 사람, 덜 이해하는 사람들은 이러한 질문에 대한 소스 코드가 누락 된 경우에도 부정 투표를 할 수 있습니다.) –
@Akos 업데이트했습니다. – Krishnan
괜찮습니까? 여기서'targetType'은'Target'이지만'LogEntry' 메쏘드는'Trace' 클래스에 있습니다 (클래스와 메쏘드는 정적이어야합니다). 'targetType.Methods.Single()'을 실행하고'LogEntry'를 찾으면, 그것은'Trace'가 아니라'Target'에서 메소드를 찾습니다. 또는 나는 무엇인가 놓치고 있냐? –