Filtering rules based on functionality

82 views Asked by At

I am pretty sure what I am about to ask is not possible, but I am hoping experts on Code Analysis may be able to suggest a workaround.

I am trying to find a way to exclude Code Analysis warning in GlobalSuppressions.cs based on functionality. For example, I would like to disable

"Microsoft.Globalization", "CA1305:SpecifyIFormatProvider"

in ****all**** of my logging statements (I use CommonLogging facade), so signature would be something like:

Common.Logging.ILog.Trace(System.Action<Common.Logging.FormatMessageHandler>)

I would like to do this everywhere throughout the project regardless of the type, namespace, or method name....

Looking at other answers, this seems to be impossible for now...Or is it?

1

There are 1 answers

1
jessehouwing On BEST ANSWER

This is indeed not possible. When you call a method that has both an overload that accepts string and one that accepts string, IFormatProvider, this rule will trigger. And it probably should, since I expect you either want Culture Sensitive or Culture Insensitive logfiles. In which case Code Analysis forces you to make that choice.

What you could do, is write your own rule and disable this one. Or you could fix the violations and get them out of the way. A quick regex search+replace can probably fix these for you in a matter of seconds.

Or you can write one class that acts as a Proxy/Facade between your code and that of Common.Logging and which only accepts the string variant. You can then refactor your code to use your method. That way you only have to fix one violation, which will remain in the newly created facade.