Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Utf8JsonWriter in EventSourceLogger #1227

Merged
merged 3 commits into from
Mar 7, 2019
Merged

Use Utf8JsonWriter in EventSourceLogger #1227

merged 3 commits into from
Mar 7, 2019

Conversation

pakrym
Copy link

@pakrym pakrym commented Mar 7, 2019

Fixes: #1013

Copy link

@analogrelay analogrelay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The unicode escape seems like it has a story behind it... but otherwise looks fine to me.

@@ -692,7 +692,7 @@ private static class EventTypes

{ "E5JS", (e) => VerifySingleEvent(e, "Logger2", EventTypes.MessageJson, 5, null, LogLevel.Critical,
@"""ArgumentsJson"":{""stringParam"":""bar"",""int1Param"":""23"",""int2Param"":""45""",
@"""ExceptionJson"":{""TypeName"":""System.Exception"",""Message"":""oops"",""HResult"":""-2146233088"",""VerboseMessage"":""System.Exception: oops ---> System.Exception: inner oops") },
@"""ExceptionJson"":{""TypeName"":""System.Exception"",""Message"":""oops"",""HResult"":""-2146233088"",""VerboseMessage"":""System.Exception: oops ---\u003e System.Exception: inner oops") },

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

I mean, it's the same, right? \u003e should be >

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, Utf8JsonWriter just decides to escape it for some reason. The end result is equivalent so I didn't spend too much time digging.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough :)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like https://github.com/dotnet/corefx/blob/3eacad602f7505f9f199dcd1992429506dd98330/src/System.Text.Json/src/System/Text/Json/Writer/JsonWriterHelper.Escaping.cs#L15-L17

        // Only allow ASCII characters between ' ' (0x20) and '~' (0x7E), inclusively,
        // but exclude characters that need to be escaped as hex: '"', '\'', '&', '+', '<', '>', '`'
        // and exclude characters that need to be escaped by adding a backslash: '\n', '\r', '\t', '\\', '/', '\b', '\f'

It's surprising to me that < needs to be escaped in JSON. @ahsonkhan, any particular reason for this? Mostly curious but it does seem like a user would be surprised to have it escaped in a string...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The JSON escaping here is following a strict mode to be SDL compliant. This is following the semantics of the default Javascript escaper which also escapes all non-ascii characters (and some ascii as well, including those that show up in html). This is primarily for defense in depth since JSON payloads could be embedded within html.

We currently do not have a way to customize the escaping behavior, but I imagine a writer option to plug in a custom escaper. Something that would work with https://github.com/dotnet/corefx/issues/34830

Some related issues:
https://github.com/dotnet/corefx/issues/34958
https://github.com/dotnet/corefx/issues/35385

cc @GrabYourPitchforks

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds fine to me. I mostly just wanted to confirm that this was an intentional choice because it was a little surprising. Thanks!

var sw = new StringWriter();
var writer = new JsonTextWriter(sw);
writer.DateFormatString = "O"; // ISO 8601
var arrayBufferWriter = new ArrayBufferWriter<byte>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm... using an internal type, which wasn't meant to be used here. But hey, source package :)

</PropertyGroup>

<ItemGroup>
<Compile Include="../../shared/*.cs" />

<Reference Include="Microsoft.Bcl.Json.Sources">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this library have to support netstandard2.0?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it was netstandard2.0 in 2.2 and I don't think we have plans to change that. Most of Microsoft.Extensions.* is designed to remain netstandard2.0

@natemcmaster natemcmaster deleted the pakrym/utf8json branch May 3, 2019 06:34
maryamariyan pushed a commit to maryamariyan/runtime that referenced this pull request Feb 28, 2020
maryamariyan pushed a commit to maryamariyan/runtime that referenced this pull request Mar 2, 2020
maryamariyan pushed a commit to maryamariyan/runtime that referenced this pull request Mar 11, 2020
maryamariyan pushed a commit to maryamariyan/runtime that referenced this pull request Mar 27, 2020
@ghost ghost locked as resolved and limited conversation to collaborators May 29, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants