Suppress CA1305 for DebugFormat InfoFormat and similar calls
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 开发者_StackOverflow社区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.
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:
- Add a suppression for each violation,
- Replace the rule by a custom rule that ignores methods that you want to ignore, or
- 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...
精彩评论