Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(otel logging): Include ILogger CategoryName in logger output #44754

Merged
merged 6 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -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)!);
cijothomas marked this conversation as resolved.
Show resolved Hide resolved
}

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,65 @@ 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 PropertiesContainLoggerCategoryName()
{
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]
public void ExceptionPropertiesContainLoggerCategoryName()
{
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 logger = loggerFactory.CreateLogger<LogsHelperTests>();
try
{
throw new Exception("Test Exception");
}
catch (Exception ex)
{
logger.LogError(ex, "Here's an error");
}

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

Assert.Equal("Test Exception", message);

Assert.True(properties.TryGetValue("CategoryName", out string categoryName));
Assert.EndsWith(nameof(LogsHelperTests), categoryName);
}

[Fact]
Expand Down Expand Up @@ -318,13 +376,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 +466,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
Loading