question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

JSON: forward slashes (e.g. in URL) are getting escaped

See original GitHub issue

Hello,

When I am trying to capture the url of my asp.net core application using JsonLayout the url of the site is being escaped.

NLog version: 4.5.11

NLog.Web.AspNetCore version: 4.8.0

Platform: .NET Core 2.2

Current NLog config (xml or C#, if relevant)

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      autoReload="true"
      internalLogLevel="Info"
      internalLogFile="c:\temp\internal-nlog.txt">

  <!-- enable asp.net core layout renderers -->
  <extensions>
    <add assembly="NLog.Web.AspNetCore"/>
  </extensions>

  <!-- the targets to write to -->
  <targets async="true">
    <!-- Write logs as Json into a file -->
    <target name="json-file" 
      xsi:type="File" 
      fileName="${basedir}/application-logs.json"
      archiveAboveSize="50000000" 
      archiveEvery="Day" 
      maxArchiveFiles="9"
      concurrentWrites="False">
      <layout xsi:type="JsonLayout">
        <attribute name="date" layout="${longdate}" />
        <attribute name="level" layout="${level:upperCase=true}"/>
        <attribute name="logger" layout="${logger}"/>
        <attribute name="identity" layout="${aspnet-user-identity}"/>
        <attribute name="url" layout="${aspnet-request-url:IncludePort=true:IncludeQueryString=true}" encode="true"/>
        <attribute name="host" layout="${aspnet-Request-Host}"/>
        <attribute name="userAgent" layout="${aspnet-Request-UserAgent}"/>
        <attribute name="referrer" layout="${aspnet-Request-Referrer}"/>
        <attribute name="action" layout="${aspnet-mvc-action}"/>
        <attribute name="clientIp" layout="${aspnet-request-ip}"/>
        <attribute name="message" layout="${message}" />
        <attribute name="exception" layout="${onexception:|${exception:format=ToString}}" />
      </layout>
    </target>

  </targets>
  <rules>
    <!--Skip non-critical Microsoft logs and so log only own logs-->
    <logger name="Microsoft.*" maxlevel="Info" final="true" /> <!-- BlackHole without writeTo -->
    
    <!-- Log all events to the json-file target -->
    <logger name="*" writeTo="json-file" minlevel="Trace" />
  </rules>
</nlog>
  • What is the current result? The output line looks like the following, notice the escaped slashes in the url:
{ "date": "2019-09-08 14:06:51.4229", "level": "DEBUG", "logger": "API.Controllers.ValuesController", "url": "https:\/\/localhost:5001\/api\/values", "action": "Get", "clientip": "::1", "message": "Some Log Goes Here", "exception": "|System.Exception: An exception" }
  • What is the expected result? The url would not be escaped.
{ "date": "2019-09-08 14:06:51.4229", "level": "DEBUG", "logger": "Miovision.QAP.API.Controllers.ValuesController", "url": "https://localhost:5001/api/values", "action": "Get", "clientip": "::1", "message": "Some Log Goes Here", "exception": "|System.Exception: An exception" }

I have tried to set the encoding to false but then the url is no longer wrapped in quotes, which is needed for me to consume the url correctly in a later step.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:19 (15 by maintainers)

github_iconTop GitHub Comments

2reactions
snakefootcommented, Nov 5, 2019

@kylelaverty + @sagebind NLog 4.6.8 has been released

https://www.nuget.org/packages/NLog

New setting EscapeForwardSlash has been added to JsonLayout.

1reaction
snakefootcommented, Dec 28, 2019

Created PR #3743 to resolve this. But it will only apply to sub-attributes.

If a nested attribute embeds a JsonLayout using encode="false", then it will not inherit the configuration from the top-JsonLayout (There is no guaranteed of the order of initializing NLog Layouts)

Read more comments on GitHub >

github_iconTop Results From Across the Web

JSON: why are forward slashes escaped?
JSON escapes the forward slash, so a hash {a: "a/b/c"} is serialized as {"a":"a\/b\/c"} instead of {"a":"a/b/c"} . Why? javascript · json.
Read more >
Keep escaped forward slashes in JSON string values with jq
According to the JSON specification, forward slashes don't have to be escaped with a backslash but they can be. I have a JSON...
Read more >
IT16439: JSON PARSER ESCAPES FORWARD SLASHES ...
When serializing a message tree the JSON parser escapes forward slash characters so that values such as "http://myURI" appear as.
Read more >
PAM escapes the forward slash '/' with a backslash '\' like ...
JSON spec does not mandate that forward slash not be escaped. The spec actually states that some fields may be escaped and forward...
Read more >
Why does XML to JSON insert a backslash in front o...
Those backslashes are escape characters. They are escaping the special characters inside of the string associated with JSON response. You have ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found