构建新异常时抑制 CA1303 fxcop 警告

Suppress CA1303 fxcop warning when constructing a new Exception

当我在 ASP.NET Core 3.1 代码中抛出异常时,fxcop 在将字符串文字视为 new Exception() 的参数时向我发出警告。例如:

throw new InvalidOperationException("Ouch");

给我 CA1303: Do not pass literals as localized parameters

一般来说,我不会向最终用户显示异常消息,因此我不想对它们进行本地化。有没有办法配置 CA1303,使其忽略从 System.Exception?

派生的任何内容的构造函数参数

编辑:

经过更多搜索,我找到了关于这个问题的对话:

https://github.com/dotnet/roslyn-analyzers/issues/2933

如果您使用的是 .editor 配置,您可以这样做:dotnet_code_quality.CA1303.use_naming_heuristic = true 或者完全禁用 CA1303。

Microsoft.CodeAnalysis.FxCopAnalyzers 的 3.3.0 版终于修复了调用特定类型时抑制警告的功能。

为了在构建 Exception 或调用 ILogger 函数时抑制 CA1303,我在解决方案的根目录中添加了一个 .editorconfig 文件(所有项目是该目录的子目录)并向其中添加以下行:

[*.cs]
dotnet_code_quality.CA1303.excluded_type_names_with_derived_types = Exception|LoggerExtensions|ILogger

这告诉 CA1303 忽略对 ExceptionLoggerExtensionsILogger(以及派生自这些类型的任何类型)的调用。

作为参考,请参阅此问题及其下方的回复:
https://github.com/dotnet/roslyn-analyzers/issues/2933#issuecomment-627256340