Skip to content

Commit

Permalink
Align console logging example with ASP.NET Core logging example (#4855)
Browse files Browse the repository at this point in the history
  • Loading branch information
reyang authored Sep 18, 2023
1 parent fc1100f commit 7eb3e73
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 82 deletions.
40 changes: 0 additions & 40 deletions docs/logs/getting-started-console/FoodSupplyLogs.cs

This file was deleted.

53 changes: 29 additions & 24 deletions docs/logs/getting-started-console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.");

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);
}
25 changes: 7 additions & 18 deletions docs/logs/getting-started-console/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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.
Expand Down

0 comments on commit 7eb3e73

Please sign in to comment.