Skip to content

Commit 466f102

Browse files
authored
MicrosoftConsoleJsonLayout - Forward MaxRecursionLimit to State-JsonLayout (#826)
1 parent e4872a7 commit 466f102

3 files changed

Lines changed: 38 additions & 3 deletions

File tree

src/NLog.Extensions.Logging/LayoutRenderers/MicrosoftConsoleLayoutRenderer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace NLog.Extensions.Logging
1313
[ThreadAgnostic]
1414
class MicrosoftConsoleLayoutRenderer : LayoutRenderer
1515
{
16-
private static readonly string[] EventIdMapper = Enumerable.Range(0, 50).Select(id => id.ToString(System.Globalization.CultureInfo.InvariantCulture)).ToArray();
16+
private static readonly string[] EventIdMapper = Enumerable.Range(0, 512).Select(id => id.ToString(System.Globalization.CultureInfo.InvariantCulture)).ToArray();
1717

1818
/// <summary>
1919
/// Gets or sets format string used to format timestamp in logging messages. Defaults to <c>null</c>.

src/NLog.Extensions.Logging/Layouts/MicrosoftConsoleJsonLayout.cs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace NLog.Extensions.Logging
1515
[ThreadAgnostic]
1616
public class MicrosoftConsoleJsonLayout : JsonLayout
1717
{
18-
private static readonly string[] EventIdMapper = Enumerable.Range(0, 50).Select(id => id.ToString(System.Globalization.CultureInfo.InvariantCulture)).ToArray();
18+
private static readonly string[] EventIdMapper = Enumerable.Range(0, 512).Select(id => id.ToString(System.Globalization.CultureInfo.InvariantCulture)).ToArray();
1919

2020
private readonly SimpleLayout _timestampLayout = new SimpleLayout("\"${date:format=o:universalTime=true}\"");
2121

@@ -47,7 +47,7 @@ public IList<JsonAttribute>? StateAttributes
4747
get
4848
{
4949
var index = LookupNamedAttributeIndex("State");
50-
return index >= 0 ? (Attributes[index]?.Layout as JsonLayout)?.Attributes : null;
50+
return index >= 0 ? (Attributes[index]?.Layout as JsonLayout)?.Attributes : new List<JsonAttribute>();
5151
}
5252
}
5353

@@ -99,6 +99,31 @@ public string? TimestampFormat
9999
}
100100
}
101101

102+
/// <inheritdoc />
103+
protected override void InitializeLayout()
104+
{
105+
IncludeEventProperties = false;
106+
IncludeScopeProperties = false;
107+
108+
var stateIndex = LookupNamedAttributeIndex("State");
109+
var stateJsonLayout = stateIndex >= 0 ? Attributes[stateIndex]?.Layout as JsonLayout : null;
110+
if (stateJsonLayout != null)
111+
{
112+
stateJsonLayout.MaxRecursionLimit = MaxRecursionLimit;
113+
stateJsonLayout.ExcludeEmptyProperties = ExcludeEmptyProperties;
114+
stateJsonLayout.SuppressSpaces = SuppressSpaces && !IndentJson;
115+
if (ExcludeProperties?.Count > 0)
116+
{
117+
foreach (var excludeProperty in ExcludeProperties)
118+
{
119+
stateJsonLayout.ExcludeProperties.Add(excludeProperty);
120+
}
121+
}
122+
}
123+
124+
base.InitializeLayout();
125+
}
126+
102127
private int LookupNamedAttributeIndex(string attributeName)
103128
{
104129
for (int i = 0; i < Attributes.Count; ++i)

test/NLog.Extensions.Logging.Tests/MicrosoftConsoleJsonLayoutTests.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ public void MicrosoftConsoleJsonLayout_TimestampFormat()
2323
Assert.Equal($"{{ \"Timestamp\": \"{logEvent.TimeStamp.ToUniversalTime().ToString("R")}\", \"EventId\": {0}, \"LogLevel\": \"Error\", \"Category\": \"MyLogger\", \"Message\": \"Hello World\", \"State\": {{ \"{{OriginalFormat}}\": \"Hello World\" }} }}", result);
2424
}
2525

26+
[Fact]
27+
public void MicrosoftConsoleJsonLayout_MaxRecursionLimit()
28+
{
29+
var layout = new MicrosoftConsoleJsonLayout() { MaxRecursionLimit = 0, TimestampFormat = null };
30+
var logEvent = new LogEventInfo(LogLevel.Error, "MyLogger", "Hello World");
31+
logEvent.Properties["Planet"] = new { Name = "Earth", Location = new { Galaxy = "Milky Way" } };
32+
var result = layout.Render(logEvent);
33+
Assert.Equal("{ \"EventId\": 0, \"LogLevel\": \"Error\", \"Category\": \"MyLogger\", \"Message\": \"Hello World\", \"State\": { \"{OriginalFormat}\": \"Hello World\", \"Planet\": \"{ Name = Earth, Location = { Galaxy = Milky Way } }\" } }", result);
34+
}
35+
2636
[Fact]
2737
public void MicrosoftConsoleJsonLayout_ExceptionEvent()
2838
{

0 commit comments

Comments
 (0)