抑制 DebugFormat InfoFormat 和类似调用的 CA1305

发布于 2024-11-16 03:56:17 字数 2377 浏览 12 评论 0原文

我正在使用 Common.Logging for .NET。当我使用 XXXFormat 函数之一(如 DebugFormat 或 InfoFormat)时,我得到 CA1305 警告。通过右键单击并在代码中说要抑制或添加到项目抑制文件中来抑制很容易。我想在 GlobalSuppressions.cs 中添加一行,以抑制对 DebugFormat 的所有调用(以及 InfoFormat、TraceFormat 等的单独行)出现此警告。到目前为止,我还不知道该怎么做。当我右键单击警告并选择抑制消息时 ->在项目抑制文件中,添加了以下内容:

[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", MessageId = "Common.Logging.ILog.DebugFormat(System.String,System.Object[])", Scope = "member", Target = "My.Full.Namespace.Class.#MyFunctionName(int)")]

如您所见,抑制创建了一个程序集级属性来抑制消息。在属性本身内,Scope 设置为“member”,Target 设置为命名空间限定类名“dot”方法签名。这确实抑制了该函数中调用 DebugFormat 的消息,但我想抑制所有调用 DebugFormat 的消息。我尝试删除目标并将范围更改为“模块”(和“程序集” - 不确定这是否有效),但我仍然无法针对所有出现的 DebugFormat 抑制此警告。

理想情况下,我想创建一个 GlobalSuppressions.cs 文件,其中至少包含以下条目(或类似条目):

[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", MessageId = "Common.Logging.ILog.TraceFormat(System.String,System.Object[])", Scope = "module",)] 
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", MessageId = "Common.Logging.ILog.DebugFormat(System.String,System.Object[])", Scope = "module",)] 
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", MessageId = "Common.Logging.ILog.InfoFormat(System.String,System.Object[])", Scope = "module",)] 
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", MessageId = "Common.Logging.ILog.WarnFormat(System.String,System.Object[])", Scope = "module",)] 
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", MessageId = "Common.Logging.ILog.ErrorFormat(System.String,System.Object[])", Scope = "module",)] 
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", MessageId = "Common.Logging.ILog.FatalFormat(System.String,System.Object[])", Scope = "module",)] 

有人知道如何执行此操作吗?

谢谢。

I am using Common.Logging for .NET. When I use one of the XXXFormat functions like DebugFormat or InfoFormat, I get the CA1305 warning. It is easy enough to suppress by right clicking and either saying to suppress in code or to add to project suppressions file. I would like to add one line to GlobalSuppressions.cs to suppress all occurrences of this warning for all calls to DebugFormat (and separate lines for InfoFormat, TraceFormat, etc). So far, I can't figure out how to do it. When I right click the warning and choose to Suppress Message -> In Project Suppressions file, this is what gets added:

[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", MessageId = "Common.Logging.ILog.DebugFormat(System.String,System.Object[])", Scope = "member", Target = "My.Full.Namespace.Class.#MyFunctionName(int)")]

As you can see, the suppression has created an assembly-level attribute to suppress the message. Within the attribute itself, Scope is set to "member" and Target is set to the namespace qualified classname "dot" method signature. This does indeed suppress the message for calls to DebugFormat in that function, but I want to suppress the message for all calls to DebugFormat. I have tried removing Target and changing Scope to "module" (and "assembly" - not sure if that is valid), but I still cannot get this warning suppressed for all occurrences of DebugFormat.

Ideally, I want to create a GlobalSuppressions.cs file that has at least these entries in it (or similar):

[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", MessageId = "Common.Logging.ILog.TraceFormat(System.String,System.Object[])", Scope = "module",)] 
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", MessageId = "Common.Logging.ILog.DebugFormat(System.String,System.Object[])", Scope = "module",)] 
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", MessageId = "Common.Logging.ILog.InfoFormat(System.String,System.Object[])", Scope = "module",)] 
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", MessageId = "Common.Logging.ILog.WarnFormat(System.String,System.Object[])", Scope = "module",)] 
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", MessageId = "Common.Logging.ILog.ErrorFormat(System.String,System.Object[])", Scope = "module",)] 
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", MessageId = "Common.Logging.ILog.FatalFormat(System.String,System.Object[])", Scope = "module",)] 

Does anyone know how to do this?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

叹沉浮 2024-11-23 03:56:17

不幸的是,现有的 FxCop 版本不支持这样的分层抑制。每个规则违规实例都需要一个相应的 SuppressMessageAttribute 实例。如果您不想使用该规则,请将其禁用。如果您不希望它应用于某些方法,您基本上有三个选择:

  1. 为每个违规添加抑制,
  2. 将规则替换为忽略您想要忽略的方法的自定义规则,或者
  3. 不直接调用这些方法,更改您的代码以调用应用正确区域性的外观方法。

就我个人而言,我会选择#3,但是 ymmv...

Unfortunately, hierarchical suppressions like this aren't supported in existing FxCop versions. Each rule violation instance needs a corresponding SuppressMessageAttribute instance. If you don't want to use the rule, disable it. If you don't want it to apply to certain methods, you basically have three options:

  1. Add a suppression for each violation,
  2. Replace the rule by a custom rule that ignores methods that you want to ignore, or
  3. Instead of calling these methods directly, change your code to call facade methods that apply the correct culture.

Personally, I would opt for #3, but ymmv...

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