Log4PostSharp 项目通过继承现成的(和工作的)自定义属性
需要通过继承现成的(和工作的)自定义属性来修改 Log4PostSharp 项目。它看起来与此类似:
[AttributeUsage(
AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Constructor | AttributeTargets.Method | AttributeTargets.Module | AttributeTargets.Struct,
AllowMultiple = true,
Inherited = false)]
[MulticastAttributeUsage(
MulticastTargets.InstanceConstructor | MulticastTargets.StaticConstructor | MulticastTargets.Method,
AllowMultiple = true)]
#if SILVERLIGHT
[RequirePostSharp("Log4PostSharp", "Log4PostSharp.Weaver.SilverlightLogTask")]
#else
[RequirePostSharp("Log4PostSharp", "Log4PostSharp.Weaver.LogTask")]
[Serializable]
#endif
public sealed class LogWithExceptionAttribute : LogAttribute
{
public LogWithExceptionAttribute()
{
ExceptionLevel = LogLevel.Error;
}
...
}
这就是我选择注释一些单元测试代码的方式:
[LogWithException]
private void DoThrowException()
{
throw new Exception("test exception");
}
想要尝试调试编译时过程,尝试从命令行编译但无法得到任何提示:
> msbuild Log4PostSharp.Test.csproj /p:PostSharpBuild=Diag /p:PostSharpTrace="AssemblyBinding;Project" /v:detailed
哪个是解决问题的正确方法?我想做的事情是错误的吗?
Need to modify Log4PostSharp project by inheriting from the ready-made (and working) custom attribute. It looks similar to this:
[AttributeUsage(
AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Constructor | AttributeTargets.Method | AttributeTargets.Module | AttributeTargets.Struct,
AllowMultiple = true,
Inherited = false)]
[MulticastAttributeUsage(
MulticastTargets.InstanceConstructor | MulticastTargets.StaticConstructor | MulticastTargets.Method,
AllowMultiple = true)]
#if SILVERLIGHT
[RequirePostSharp("Log4PostSharp", "Log4PostSharp.Weaver.SilverlightLogTask")]
#else
[RequirePostSharp("Log4PostSharp", "Log4PostSharp.Weaver.LogTask")]
[Serializable]
#endif
public sealed class LogWithExceptionAttribute : LogAttribute
{
public LogWithExceptionAttribute()
{
ExceptionLevel = LogLevel.Error;
}
...
}
This is how I've chosen to annotate some unit-test code:
[LogWithException]
private void DoThrowException()
{
throw new Exception("test exception");
}
Wanted to try to debug the compile-time process, tried to compile from the command line but couldn't get any hint:
> msbuild Log4PostSharp.Test.csproj /p:PostSharpBuild=Diag /p:PostSharpTrace="AssemblyBinding;Project" /v:detailed
Which is the right way to tackle the problem? What is the wrong thing I am trying to do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
Log4PostSharp.Weaver.LogTask::ProvideAdvice()
中,有以下行:var customAttributeEnumerator = customAttributeDictionaryTask.GetAnnotationsOfType(typeof(LogAttribute), true);
。GetAnnotationsOfType()
的第二个属性是false
,这意味着只应发现LogAttribute
注释。将其更改为 true 会导致考虑 LogAttribute 的所有后代(包括我的 LogWithExceptionAttribute)。In
Log4PostSharp.Weaver.LogTask::ProvideAdvice()
there was a following line:var customAttributeEnumerator = customAttributeDictionaryTask.GetAnnotationsOfType(typeof(LogAttribute), true);
. The second attribute ofGetAnnotationsOfType()
wasfalse
, meaning that onlyLogAttribute
annotations should be discovered. Changing it totrue
caused all the descendants ofLogAttribute
(including myLogWithExceptionAttribute
) to be considered.