In the Getting Started with OpenTelemetry .NET Logs in 5 Minutes - Console Application tutorial, we've learned how to log primitive data types. In this tutorial, we'll learn how to log complex objects.
Complex objects logging was introduced in .NET 8.0 via LogPropertiesAttribute. This attribute and the corresponding code generation logic are provided by an extension package called Microsoft.Extensions.Telemetry.Abstractions.
Note
Although Microsoft.Extensions.Telemetry.Abstractions
was introduced in .NET
8.0, it supports previous versions of the target framework (e.g. .NET 6.0).
Refer to the compatible target
frameworks
for more information.
First, complete the getting started tutorial, then install the Microsoft.Extensions.Telemetry.Abstractions package:
dotnet add package Microsoft.Extensions.Telemetry.Abstractions
Define a new complex data type, as shown in FoodRecallNotice.cs:
public struct FoodRecallNotice
{
public string? BrandName { get; set; }
public string? ProductDescription { get; set; }
public string? ProductType { get; set; }
public string? RecallReasonDescription { get; set; }
public string? CompanyName { get; set; }
}
Update the Program.cs
file with the code from Program.cs. Note
that the following code is added which uses the LogPropertiesAttribute
to log
the FoodRecallNotice
object:
internal static partial class LoggerExtensions
{
[LoggerMessage(LogLevel.Critical)]
public static partial void FoodRecallNotice(
this ILogger logger,
[LogProperties(OmitReferenceName = true)] in FoodRecallNotice foodRecallNotice);
}
The following code is used to create a FoodRecallNotice
object and log it:
var foodRecallNotice = new FoodRecallNotice
{
BrandName = "Contoso",
ProductDescription = "Salads",
ProductType = "Food & Beverages",
RecallReasonDescription = "due to a possible health risk from Listeria monocytogenes",
CompanyName = "Contoso Fresh Vegetables, Inc.",
};
logger.FoodRecallNotice(foodRecallNotice);
Run the application again (using dotnet run
) and you should see the log output
on the console.
LogRecord.Timestamp: 2024-01-12T19:01:16.0604084Z
LogRecord.CategoryName: Program
LogRecord.Severity: Fatal
LogRecord.SeverityText: Critical
LogRecord.FormattedMessage:
LogRecord.Body:
LogRecord.Attributes (Key:Value):
CompanyName: Contoso Fresh Vegetables, Inc.
RecallReasonDescription: due to a possible health risk from Listeria monocytogenes
ProductType: Food & Beverages
ProductDescription: Salads
BrandName: Contoso
LogRecord.EventId: 252550133
LogRecord.EventName: FoodRecallNotice
Note
In this tutorial we've used LogPropertiesAttribute.OmitReferenceName which changed the style of attribute names. There are more options available, check out the learn more section for more information.