From 129339355778f9fcf3eee25cc0b8251253e4bb2c Mon Sep 17 00:00:00 2001 From: Reiley Yang Date: Thu, 14 Sep 2023 23:04:30 -0700 Subject: [PATCH 1/4] align console logging example with ASP.NET Core logging example --- .../getting-started-console/FoodSupplyLogs.cs | 40 ------------- docs/logs/getting-started-console/Program.cs | 56 +++++++++++-------- docs/logs/getting-started-console/README.md | 15 +---- 3 files changed, 35 insertions(+), 76 deletions(-) delete mode 100644 docs/logs/getting-started-console/FoodSupplyLogs.cs diff --git a/docs/logs/getting-started-console/FoodSupplyLogs.cs b/docs/logs/getting-started-console/FoodSupplyLogs.cs deleted file mode 100644 index c7c54610bfb..00000000000 --- a/docs/logs/getting-started-console/FoodSupplyLogs.cs +++ /dev/null @@ -1,40 +0,0 @@ -// -// Copyright The OpenTelemetry Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// - -using Microsoft.Extensions.Logging; - -namespace SourceGeneration; - -public static partial class FoodSupplyLogs -{ - [LoggerMessage( - EventId = 1, - Level = LogLevel.Information, - Message = "Food `{name}` price changed to `{price}`.")] - public static partial void FoodPriceChanged(this ILogger logger, string name, double price); - - [LoggerMessage( - EventId = 2, - Message = "A `{productType}` recall notice was published for `{brandName} {productDescription}` produced by `{companyName}` ({recallReasonDescription}).")] - public static partial void FoodRecallNotice( - this ILogger logger, - LogLevel logLevel, - string brandName, - string productDescription, - string productType, - string recallReasonDescription, - string companyName); -} diff --git a/docs/logs/getting-started-console/Program.cs b/docs/logs/getting-started-console/Program.cs index 42c0e41ec3a..99242c18491 100644 --- a/docs/logs/getting-started-console/Program.cs +++ b/docs/logs/getting-started-console/Program.cs @@ -17,33 +17,41 @@ using Microsoft.Extensions.Logging; using OpenTelemetry.Logs; -namespace SourceGeneration; - -public class Program +using var loggerFactory = LoggerFactory.Create(builder => { - public static void Main() + builder.AddOpenTelemetry(options => { - using var loggerFactory = LoggerFactory.Create(builder => - { - builder.AddOpenTelemetry(options => - { - options.IncludeScopes = true; - options.ParseStateValues = true; - options.IncludeFormattedMessage = true; - options.AddConsoleExporter(); - }); - }); + options.IncludeScopes = true; + options.ParseStateValues = true; + options.IncludeFormattedMessage = true; + options.AddConsoleExporter(); + }); +}); + +var logger = loggerFactory.CreateLogger(); - var logger = loggerFactory.CreateLogger(); +logger.FoodPriceChanged("artichoke", 9.99); - logger.FoodPriceChanged("artichoke", 9.99); +logger.FoodRecallNotice( + logLevel: LogLevel.Critical, + brandName: "Contoso", + productDescription: "Salads", + productType: "Food & Beverages", + recallReasonDescription: "due to a possible health risk from Listeria monocytogenes", + companyName: "Contoso Fresh Vegetables, Inc."); + +public static partial class ApplicationLogs +{ + [LoggerMessage(EventId = 1, Level = LogLevel.Information, Message = "Food `{name}` price changed to `{price}`.")] + public static partial void FoodPriceChanged(this ILogger logger, string name, double price); - logger.FoodRecallNotice( - logLevel: LogLevel.Critical, - brandName: "Contoso", - productDescription: "Salads", - productType: "Food & Beverages", - recallReasonDescription: "due to a possible health risk from Listeria monocytogenes", - companyName: "Contoso Fresh Vegetables, Inc."); - } + [LoggerMessage(EventId = 2, Message = "A `{productType}` recall notice was published for `{brandName} {productDescription}` produced by `{companyName}` ({recallReasonDescription}).")] + public static partial void FoodRecallNotice( + this ILogger logger, + LogLevel logLevel, + string brandName, + string productDescription, + string productType, + string recallReasonDescription, + string companyName); } diff --git a/docs/logs/getting-started-console/README.md b/docs/logs/getting-started-console/README.md index 53ca89924d2..1c3b0d5f58a 100644 --- a/docs/logs/getting-started-console/README.md +++ b/docs/logs/getting-started-console/README.md @@ -31,8 +31,7 @@ package: dotnet add package OpenTelemetry.Exporter.Console ``` -Copy the [FoodSupplyLogs.cs](./FoodSupplyLogs.cs) and [Program.cs](./Program.cs) -files to the project folder. +Update the `Program.cs` file with the code from [Program.cs](./Program.cs). Run the application again (using `dotnet run`) and you should see the log output on the console. @@ -51,11 +50,7 @@ LogRecord.Attributes (Key:Value): LogRecord.EventId: 1 LogRecord.EventName: FoodPriceChanged -Resource associated with LogRecord: -telemetry.sdk.name: opentelemetry -telemetry.sdk.language: dotnet -telemetry.sdk.version: 1.6.0-alpha.1.55 -service.name: unknown_service:getting-started +... LogRecord.Timestamp: 2023-08-03T22:53:51.0403466Z LogRecord.CategoryName: SourceGeneration.Program @@ -73,11 +68,7 @@ LogRecord.Attributes (Key:Value): LogRecord.EventId: 2 LogRecord.EventName: FoodRecallNotice -Resource associated with LogRecord: -telemetry.sdk.name: opentelemetry -telemetry.sdk.language: dotnet -telemetry.sdk.version: 1.6.0-alpha.1.55 -service.name: unknown_service:getting-started +... ``` Congratulations! You are now collecting logs using OpenTelemetry. From f290c4cfa33266b8fafb4aff6860fcd94f081897 Mon Sep 17 00:00:00 2001 From: Reiley Yang Date: Thu, 14 Sep 2023 23:07:50 -0700 Subject: [PATCH 2/4] clean up --- docs/logs/getting-started-console/Program.cs | 2 -- docs/logs/getting-started-console/README.md | 10 ++++------ 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/docs/logs/getting-started-console/Program.cs b/docs/logs/getting-started-console/Program.cs index 99242c18491..2c6997a06ca 100644 --- a/docs/logs/getting-started-console/Program.cs +++ b/docs/logs/getting-started-console/Program.cs @@ -22,8 +22,6 @@ builder.AddOpenTelemetry(options => { options.IncludeScopes = true; - options.ParseStateValues = true; - options.IncludeFormattedMessage = true; options.AddConsoleExporter(); }); }); diff --git a/docs/logs/getting-started-console/README.md b/docs/logs/getting-started-console/README.md index 1c3b0d5f58a..8248ddf0335 100644 --- a/docs/logs/getting-started-console/README.md +++ b/docs/logs/getting-started-console/README.md @@ -37,11 +37,10 @@ Run the application again (using `dotnet run`) and you should see the log output on the console. ```text -LogRecord.Timestamp: 2023-08-03T22:53:51.0194130Z -LogRecord.CategoryName: SourceGeneration.Program +LogRecord.Timestamp: 2023-09-15T06:07:03.5502083Z +LogRecord.CategoryName: Program LogRecord.Severity: Info LogRecord.SeverityText: Information -LogRecord.FormattedMessage: Food `artichoke` price changed to `9.99`. LogRecord.Body: Food `{name}` price changed to `{price}`. LogRecord.Attributes (Key:Value): name: artichoke @@ -52,11 +51,10 @@ LogRecord.EventName: FoodPriceChanged ... -LogRecord.Timestamp: 2023-08-03T22:53:51.0403466Z -LogRecord.CategoryName: SourceGeneration.Program +LogRecord.Timestamp: 2023-09-15T06:07:03.5683511Z +LogRecord.CategoryName: Program LogRecord.Severity: Fatal LogRecord.SeverityText: Critical -LogRecord.FormattedMessage: A `Food & Beverages` recall notice was published for `Contoso Salads` produced by `Contoso Fresh Vegetables, Inc.` (due to a possible health risk from Listeria monocytogenes). LogRecord.Body: A `{productType}` recall notice was published for `{brandName} {productDescription}` produced by `{companyName}` ({recallReasonDescription}). LogRecord.Attributes (Key:Value): brandName: Contoso From 7a43bee69718765b7047e6bee3b34fe9451fac12 Mon Sep 17 00:00:00 2001 From: Reiley Yang Date: Thu, 14 Sep 2023 23:09:58 -0700 Subject: [PATCH 3/4] cleanup --- docs/logs/getting-started-console/Program.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/docs/logs/getting-started-console/Program.cs b/docs/logs/getting-started-console/Program.cs index 2c6997a06ca..8698eef4cee 100644 --- a/docs/logs/getting-started-console/Program.cs +++ b/docs/logs/getting-started-console/Program.cs @@ -21,7 +21,6 @@ { builder.AddOpenTelemetry(options => { - options.IncludeScopes = true; options.AddConsoleExporter(); }); }); From 4f316de3fd241174127c56788a53f33d39e04539 Mon Sep 17 00:00:00 2001 From: Reiley Yang Date: Mon, 18 Sep 2023 08:29:20 -0700 Subject: [PATCH 4/4] nit --- docs/logs/getting-started-console/Program.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/logs/getting-started-console/Program.cs b/docs/logs/getting-started-console/Program.cs index 8698eef4cee..a2d50878cfe 100644 --- a/docs/logs/getting-started-console/Program.cs +++ b/docs/logs/getting-started-console/Program.cs @@ -19,9 +19,9 @@ using var loggerFactory = LoggerFactory.Create(builder => { - builder.AddOpenTelemetry(options => + builder.AddOpenTelemetry(logging => { - options.AddConsoleExporter(); + logging.AddConsoleExporter(); }); });