-
Notifications
You must be signed in to change notification settings - Fork 6.1k
CA2254 documentation does not explain how to prevent the default escaping behavior #30018
Description
The CA2254 documentation does not provide guidance on avoiding the default escaping behavior.
We have a product where we'd like to dump the (JSON) config file into the log. This way, bug reports that come with logs allow us to copy the file out of the log and into a new local file for testing.
The current code looks like
JObject parsedAppSettings = JObject.Parse(File.ReadAllText("appsettings.json"));
Logger.LogInformation("Appsettings.json file configuration on start up:\n" + parsedAppSettings); // CA2254 hereThis triggers CA2254, but all the fixes I've found emit escaped JSON { \"name\" : \"value\" } instead of raw JSON { "name" : "value" }. This makes it unpleasant at best to copy the configuration out of the logs and into a local file for testing.
I've seen some notes that the format string is similar to string.Format, but I'm not aware of any way that string.Format can be convinced to perform this sort of escaping.
What is the recommended method to avoid this default escaping behavior without just #pragma warning disable CA2254ing?
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
- ID: f5662dc7-dcf6-47d5-9340-55b22ebfeca8
- Version Independent ID: 0df9ae80-f763-ba60-a876-a98e86d64887
- Content: CA2254: Template should be a static expression - .NET
- Content Source: docs/fundamentals/code-analysis/quality-rules/ca2254.md
- Product: dotnet-fundamentals
- GitHub Login: @Youssef1313
- Microsoft Alias: gewarren