Skip to content

Commit

Permalink
fix(telemetry transmission): Include ILogger CategoryName in logger o…
Browse files Browse the repository at this point in the history
…utput

Previously this field was omitted, which was not consistent with normal ILogger behavior, or with Application Insights classic behavior.

fixes: Azure#44746
  • Loading branch information
johncrim committed Jun 27, 2024
1 parent ce720b9 commit 8d7472f
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ internal static List<TelemetryItem> OtelToAzureMonitorLogs(Batch<LogRecord> batc

logRecord.ForEachScope(s_processScope, properties);

var categoryName = logRecord.CategoryName;
if (!string.IsNullOrEmpty(categoryName))
{
properties.Add("CategoryName", categoryName.Truncate(SchemaConstants.KVP_MaxValueLength)!);
}

if (logRecord.EventId.Id != 0)
{
properties.Add("EventId", logRecord.EventId.Id.ToString(CultureInfo.InvariantCulture));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void VerifyLog(LogLevel logLevel, string expectedSeverityLevel)
telemetryItem: telemetryItem!,
expectedSeverityLevel: expectedSeverityLevel,
expectedMessage: "Hello {name}.",
expectedMessageProperties: new Dictionary<string, string> { { "name", "World" }},
expectedMessageProperties: new Dictionary<string, string> { { "name", "World" }, { "CategoryName", logCategoryName } },
expectedSpanId: null,
expectedTraceId: null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void MessageIsSetToExceptionMessage(bool parseStateValues)
Assert.Equal("tomato", name);
Assert.True(properties.TryGetValue("price", out string price));
Assert.Equal("2.99", price);
Assert.Equal(3, properties.Count);
Assert.Equal(4, properties.Count);
}

[Fact]
Expand Down Expand Up @@ -91,7 +91,7 @@ public void MessageIsSetToFormattedMessageWhenIncludeFormattedMessageIsSet()
Assert.Equal("tomato", name);
Assert.True(properties.TryGetValue("price", out string price));
Assert.Equal("2.99", price);
Assert.Equal(2, properties.Count);
Assert.Equal(3, properties.Count);
}

[Fact]
Expand Down Expand Up @@ -121,7 +121,7 @@ public void MessageIsSetToOriginalFormatWhenIncludeFormattedMessageIsNotSet()
Assert.Equal("tomato", name);
Assert.True(properties.TryGetValue("price", out string price));
Assert.Equal("2.99", price);
Assert.Equal(2, properties.Count);
Assert.Equal(3, properties.Count);
}

[Theory]
Expand Down Expand Up @@ -152,7 +152,7 @@ public void PropertiesContainFieldsFromStructuredLogs(bool parseStateValues)
Assert.Equal("tomato", name);
Assert.True(properties.TryGetValue("price", out string price));
Assert.Equal("2.99", price);
Assert.Equal(2, properties.Count);
Assert.Equal(3, properties.Count);
}

[Fact]
Expand Down Expand Up @@ -180,7 +180,33 @@ public void PropertiesContainEventIdAndEventNameIfSetOnLog()
Assert.Equal("1", eventId);
Assert.True(properties.TryGetValue("EventName", out string eventName));
Assert.Equal("TestEvent", eventName);
Assert.Equal(2, properties.Count);
Assert.Equal(3, properties.Count);
}

[Fact]
public void PropertiesContainLoggerName()
{
var logRecords = new List<LogRecord>();
using var loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddOpenTelemetry(options =>
{
options.AddInMemoryExporter(logRecords);
});
builder.AddFilter(typeof(LogsHelperTests).FullName, LogLevel.Trace);
});

var categoryName = nameof(LogsHelperTests);
var logger = loggerFactory.CreateLogger(categoryName);

logger.LogInformation("Information goes here");

var properties = new ChangeTrackingDictionary<string, string>();
LogsHelper.GetMessageAndSetProperties(logRecords[0], properties);

Assert.True(properties.TryGetValue("CategoryName", out string loggedCategoryName));
Assert.Equal(categoryName, loggedCategoryName);
Assert.Single(properties);
}

[Fact]
Expand Down Expand Up @@ -318,13 +344,13 @@ public void VerifyHandlingOfVariousScopeDataTypes(object scopeValue)

if (scopeValue != null)
{
Assert.Single(properties); // Assert that there is exactly one property
Assert.Equal(2, properties.Count); // Scope property + CategoryName
Assert.True(properties.TryGetValue(expectedScopeKey, out string actualScopeValue));
Assert.Equal(scopeValue.ToString(), actualScopeValue);
}
else
{
Assert.Empty(properties); // Assert that properties are empty
Assert.Single(properties); // Single property expected (CategoryName)
}
}

Expand Down Expand Up @@ -408,7 +434,7 @@ public void DuplicateKeysInLogRecordAttributesAndLogScope()
var properties = new ChangeTrackingDictionary<string, string>();
LogsHelper.GetMessageAndSetProperties(logRecords[0], properties);

Assert.Equal(2, properties.Count);
Assert.Equal(3, properties.Count);
Assert.True(properties.TryGetValue(expectedScopeKey, out string actualScopeValue));
Assert.Equal(expectedScopeValue, actualScopeValue);
Assert.True(properties.TryGetValue("attributeKey", out string actualAttributeValue));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void ValidateMessageData(LogLevel logLevel)

Assert.Equal("Log Message", messageData.Message);
Assert.Equal(LogsHelper.GetSeverityLevel(logLevel), messageData.SeverityLevel);
Assert.Empty(messageData.Properties);
Assert.Single(messageData.Properties); // CategoryName property expected
Assert.Empty(messageData.Measurements);
}
}
Expand Down

0 comments on commit 8d7472f

Please sign in to comment.