Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace NLog.Extensions.Logging
[ThreadAgnostic]
class MicrosoftConsoleLayoutRenderer : LayoutRenderer
{
private static readonly string[] EventIdMapper = Enumerable.Range(0, 50).Select(id => id.ToString(System.Globalization.CultureInfo.InvariantCulture)).ToArray();
private static readonly string[] EventIdMapper = Enumerable.Range(0, 512).Select(id => id.ToString(System.Globalization.CultureInfo.InvariantCulture)).ToArray();

/// <summary>
/// Gets or sets format string used to format timestamp in logging messages. Defaults to <c>null</c>.
Expand Down
29 changes: 27 additions & 2 deletions src/NLog.Extensions.Logging/Layouts/MicrosoftConsoleJsonLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace NLog.Extensions.Logging
[ThreadAgnostic]
public class MicrosoftConsoleJsonLayout : JsonLayout
{
private static readonly string[] EventIdMapper = Enumerable.Range(0, 50).Select(id => id.ToString(System.Globalization.CultureInfo.InvariantCulture)).ToArray();
private static readonly string[] EventIdMapper = Enumerable.Range(0, 512).Select(id => id.ToString(System.Globalization.CultureInfo.InvariantCulture)).ToArray();

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

Expand Down Expand Up @@ -47,7 +47,7 @@ public IList<JsonAttribute>? StateAttributes
get
{
var index = LookupNamedAttributeIndex("State");
return index >= 0 ? (Attributes[index]?.Layout as JsonLayout)?.Attributes : null;
return index >= 0 ? (Attributes[index]?.Layout as JsonLayout)?.Attributes : new List<JsonAttribute>();
}
}

Expand Down Expand Up @@ -99,6 +99,31 @@ public string? TimestampFormat
}
}

/// <inheritdoc />
protected override void InitializeLayout()
{
IncludeEventProperties = false;
IncludeScopeProperties = false;

var stateIndex = LookupNamedAttributeIndex("State");
var stateJsonLayout = stateIndex >= 0 ? Attributes[stateIndex]?.Layout as JsonLayout : null;
if (stateJsonLayout != null)
{
stateJsonLayout.MaxRecursionLimit = MaxRecursionLimit;
stateJsonLayout.ExcludeEmptyProperties = ExcludeEmptyProperties;
stateJsonLayout.SuppressSpaces = SuppressSpaces && !IndentJson;
if (ExcludeProperties?.Count > 0)
{
foreach (var excludeProperty in ExcludeProperties)
{
stateJsonLayout.ExcludeProperties.Add(excludeProperty);
}
}
}

base.InitializeLayout();
}

private int LookupNamedAttributeIndex(string attributeName)
{
for (int i = 0; i < Attributes.Count; ++i)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ public void MicrosoftConsoleJsonLayout_TimestampFormat()
Assert.Equal($"{{ \"Timestamp\": \"{logEvent.TimeStamp.ToUniversalTime().ToString("R")}\", \"EventId\": {0}, \"LogLevel\": \"Error\", \"Category\": \"MyLogger\", \"Message\": \"Hello World\", \"State\": {{ \"{{OriginalFormat}}\": \"Hello World\" }} }}", result);
}

[Fact]
public void MicrosoftConsoleJsonLayout_MaxRecursionLimit()
{
var layout = new MicrosoftConsoleJsonLayout() { MaxRecursionLimit = 0, TimestampFormat = null };
var logEvent = new LogEventInfo(LogLevel.Error, "MyLogger", "Hello World");
logEvent.Properties["Planet"] = new { Name = "Earth", Location = new { Galaxy = "Milky Way" } };
var result = layout.Render(logEvent);
Assert.Equal("{ \"EventId\": 0, \"LogLevel\": \"Error\", \"Category\": \"MyLogger\", \"Message\": \"Hello World\", \"State\": { \"{OriginalFormat}\": \"Hello World\", \"Planet\": \"{ Name = Earth, Location = { Galaxy = Milky Way } }\" } }", result);
}

[Fact]
public void MicrosoftConsoleJsonLayout_ExceptionEvent()
{
Expand Down