Skip to content

CA2254 documentation does not explain how to prevent the default escaping behavior #30018

@DaleStan

Description

@DaleStan

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 here

This 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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions