Log4PostSharp 项目通过继承现成的(和工作的)自定义属性

发布于 2024-09-17 18:15:30 字数 1210 浏览 10 评论 0原文

需要通过继承现成的(和工作的)自定义属性来修改 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

阳光下的泡沫是彩色的 2024-09-24 18:15:30

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 of GetAnnotationsOfType() was false, meaning that only LogAttribute annotations should be discovered. Changing it to true caused all the descendants of LogAttribute (including my LogWithExceptionAttribute) to be considered.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文