Domanda

Whenever I call my logger in a method, e.g.

_logger.Debug("Connecting to database...");

I get the warning:

CA1303 : Microsoft.Globalization: 
Method 'Database.Connect()' passes a literal 
string as parameter  'message' of a call to 'ILogger.Debug(string)'. 
Retrieve the following string(s) from a resource table instead: 
"Connecting to database...".

Is there a way to suppress this warning every time I use a function of ILogger? I really don't want to suppress it in every method I'm using it.

È stato utile?

Soluzione

Neither FxCop/VS Code Analysis nor the CA1303 rule are configurable to ignore particular targets in this way. You basically have three options:

  1. Suppress the violations individually,
  2. Disable the rule, or
  3. Substitute a custom rule that behaves the way you would prefer.

I tend to lean toward #3 for this sort of thing, but ymmv... Also, if you feel strongly that you ought to be able to control the CA1303 behaviour, this is something to consider requesting at https://connect.microsoft.com/VisualStudio or http://visualstudio.uservoice.com/forums/121579-visual-studio.

Altri suggerimenti

If you control the ILogger interface, you can leverage the Localizable attribute with a value of false to indicate the value is not localizable.

For example:

void Info([Localizable(false)] string message);

I ran into this issue and found that the easiest solution was to rename my logging method's parameter from "message" to something else.

CA1303 will only trigger if the relevant parameter or property name contains "Text", "Message" or "Caption". If the parameter is passed to Console.Write or Console.WriteLine, the parameter also cannot be named "value" or "format".

If you apply the GeneratedCode attribute to a class, Code Analysis will not analyze your class.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top