如何向 PostSharp 属性添加参数?
我有一个简单的 PostSharp 日志记录属性:
[Serializable]
public class MethodLoggingAttribute : OnMethodBoundaryAspect
{
private ILog _logger;
public override void OnEntry(MethodExecutionEventArgs eventArgs)
{
_logger = LogManager.GetLogger(eventArgs.Method.DeclaringType.ToString());
_logger.DebugFormat("Entered {0}", eventArgs.Method.Name);
}
}
我想通过向日志条目添加有关方法参数的信息来使该属性更加灵活,但仅在需要时才这样做。
[Serializable]
public class MethodLoggingAttribute : OnMethodBoundaryAspect
{
private ILog _logger;
public override void OnEntry(MethodExecutionEventArgs eventArgs)
{
_logger = LogManager.GetLogger(eventArgs.Method.DeclaringType.ToString());
// if ShowParameters = true
_logger.DebugFormat("Entered {0} with args:{1}", eventArgs.Method.Name, args);
// else
_logger.DebugFormat("Entered {0}", eventArgs.Method.Name);
// endif
}
}
我不知道如何执行带有 IF 的伪代码。 我如何将其传递到属性中? 我期望它看起来像这样,但我不知道如何在属性代码中处理它:
[MethodLogging(ShowParameters=true)]
public void SomeCrazyMethod(int CustomerId, string SecretName) {...}
I have a simple PostSharp logging attribute:
[Serializable]
public class MethodLoggingAttribute : OnMethodBoundaryAspect
{
private ILog _logger;
public override void OnEntry(MethodExecutionEventArgs eventArgs)
{
_logger = LogManager.GetLogger(eventArgs.Method.DeclaringType.ToString());
_logger.DebugFormat("Entered {0}", eventArgs.Method.Name);
}
}
I want to make this attribute more flexible by adding information about the methods arguments to the log entry, but only if its needed.
[Serializable]
public class MethodLoggingAttribute : OnMethodBoundaryAspect
{
private ILog _logger;
public override void OnEntry(MethodExecutionEventArgs eventArgs)
{
_logger = LogManager.GetLogger(eventArgs.Method.DeclaringType.ToString());
// if ShowParameters = true
_logger.DebugFormat("Entered {0} with args:{1}", eventArgs.Method.Name, args);
// else
_logger.DebugFormat("Entered {0}", eventArgs.Method.Name);
// endif
}
}
The pseudo code with the IF is what I'm not sure how to do. How can I pass this into the attribute? I'm expecting it to look something like this but I don know how to handle it inside the attribute code:
[MethodLogging(ShowParameters=true)]
public void SomeCrazyMethod(int CustomerId, string SecretName) {...}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需声明一个属性,
然后您就可以按照您提到的方式指定它。
Just declare a property
Then you can specify it in the way you have mentioned.