ReSharper has the fantastic StringFormatMethodAttribute
annotation which provides code analysis for format strings and their parameters. It can be added to any custom string format methods like this:
[StringFormatMethod("format")]
void DebugFormat(string format, params object[] args);
This works well when using integer placeholders, eg:
x.DebugFormat("ReSharper is {0} for {1} placeholders", "great", "numbered");
However, some libraries (like Serilog and ASP.Net Core / Microsoft.Extensions.Logging ) offer named format placeholders which are used for (semantic | structured) logging and this seems to be something that ReSharper does not (yet) offer. For example, a line of code like this:
x.DebugFormat("ReSharper is {Quality} for {Style} placeholders", "poor?", "named");
Results in the ReSharper code analysis warning
Format item index must be a number starting 0 without leading or trailing whitespaces
Searching on ReSharper support, this site, and Google draws a blank. Has anyone found a way to get string format annotations working with named placeholders?
Relevant JetBrains links:
I have implemented an R# extension for this. Download it here from JetBrains.
It is also hosted on GitHub .
This extension highlights structured logging templates and contains some useful analyzers. Here is how the structured logging events look with the plugin installed:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.