From 9a02a5fc43c99d1dc92ae13f5037e73ce03a131c Mon Sep 17 00:00:00 2001 From: Reiley Yang Date: Fri, 26 Jan 2024 10:09:06 -0800 Subject: [PATCH 1/5] log correlation/redaction doc improvements --- docs/logs/README.md | 19 +++++ docs/logs/correlation/Program.cs | 49 +++++------ docs/logs/correlation/README.md | 43 +++++----- docs/logs/enrichment/MyLogEnricher.cs | 12 +++ docs/logs/enrichment/Program.cs | 27 +++++++ docs/logs/enrichment/README.md | 107 +++++++++++++++++++++++++ docs/logs/enrichment/enrichment.csproj | 9 +++ 7 files changed, 218 insertions(+), 48 deletions(-) create mode 100644 docs/logs/enrichment/MyLogEnricher.cs create mode 100644 docs/logs/enrichment/Program.cs create mode 100644 docs/logs/enrichment/README.md create mode 100644 docs/logs/enrichment/enrichment.csproj diff --git a/docs/logs/README.md b/docs/logs/README.md index 8047b1170c6..c9d06de41fb 100644 --- a/docs/logs/README.md +++ b/docs/logs/README.md @@ -153,3 +153,22 @@ instances if they are created by you. category name. Refer to the [.NET official document](https://learn.microsoft.com/dotnet/core/extensions/logging#log-category) to learn more. + +## Log Correlation + +In OpenTelemetry, logs are automatically correlated to traces. Check the [Log +Correlation](./correlation/README.md) tutorial to learn more. + +## Log Enrichment + +TBD + +## Log Filtering + +TBD + +## Log Redaction + +Logs might contain sensitive information such as passwords and credit card +numbers, proper redaction is required to prevent privacy and security incidents. +Check the [Log Redaction](./redaction/README.md) tutorial to learn more. diff --git a/docs/logs/correlation/Program.cs b/docs/logs/correlation/Program.cs index 9813e514c99..19f4d341b91 100644 --- a/docs/logs/correlation/Program.cs +++ b/docs/logs/correlation/Program.cs @@ -7,39 +7,32 @@ using OpenTelemetry.Logs; using OpenTelemetry.Trace; -namespace Correlation; +ActivitySource activitySource = new("MyCompany.MyProduct.MyLibrary"); -public class Program +using var loggerFactory = LoggerFactory.Create(builder => { - private static readonly ActivitySource MyActivitySource = new( - "MyCompany.MyProduct.MyLibrary"); - - public static void Main() + builder.AddOpenTelemetry(logging => { - // Setup Logging - using var loggerFactory = LoggerFactory.Create(builder => - { - builder.AddOpenTelemetry(options => - { - options.AddConsoleExporter(); - }); - }); + logging.AddConsoleExporter(); + }); +}); - var logger = loggerFactory.CreateLogger(); +var logger = loggerFactory.CreateLogger(); - // Setup Traces - using var tracerProvider = Sdk.CreateTracerProviderBuilder() - .AddSource("MyCompany.MyProduct.MyLibrary") - .AddConsoleExporter() - .Build(); +using var tracerProvider = Sdk.CreateTracerProviderBuilder() + .AddSource("MyCompany.MyProduct.MyLibrary") + .AddConsoleExporter() + .Build(); - // Emit activity - using (var activity = MyActivitySource.StartActivity("SayHello")) - { - activity?.SetTag("foo", 1); +// Start an activity +using (var activity = activitySource.StartActivity("SayHello")) +{ + // Write a log within the context of an activity + logger.FoodPriceChanged("artichoke", 9.99); +} - // Emit logs within the context of activity - logger.LogInformation("Hello from {name} {price}.", "tomato", 2.99); - } - } +public static partial class ApplicationLogs +{ + [LoggerMessage(LogLevel.Information, "Food `{name}` price changed to `{price}`.")] + public static partial void FoodPriceChanged(this ILogger logger, string name, double price); } diff --git a/docs/logs/correlation/README.md b/docs/logs/correlation/README.md index 0c8b0ce5e4a..309cfea64d0 100644 --- a/docs/logs/correlation/README.md +++ b/docs/logs/correlation/README.md @@ -1,4 +1,4 @@ -# Logs correlation +# Log Correlation The getting started docs for [logs](../getting-started-console/README.md) and [traces](../../trace/getting-started-console/README.md) showed how to emit logs @@ -27,29 +27,32 @@ of an active `Activity`. Running the application will show the following output on the console: ```text -LogRecord.Timestamp: 2022-05-18T18:51:16.4348626Z -LogRecord.TraceId: d7aca5b2422ed8d15f56b6a93be4537d -LogRecord.SpanId: c90ac2ad41ab4d46 +LogRecord.Timestamp: 2024-01-26T17:55:39.2273475Z +LogRecord.TraceId: aed89c3b250fb9d8e16ccab1a4a9bbb5 +LogRecord.SpanId: bd44308753200c58 LogRecord.TraceFlags: Recorded -LogRecord.CategoryName: Correlation.Program -LogRecord.LogLevel: Information -LogRecord.State: Hello from tomato 2.99. +LogRecord.CategoryName: Program +LogRecord.Severity: Info +LogRecord.SeverityText: Information +LogRecord.Body: Food `{name}` price changed to `{price}`. +LogRecord.Attributes (Key:Value): + name: artichoke + price: 9.99 + OriginalFormat (a.k.a Body): Food `{name}` price changed to `{price}`. +LogRecord.EventId: 344095174 +LogRecord.EventName: FoodPriceChanged -Resource associated with LogRecord: -service.name: unknown_service:correlation +... -Activity.TraceId: d7aca5b2422ed8d15f56b6a93be4537d -Activity.SpanId: c90ac2ad41ab4d46 -Activity.TraceFlags: Recorded +Activity.TraceId: aed89c3b250fb9d8e16ccab1a4a9bbb5 +Activity.SpanId: bd44308753200c58 +Activity.TraceFlags: Recorded Activity.ActivitySourceName: MyCompany.MyProduct.MyLibrary -Activity.DisplayName: SayHello -Activity.Kind: Internal -Activity.StartTime: 2022-05-18T18:51:16.3427411Z -Activity.Duration: 00:00:00.2248932 -Activity.Tags: - foo: 1 -Resource associated with Activity: - service.name: unknown_service:correlation +Activity.DisplayName: SayHello +Activity.Kind: Internal +Activity.StartTime: 2024-01-26T17:55:39.2223849Z +Activity.Duration: 00:00:00.0361682 +... ``` As you can see, the `LogRecord` automatically had the `TraceId`, `SpanId` fields diff --git a/docs/logs/enrichment/MyLogEnricher.cs b/docs/logs/enrichment/MyLogEnricher.cs new file mode 100644 index 00000000000..5cac05e2bed --- /dev/null +++ b/docs/logs/enrichment/MyLogEnricher.cs @@ -0,0 +1,12 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +using Microsoft.Extensions.Logging; + +public class MyLogEnricher : ILogEnricher +{ + public void Enrich(IEnrichmentTagCollector collector) + { + collector.Add("CustomTag", "CustomValue"); + } +} diff --git a/docs/logs/enrichment/Program.cs b/docs/logs/enrichment/Program.cs new file mode 100644 index 00000000000..d38052e54ea --- /dev/null +++ b/docs/logs/enrichment/Program.cs @@ -0,0 +1,27 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +using Microsoft.Extensions.Logging; +using OpenTelemetry.Logs; + +var loggerFactory = LoggerFactory.Create(builder => +{ + builder.AddOpenTelemetry(logging => + { + logging.AddConsoleExporter(); + }); +}); + +var logger = loggerFactory.CreateLogger(); + +logger.FoodPriceChanged("artichoke", 9.99); + +// Dispose logger factory before the application ends. +// This will flush the remaining logs and shutdown the logging pipeline. +loggerFactory.Dispose(); + +public static partial class ApplicationLogs +{ + [LoggerMessage(LogLevel.Information, "Food `{name}` price changed to `{price}`.")] + public static partial void FoodPriceChanged(this ILogger logger, string name, double price); +} diff --git a/docs/logs/enrichment/README.md b/docs/logs/enrichment/README.md new file mode 100644 index 00000000000..d623be4385b --- /dev/null +++ b/docs/logs/enrichment/README.md @@ -0,0 +1,107 @@ +# Logging with Complex Objects + +In the [Getting Started with OpenTelemetry .NET Logs in 5 Minutes - Console +Application](../getting-started-console/README.md) 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](https://learn.microsoft.com/dotnet/api/microsoft.extensions.logging.logpropertiesattribute). +This attribute and the corresponding code generation logic are provided by an +extension package called +[Microsoft.Extensions.Telemetry.Abstractions](https://www.nuget.org/packages/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](https://www.nuget.org/packages/Microsoft.Extensions.Telemetry.Abstractions/#supportedframeworks-body-tab) +for more information. + +First, complete the [getting started](../getting-started-console/README.md) +tutorial, then install the +[Microsoft.Extensions.Telemetry.Abstractions](https://www.nuget.org/packages/Microsoft.Extensions.Telemetry.Abstractions/) +package: + +```sh +dotnet add package Microsoft.Extensions.Telemetry.Abstractions +``` + +Define a new complex data type, as shown in [FoodRecallNotice.cs](./FoodRecallNotice.cs): + +```csharp +public class 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](./Program.cs). Note +that the following code is added which uses the `LogPropertiesAttribute` to log +the `FoodRecallNotice` object: + +```csharp +public static partial class ApplicationLogs +{ + [LoggerMessage(LogLevel.Critical)] + public static partial void FoodRecallNotice( + this ILogger logger, + [LogProperties(OmitReferenceName = true)] FoodRecallNotice foodRecallNotice); +} +``` + +The following code is used to create a `FoodRecallNotice` object and log it: + +```csharp +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. + +```text +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 +``` + +> [!NOTE] +> In this tutorial we've used +[LogPropertiesAttribute.OmitReferenceName](https://learn.microsoft.com/dotnet/api/microsoft.extensions.logging.logpropertiesattribute.omitreferencename) +which changed the style of attribute names. There are more options available, +check out the [learn more](#learn-more) section for more information. + +## Learn more + +* [Microsoft.Extensions.Logging.LogPropertiesAttribute](https://learn.microsoft.com/dotnet/api/microsoft.extensions.logging.logpropertiesattribute) +* [Microsoft.Extensions.Telemetry.Abstractions](https://github.com/dotnet/extensions/blob/main/src/Libraries/Microsoft.Extensions.Telemetry.Abstractions/README.md) +* [Strong-type support feature + request](https://github.com/dotnet/runtime/issues/61947) +* [LogPropertiesAttribute design + proposal](https://github.com/dotnet/runtime/issues/81730) diff --git a/docs/logs/enrichment/enrichment.csproj b/docs/logs/enrichment/enrichment.csproj new file mode 100644 index 00000000000..cb2c93fa1ff --- /dev/null +++ b/docs/logs/enrichment/enrichment.csproj @@ -0,0 +1,9 @@ + + + true + + + + + + From 5b5e720699fc2b23b5869cc5bc3c278656d8ab40 Mon Sep 17 00:00:00 2001 From: Reiley Yang Date: Fri, 26 Jan 2024 10:10:42 -0800 Subject: [PATCH 2/5] remove enrichment project --- docs/logs/enrichment/MyLogEnricher.cs | 12 --- docs/logs/enrichment/Program.cs | 27 ------- docs/logs/enrichment/README.md | 107 ------------------------- docs/logs/enrichment/enrichment.csproj | 9 --- 4 files changed, 155 deletions(-) delete mode 100644 docs/logs/enrichment/MyLogEnricher.cs delete mode 100644 docs/logs/enrichment/Program.cs delete mode 100644 docs/logs/enrichment/README.md delete mode 100644 docs/logs/enrichment/enrichment.csproj diff --git a/docs/logs/enrichment/MyLogEnricher.cs b/docs/logs/enrichment/MyLogEnricher.cs deleted file mode 100644 index 5cac05e2bed..00000000000 --- a/docs/logs/enrichment/MyLogEnricher.cs +++ /dev/null @@ -1,12 +0,0 @@ -// Copyright The OpenTelemetry Authors -// SPDX-License-Identifier: Apache-2.0 - -using Microsoft.Extensions.Logging; - -public class MyLogEnricher : ILogEnricher -{ - public void Enrich(IEnrichmentTagCollector collector) - { - collector.Add("CustomTag", "CustomValue"); - } -} diff --git a/docs/logs/enrichment/Program.cs b/docs/logs/enrichment/Program.cs deleted file mode 100644 index d38052e54ea..00000000000 --- a/docs/logs/enrichment/Program.cs +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright The OpenTelemetry Authors -// SPDX-License-Identifier: Apache-2.0 - -using Microsoft.Extensions.Logging; -using OpenTelemetry.Logs; - -var loggerFactory = LoggerFactory.Create(builder => -{ - builder.AddOpenTelemetry(logging => - { - logging.AddConsoleExporter(); - }); -}); - -var logger = loggerFactory.CreateLogger(); - -logger.FoodPriceChanged("artichoke", 9.99); - -// Dispose logger factory before the application ends. -// This will flush the remaining logs and shutdown the logging pipeline. -loggerFactory.Dispose(); - -public static partial class ApplicationLogs -{ - [LoggerMessage(LogLevel.Information, "Food `{name}` price changed to `{price}`.")] - public static partial void FoodPriceChanged(this ILogger logger, string name, double price); -} diff --git a/docs/logs/enrichment/README.md b/docs/logs/enrichment/README.md deleted file mode 100644 index d623be4385b..00000000000 --- a/docs/logs/enrichment/README.md +++ /dev/null @@ -1,107 +0,0 @@ -# Logging with Complex Objects - -In the [Getting Started with OpenTelemetry .NET Logs in 5 Minutes - Console -Application](../getting-started-console/README.md) 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](https://learn.microsoft.com/dotnet/api/microsoft.extensions.logging.logpropertiesattribute). -This attribute and the corresponding code generation logic are provided by an -extension package called -[Microsoft.Extensions.Telemetry.Abstractions](https://www.nuget.org/packages/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](https://www.nuget.org/packages/Microsoft.Extensions.Telemetry.Abstractions/#supportedframeworks-body-tab) -for more information. - -First, complete the [getting started](../getting-started-console/README.md) -tutorial, then install the -[Microsoft.Extensions.Telemetry.Abstractions](https://www.nuget.org/packages/Microsoft.Extensions.Telemetry.Abstractions/) -package: - -```sh -dotnet add package Microsoft.Extensions.Telemetry.Abstractions -``` - -Define a new complex data type, as shown in [FoodRecallNotice.cs](./FoodRecallNotice.cs): - -```csharp -public class 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](./Program.cs). Note -that the following code is added which uses the `LogPropertiesAttribute` to log -the `FoodRecallNotice` object: - -```csharp -public static partial class ApplicationLogs -{ - [LoggerMessage(LogLevel.Critical)] - public static partial void FoodRecallNotice( - this ILogger logger, - [LogProperties(OmitReferenceName = true)] FoodRecallNotice foodRecallNotice); -} -``` - -The following code is used to create a `FoodRecallNotice` object and log it: - -```csharp -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. - -```text -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 -``` - -> [!NOTE] -> In this tutorial we've used -[LogPropertiesAttribute.OmitReferenceName](https://learn.microsoft.com/dotnet/api/microsoft.extensions.logging.logpropertiesattribute.omitreferencename) -which changed the style of attribute names. There are more options available, -check out the [learn more](#learn-more) section for more information. - -## Learn more - -* [Microsoft.Extensions.Logging.LogPropertiesAttribute](https://learn.microsoft.com/dotnet/api/microsoft.extensions.logging.logpropertiesattribute) -* [Microsoft.Extensions.Telemetry.Abstractions](https://github.com/dotnet/extensions/blob/main/src/Libraries/Microsoft.Extensions.Telemetry.Abstractions/README.md) -* [Strong-type support feature - request](https://github.com/dotnet/runtime/issues/61947) -* [LogPropertiesAttribute design - proposal](https://github.com/dotnet/runtime/issues/81730) diff --git a/docs/logs/enrichment/enrichment.csproj b/docs/logs/enrichment/enrichment.csproj deleted file mode 100644 index cb2c93fa1ff..00000000000 --- a/docs/logs/enrichment/enrichment.csproj +++ /dev/null @@ -1,9 +0,0 @@ - - - true - - - - - - From ce810cc3af7b9ce91771e75ed2bc503200d2b77f Mon Sep 17 00:00:00 2001 From: Reiley Yang Date: Fri, 26 Jan 2024 10:40:04 -0800 Subject: [PATCH 3/5] link to the existing filtering doc --- docs/logs/README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/logs/README.md b/docs/logs/README.md index c9d06de41fb..2c456035431 100644 --- a/docs/logs/README.md +++ b/docs/logs/README.md @@ -165,7 +165,8 @@ TBD ## Log Filtering -TBD +Check the [Customizing OpenTelemetry .NET SDK for +Logs](./customizing-the-sdk/README.md#log-filtering) document to learn more. ## Log Redaction From 59c4368189ae46371bff2c46a8e6e0a399491c79 Mon Sep 17 00:00:00 2001 From: Reiley Yang Date: Fri, 26 Jan 2024 11:20:16 -0800 Subject: [PATCH 4/5] explicit dispose --- docs/logs/correlation/Program.cs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/docs/logs/correlation/Program.cs b/docs/logs/correlation/Program.cs index 19f4d341b91..a824f89db3a 100644 --- a/docs/logs/correlation/Program.cs +++ b/docs/logs/correlation/Program.cs @@ -9,7 +9,7 @@ ActivitySource activitySource = new("MyCompany.MyProduct.MyLibrary"); -using var loggerFactory = LoggerFactory.Create(builder => +var loggerFactory = LoggerFactory.Create(builder => { builder.AddOpenTelemetry(logging => { @@ -19,7 +19,7 @@ var logger = loggerFactory.CreateLogger(); -using var tracerProvider = Sdk.CreateTracerProviderBuilder() +var tracerProvider = Sdk.CreateTracerProviderBuilder() .AddSource("MyCompany.MyProduct.MyLibrary") .AddConsoleExporter() .Build(); @@ -31,6 +31,14 @@ logger.FoodPriceChanged("artichoke", 9.99); } +// Dispose tracer provider before the application ends. +// This will flush the remaining spans and shutdown the tracing pipeline. +tracerProvider.Dispose(); + +// Dispose logger factory before the application ends. +// This will flush the remaining logs and shutdown the logging pipeline. +loggerFactory.Dispose(); + public static partial class ApplicationLogs { [LoggerMessage(LogLevel.Information, "Food `{name}` price changed to `{price}`.")] From a74ce0cf054da7acc4ab9fd25c74310836797c33 Mon Sep 17 00:00:00 2001 From: Reiley Yang Date: Fri, 26 Jan 2024 13:10:43 -0800 Subject: [PATCH 5/5] split files --- docs/logs/correlation/LoggerExtensions.cs | 10 +++ docs/logs/correlation/Program.cs | 67 +++++++++---------- .../getting-started-aspnetcore/Program.cs | 2 +- .../logs/getting-started-aspnetcore/README.md | 2 +- docs/logs/getting-started-console/Program.cs | 2 +- docs/logs/getting-started-console/README.md | 2 +- docs/logs/redaction/Program.cs | 2 +- 7 files changed, 48 insertions(+), 39 deletions(-) create mode 100644 docs/logs/correlation/LoggerExtensions.cs diff --git a/docs/logs/correlation/LoggerExtensions.cs b/docs/logs/correlation/LoggerExtensions.cs new file mode 100644 index 00000000000..facd6d895c8 --- /dev/null +++ b/docs/logs/correlation/LoggerExtensions.cs @@ -0,0 +1,10 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +using Microsoft.Extensions.Logging; + +internal static partial class LoggerExtensions +{ + [LoggerMessage(LogLevel.Information, "Food `{name}` price changed to `{price}`.")] + public static partial void FoodPriceChanged(this ILogger logger, string name, double price); +} diff --git a/docs/logs/correlation/Program.cs b/docs/logs/correlation/Program.cs index a824f89db3a..b1a3284fbcc 100644 --- a/docs/logs/correlation/Program.cs +++ b/docs/logs/correlation/Program.cs @@ -7,40 +7,39 @@ using OpenTelemetry.Logs; using OpenTelemetry.Trace; -ActivitySource activitySource = new("MyCompany.MyProduct.MyLibrary"); - -var loggerFactory = LoggerFactory.Create(builder => +public class Program { - builder.AddOpenTelemetry(logging => - { - logging.AddConsoleExporter(); - }); -}); - -var logger = loggerFactory.CreateLogger(); + private static readonly ActivitySource MyActivitySource = new("MyCompany.MyProduct.MyLibrary"); -var tracerProvider = Sdk.CreateTracerProviderBuilder() - .AddSource("MyCompany.MyProduct.MyLibrary") - .AddConsoleExporter() - .Build(); - -// Start an activity -using (var activity = activitySource.StartActivity("SayHello")) -{ - // Write a log within the context of an activity - logger.FoodPriceChanged("artichoke", 9.99); -} - -// Dispose tracer provider before the application ends. -// This will flush the remaining spans and shutdown the tracing pipeline. -tracerProvider.Dispose(); - -// Dispose logger factory before the application ends. -// This will flush the remaining logs and shutdown the logging pipeline. -loggerFactory.Dispose(); - -public static partial class ApplicationLogs -{ - [LoggerMessage(LogLevel.Information, "Food `{name}` price changed to `{price}`.")] - public static partial void FoodPriceChanged(this ILogger logger, string name, double price); + public static void Main() + { + var tracerProvider = Sdk.CreateTracerProviderBuilder() + .AddSource("MyCompany.MyProduct.MyLibrary") + .AddConsoleExporter() + .Build(); + + var loggerFactory = LoggerFactory.Create(builder => + { + builder.AddOpenTelemetry(logging => + { + logging.AddConsoleExporter(); + }); + }); + + var logger = loggerFactory.CreateLogger(); + + using (var activity = MyActivitySource.StartActivity("SayHello")) + { + // Write a log within the context of an activity + logger.FoodPriceChanged("artichoke", 9.99); + } + + // Dispose logger factory before the application ends. + // This will flush the remaining logs and shutdown the logging pipeline. + loggerFactory.Dispose(); + + // Dispose tracer provider before the application ends. + // This will flush the remaining spans and shutdown the tracing pipeline. + tracerProvider.Dispose(); + } } diff --git a/docs/logs/getting-started-aspnetcore/Program.cs b/docs/logs/getting-started-aspnetcore/Program.cs index 60d332c3a8a..179d9d2237a 100644 --- a/docs/logs/getting-started-aspnetcore/Program.cs +++ b/docs/logs/getting-started-aspnetcore/Program.cs @@ -39,7 +39,7 @@ app.Run(); -public static partial class ApplicationLogs +internal static partial class LoggerExtensions { [LoggerMessage(LogLevel.Information, "Starting the app...")] public static partial void StartingApp(this ILogger logger); diff --git a/docs/logs/getting-started-aspnetcore/README.md b/docs/logs/getting-started-aspnetcore/README.md index 1a4b8717485..6c6bbe26dc2 100644 --- a/docs/logs/getting-started-aspnetcore/README.md +++ b/docs/logs/getting-started-aspnetcore/README.md @@ -105,7 +105,7 @@ has been used across the example, which delivers high performance, structured logging, and type-checked parameters: ```csharp -public static partial class ApplicationLogs +internal static partial class LoggerExtensions { [LoggerMessage(LogLevel.Information, "Starting the app...")] public static partial void StartingApp(this ILogger logger); diff --git a/docs/logs/getting-started-console/Program.cs b/docs/logs/getting-started-console/Program.cs index cb7c075ab17..b907d169d20 100644 --- a/docs/logs/getting-started-console/Program.cs +++ b/docs/logs/getting-started-console/Program.cs @@ -27,7 +27,7 @@ // This will flush the remaining logs and shutdown the logging pipeline. loggerFactory.Dispose(); -public static partial class ApplicationLogs +internal static partial class LoggerExtensions { [LoggerMessage(LogLevel.Information, "Food `{name}` price changed to `{price}`.")] public static partial void FoodPriceChanged(this ILogger logger, string name, double price); diff --git a/docs/logs/getting-started-console/README.md b/docs/logs/getting-started-console/README.md index 7cd60b47bc1..aa251d09951 100644 --- a/docs/logs/getting-started-console/README.md +++ b/docs/logs/getting-started-console/README.md @@ -88,7 +88,7 @@ has been used across the example, which delivers high performance, structured logging, and type-checked parameters: ```csharp -public static partial class ApplicationLogs +internal static partial class LoggerExtensions { [LoggerMessage(LogLevel.Information, "Food `{name}` price changed to `{price}`.")] public static partial void FoodPriceChanged(this ILogger logger, string name, double price); diff --git a/docs/logs/redaction/Program.cs b/docs/logs/redaction/Program.cs index 207a2b659d5..fa33d884581 100644 --- a/docs/logs/redaction/Program.cs +++ b/docs/logs/redaction/Program.cs @@ -22,7 +22,7 @@ // This will flush the remaining logs and shutdown the logging pipeline. loggerFactory.Dispose(); -public static partial class ApplicationLogs +internal static partial class LoggerExtensions { [LoggerMessage(LogLevel.Information, "Food `{name}` price changed to `{price}`.")] public static partial void FoodPriceChanged(this ILogger logger, string name, double price);