|
| 1 | +--- |
| 2 | +title: Logging complex objects |
| 3 | +linkTitle: Complex objects |
| 4 | +description: Learn how to log complex objects with OpenTelemetry .NET |
| 5 | +weight: 20 |
| 6 | +# prettier-ignore |
| 7 | +cSpell:ignore: BrandName CompanyName Contoso FoodRecallNotice Listeria monocytogenes ProductDescription ProductType RecallReasonDescription |
| 8 | +--- |
| 9 | + |
| 10 | +In the |
| 11 | +[Getting Started with OpenTelemetry .NET Logs - Console](/docs/languages/dotnet/logs/getting-started-console/) |
| 12 | +guide, we learned how to log primitive data types. This guide will show you how |
| 13 | +to log complex objects. |
| 14 | + |
| 15 | +## Complex object logging in .NET |
| 16 | + |
| 17 | +Complex object logging was introduced in .NET 8.0 through the |
| 18 | +[`LogPropertiesAttribute`](https://learn.microsoft.com/dotnet/api/microsoft.extensions.logging.logpropertiesattribute). |
| 19 | +This attribute and the corresponding code generation logic are provided by an |
| 20 | +extension package called |
| 21 | +[`Microsoft.Extensions.Telemetry.Abstractions`](https://www.nuget.org/packages/Microsoft.Extensions.Telemetry.Abstractions/). |
| 22 | + |
| 23 | +## Prerequisites |
| 24 | + |
| 25 | +- Complete the |
| 26 | + [Getting Started with Console](/docs/languages/dotnet/logs/getting-started-console/) |
| 27 | + tutorial. |
| 28 | + |
| 29 | +## Implementation steps |
| 30 | + |
| 31 | +### 1. Install the required package |
| 32 | + |
| 33 | +Install the `Microsoft.Extensions.Telemetry.Abstractions` package: |
| 34 | + |
| 35 | +```shell |
| 36 | +dotnet add package Microsoft.Extensions.Telemetry.Abstractions |
| 37 | +``` |
| 38 | + |
| 39 | +### 2. Define a complex data type |
| 40 | + |
| 41 | +Create a struct to represent your complex object: |
| 42 | + |
| 43 | +```csharp |
| 44 | +public struct FoodRecallNotice |
| 45 | +{ |
| 46 | + public string? BrandName { get; set; } |
| 47 | + public string? ProductDescription { get; set; } |
| 48 | + public string? ProductType { get; set; } |
| 49 | + public string? RecallReasonDescription { get; set; } |
| 50 | + public string? CompanyName { get; set; } |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | +### 3. Create a logger extension method with LogPropertiesAttribute |
| 55 | + |
| 56 | +Define an extension method for your logger that uses the Define an extension |
| 57 | +method to `ILogger` that uses the |
| 58 | + |
| 59 | +```csharp |
| 60 | +using Microsoft.Extensions.Logging; |
| 61 | + |
| 62 | +internal static partial class LoggerExtensions |
| 63 | +{ |
| 64 | + [LoggerMessage(LogLevel.Critical)] |
| 65 | + public static partial void FoodRecallNotice( |
| 66 | + this ILogger logger, |
| 67 | + [LogProperties(OmitReferenceName = true)] in FoodRecallNotice foodRecallNotice); |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +The `[LogProperties(OmitReferenceName = true)]` attribute instructs the source |
| 72 | +generator to: |
| 73 | + |
| 74 | +- Include all properties of the `FoodRecallNotice` as individual log attributes |
| 75 | +- Omit the reference name (the parameter name) from the attribute keys |
| 76 | + |
| 77 | +### 4. Log the complex object |
| 78 | + |
| 79 | +Create an instance of your complex object and log it: |
| 80 | + |
| 81 | +```csharp |
| 82 | +// Create a complex object |
| 83 | +var foodRecallNotice = new FoodRecallNotice |
| 84 | +{ |
| 85 | + BrandName = "Contoso", |
| 86 | + ProductDescription = "Salads", |
| 87 | + ProductType = "Food & Beverages", |
| 88 | + RecallReasonDescription = "due to a possible health risk from Listeria monocytogenes", |
| 89 | + CompanyName = "Contoso Fresh Vegetables, Inc.", |
| 90 | +}; |
| 91 | + |
| 92 | +// Log the complex object |
| 93 | +logger.FoodRecallNotice(foodRecallNotice); |
| 94 | +``` |
| 95 | + |
| 96 | +### 5. Run the application |
| 97 | + |
| 98 | +Run the application, for example using `dotnet run`, and you should see the log |
| 99 | +output on the console: |
| 100 | + |
| 101 | +```text |
| 102 | +LogRecord.Timestamp: 2024-01-12T19:01:16.0604084Z |
| 103 | +LogRecord.CategoryName: Program |
| 104 | +LogRecord.Severity: Fatal |
| 105 | +LogRecord.SeverityText: Critical |
| 106 | +LogRecord.FormattedMessage: |
| 107 | +LogRecord.Body: |
| 108 | +LogRecord.Attributes (Key:Value): |
| 109 | + CompanyName: Contoso Fresh Vegetables, Inc. |
| 110 | + RecallReasonDescription: due to a possible health risk from Listeria monocytogenes |
| 111 | + ProductType: Food & Beverages |
| 112 | + ProductDescription: Salads |
| 113 | + BrandName: Contoso |
| 114 | +LogRecord.EventId: 252550133 |
| 115 | +LogRecord.EventName: FoodRecallNotice |
| 116 | +``` |
| 117 | + |
| 118 | +Notice that each property of the `FoodRecallNotice` object appears as a separate |
| 119 | +attribute in the log record. |
| 120 | + |
| 121 | +## LogPropertiesAttribute options |
| 122 | + |
| 123 | +The `LogPropertiesAttribute` provides several options to control how properties |
| 124 | +are included in logs: |
| 125 | + |
| 126 | +- **OmitReferenceName**: When set to `true`, the parameter name is omitted from |
| 127 | + attribute keys. In the example above, attribute keys are just the property |
| 128 | + names (for example, "BrandName") rather than "foodRecallNotice.BrandName". |
| 129 | + |
| 130 | +- **IncludeProperties**: Used to specify which properties should be included. If |
| 131 | + not specified, all properties are included. |
| 132 | + |
| 133 | +- **ExcludeProperties**: Used to specify which properties should be excluded |
| 134 | + from logging. |
| 135 | + |
| 136 | +- **IncludeSensitive**: When set to `true`, properties marked with `[Sensitive]` |
| 137 | + attribute are included in logs. The default is `false`. |
| 138 | + |
| 139 | +## Complete example |
| 140 | + |
| 141 | +Here's a complete example that puts everything together: |
| 142 | + |
| 143 | +```csharp |
| 144 | +using System; |
| 145 | +using Microsoft.Extensions.Logging; |
| 146 | +using OpenTelemetry; |
| 147 | +using OpenTelemetry.Logs; |
| 148 | + |
| 149 | +// Complex object definition |
| 150 | +public struct FoodRecallNotice |
| 151 | +{ |
| 152 | + public string? BrandName { get; set; } |
| 153 | + public string? ProductDescription { get; set; } |
| 154 | + public string? ProductType { get; set; } |
| 155 | + public string? RecallReasonDescription { get; set; } |
| 156 | + public string? CompanyName { get; set; } |
| 157 | +} |
| 158 | + |
| 159 | +// Logger extension method |
| 160 | +internal static partial class LoggerExtensions |
| 161 | +{ |
| 162 | + [LoggerMessage(LogLevel.Critical)] |
| 163 | + public static partial void FoodRecallNotice( |
| 164 | + this ILogger logger, |
| 165 | + [LogProperties(OmitReferenceName = true)] in FoodRecallNotice foodRecallNotice); |
| 166 | +} |
| 167 | + |
| 168 | +// Main program |
| 169 | +class Program |
| 170 | +{ |
| 171 | + static void Main(string[] args) |
| 172 | + { |
| 173 | + // Create a logger factory with OpenTelemetry |
| 174 | + using var loggerFactory = LoggerFactory.Create(builder => |
| 175 | + { |
| 176 | + builder.AddOpenTelemetry(options => |
| 177 | + { |
| 178 | + options.AddConsoleExporter(); |
| 179 | + }); |
| 180 | + }); |
| 181 | + |
| 182 | + // Get a logger instance |
| 183 | + var logger = loggerFactory.CreateLogger<Program>(); |
| 184 | + |
| 185 | + // Create a complex object |
| 186 | + var foodRecallNotice = new FoodRecallNotice |
| 187 | + { |
| 188 | + BrandName = "Contoso", |
| 189 | + ProductDescription = "Salads", |
| 190 | + ProductType = "Food & Beverages", |
| 191 | + RecallReasonDescription = "due to a possible health risk from Listeria monocytogenes", |
| 192 | + CompanyName = "Contoso Fresh Vegetables, Inc.", |
| 193 | + }; |
| 194 | + |
| 195 | + // Log the complex object |
| 196 | + logger.FoodRecallNotice(foodRecallNotice); |
| 197 | + |
| 198 | + Console.WriteLine("Press any key to exit"); |
| 199 | + Console.ReadKey(); |
| 200 | + } |
| 201 | +} |
| 202 | +``` |
| 203 | + |
| 204 | +## Learn more |
| 205 | + |
| 206 | +- [Microsoft.Extensions.Logging.LogPropertiesAttribute](https://learn.microsoft.com/dotnet/api/microsoft.extensions.logging.logpropertiesattribute) |
| 207 | +- [Microsoft.Extensions.Telemetry.Abstractions](https://github.com/dotnet/extensions/blob/main/src/Libraries/Microsoft.Extensions.Telemetry.Abstractions/README.md) |
| 208 | +- [Log Correlation in OpenTelemetry .NET](/docs/languages/dotnet/logs/correlation/) |
| 209 | +- [OpenTelemetry Logs Data Model](/docs/specs/otel/logs/data-model/) |
0 commit comments