Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not something introduced in this PR, but the "using" is accidentally/un-intentionally copied as-is by a lot of users into their helper methods, and the LoggerFactory ends up disposed when the helper method exits.

Copy link
Member Author

@reyang reyang Sep 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What options do we have and which one do you like?

  1. Remove using, leave Dispose to GC finalizer (if there is a finalizer).
  2. Add a comment to tell the user.
  3. Remove using, add an explicit Dispose at the end of the application.

I'll give 1) -100, 2) +2, 3) +1

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A combination of 2, 3 i.e remove using, and add explicit Dispose at the end. And comments saying

  1. Loggerfactory must be kept active for the logging to work. (to warn about disposing too early)
  2. Loggerfactory must be disposed at the end/shutdown to make sure any in-buffers telemetry is pushed out as well. (to warn about not disposing at all)

Not something to be fixed for this PR, as this issue is there for other getting-started docs as well

{
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