Align console logging example with ASP.NET Core logging example (#4855)
This commit is contained in:
parent
fc1100f794
commit
7eb3e7322e
|
|
@ -1,40 +0,0 @@
|
|||
// <copyright file="FoodSupplyLogs.cs" company="OpenTelemetry Authors">
|
||||
// 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.
|
||||
// </copyright>
|
||||
|
||||
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);
|
||||
}
|
||||
|
|
@ -17,33 +17,38 @@
|
|||
using Microsoft.Extensions.Logging;
|
||||
using OpenTelemetry.Logs;
|
||||
|
||||
namespace SourceGeneration;
|
||||
|
||||
public class Program
|
||||
using var loggerFactory = LoggerFactory.Create(builder =>
|
||||
{
|
||||
public static void Main()
|
||||
builder.AddOpenTelemetry(logging =>
|
||||
{
|
||||
using var loggerFactory = LoggerFactory.Create(builder =>
|
||||
{
|
||||
builder.AddOpenTelemetry(options =>
|
||||
{
|
||||
options.IncludeScopes = true;
|
||||
options.ParseStateValues = true;
|
||||
options.IncludeFormattedMessage = true;
|
||||
options.AddConsoleExporter();
|
||||
});
|
||||
});
|
||||
logging.AddConsoleExporter();
|
||||
});
|
||||
});
|
||||
|
||||
var logger = loggerFactory.CreateLogger<Program>();
|
||||
var logger = loggerFactory.CreateLogger<Program>();
|
||||
|
||||
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.");
|
||||
}
|
||||
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);
|
||||
|
||||
[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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -31,18 +31,16 @@ 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.
|
||||
|
||||
```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
|
||||
|
|
@ -51,17 +49,12 @@ 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
|
||||
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
|
||||
|
|
@ -73,11 +66,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.
|
||||
|
|
|
|||
Loading…
Reference in New Issue