对于我的 log4net 解决方案,我有一个使用 CallerInfo 属性的 API 包装器,例如
public void Write(string message,
[CallerMemberName] string memberName = "",
[CallerFilePath] string filePath = "",
[CallerLineNumber] int lineNumber = 0)
但是,我也在使用 Unity 拦截,以便我可以执行之前/之后响应的跟踪记录,例如在 Invoke 方法中使用 ICallHandler,如下所示。
public class TraceCallHandler : ICallHandler
{
...
public IMethodReturn Invoke(IMethodInvocation input,
GetNextHandlerDelegate getNext)
{
//---- Trace method inputs
this.LogInfoBeforeInvoke(input);
//---- invoking the target method
InvokeHandlerDelegate next = getNext();
IMethodReturn methodReturn = next(input, getNext);
//---- invoking the target method
this.LogInfoAfterInvoke(methodReturn.ReturnValue);
}
}
注意:上面的代码绝不是完整/正确的......只是想向您展示我为 Unity Interception 所做的工作。
我的问题/挑战是:当我最终调用 log.Write(...) 时,我想要目标的调用者信息,而不是我的 TraceCallHandler 信息。
例如对于方法名称,我可以这样做:
string methodName = input.MethodBase.Name;
如何获取来电者的文件路径和来电者的行号?甚至可以通过反射来实现吗?
谢谢!