Skip to content

Commit

Permalink
JsonLayout - Apply EscapeForwardSlash for LogEventInfo.Properties (#4403
Browse files Browse the repository at this point in the history
)
  • Loading branch information
snakefoot authored Apr 23, 2021
1 parent 1826e90 commit ec8e03e
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/NLog/Layouts/JsonLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class JsonLayout : Layout
{
private LimitRecursionJsonConvert JsonConverter
{
get => _jsonConverter ?? (_jsonConverter = new LimitRecursionJsonConvert(MaxRecursionLimit, ConfigurationItemFactory.Default.JsonConverter));
get => _jsonConverter ?? (_jsonConverter = new LimitRecursionJsonConvert(ConfigurationItemFactory.Default.JsonConverter, MaxRecursionLimit, EscapeForwardSlash));
set => _jsonConverter = value;
}
private LimitRecursionJsonConvert _jsonConverter;
Expand All @@ -66,11 +66,11 @@ class LimitRecursionJsonConvert : IJsonConverter
readonly Targets.DefaultJsonSerializer _serializer;
readonly Targets.JsonSerializeOptions _serializerOptions;

public LimitRecursionJsonConvert(int maxRecursionLimit, IJsonConverter converter)
public LimitRecursionJsonConvert(IJsonConverter converter, int maxRecursionLimit, bool escapeForwardSlash)
{
_converter = converter;
_serializer = converter as Targets.DefaultJsonSerializer;
_serializerOptions = new Targets.JsonSerializeOptions() { MaxRecursionLimit = Math.Max(0, maxRecursionLimit) };
_serializerOptions = new Targets.JsonSerializeOptions() { MaxRecursionLimit = Math.Max(0, maxRecursionLimit), EscapeForwardSlash = escapeForwardSlash };
}

public bool SerializeObject(object value, StringBuilder builder)
Expand Down
4 changes: 2 additions & 2 deletions tests/NLog.UnitTests/Layouts/JsonLayoutTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1027,7 +1027,7 @@ public void EscapeForwardSlashDefaultTest()
<nlog throwExceptions='true'>
<targets>
<target name='debug' type='Debug' >
<layout type='JsonLayout' escapeForwardSlash='false'>
<layout type='JsonLayout' escapeForwardSlash='false' includeAllProperties='true'>
<attribute name='myurl1' layout='${event-properties:myurl}' />
<attribute name='myurl2' layout='${event-properties:myurl}' escapeForwardSlash='true' />
</layout>
Expand All @@ -1044,7 +1044,7 @@ public void EscapeForwardSlashDefaultTest()
logEventInfo1.Properties.Add("myurl", "http://hello.world.com/");
logger.Debug(logEventInfo1);

AssertDebugLastMessage("debug", "{ \"myurl1\": \"http://hello.world.com/\", \"myurl2\": \"http:\\/\\/hello.world.com\\/\" }");
AssertDebugLastMessage("debug", "{ \"myurl1\": \"http://hello.world.com/\", \"myurl2\": \"http:\\/\\/hello.world.com\\/\", \"myurl\": \"http://hello.world.com/\" }");
}

[Fact]
Expand Down

0 comments on commit ec8e03e

Please sign in to comment.