Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve log filter example #5406

Merged
merged 5 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
78 changes: 47 additions & 31 deletions docs/logs/customizing-the-sdk/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,53 @@
using OpenTelemetry.Logs;
using OpenTelemetry.Resources;

namespace CustomizingTheSdk;

public class Program
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.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(
serviceName: "MyService",
serviceVersion: "1.0.0"));
options.AddConsoleExporter();
});
});

var logger = loggerFactory.CreateLogger<Program>();

logger.LogInformation("Hello from {name} {price}.", "tomato", 2.99);
logger.LogWarning("Hello from {name} {price}.", "tomato", 2.99);
logger.LogError("Hello from {name} {price}.", "tomato", 2.99);

// log with scopes
using (logger.BeginScope(new List<KeyValuePair<string, object>>
{
new KeyValuePair<string, object>("store", "Seattle"),
}))
{
logger.LogInformation("Hello from {food} {price}.", "tomato", 2.99);
}
}
logging.IncludeScopes = true;
logging.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(
serviceName: "MyService",
serviceVersion: "1.0.0"));
logging.AddConsoleExporter();
});
builder.AddFilter<OpenTelemetryLoggerProvider>("*", LogLevel.Information);
reyang marked this conversation as resolved.
Show resolved Hide resolved
});

var logger = loggerFactory.CreateLogger<Program>();

logger.FoodPriceChanged("artichoke", 9.99);

using (logger.BeginScope(new List<KeyValuePair<string, object>>
{
new KeyValuePair<string, object>("store", "Seattle"),
}))
{
logger.FoodPriceChanged("truffle", 999.99);
}

logger.FoodRecallNotice(
brandName: "Contoso",
productDescription: "Salads",
productType: "Food & Beverages",
recallReasonDescription: "due to a possible health risk from Listeria monocytogenes",
companyName: "Contoso Fresh Vegetables, Inc.");

// Dispose logger factory before the application ends.
// This will flush the remaining logs and shutdown the logging pipeline.
loggerFactory.Dispose();

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);

[LoggerMessage(LogLevel.Critical, "A `{productType}` recall notice was published for `{brandName} {productDescription}` produced by `{companyName}` ({recallReasonDescription}).")]
public static partial void FoodRecallNotice(
this ILogger logger,
string brandName,
string productDescription,
string productType,
string recallReasonDescription,
string companyName);
}
12 changes: 6 additions & 6 deletions docs/logs/customizing-the-sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ It is not supported to add Processors after building the `LoggerFactory`.
```csharp
var loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddOpenTelemetry(options =>
builder.AddOpenTelemetry(logging =>
{
options.AddProcessor(...)
logging.AddProcessor(...);
});
});
```
Expand All @@ -72,9 +72,9 @@ The snippet below shows configuring a custom `ResourceBuilder` to the provider.
```csharp
var loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddOpenTelemetry(options =>
builder.AddOpenTelemetry(logging =>
{
options.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(
logging.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(
serviceName: "MyService",
serviceVersion: "1.0.0"));
});
Expand Down Expand Up @@ -107,8 +107,8 @@ and also defines "Warning" as the minimum `LogLevel` for a user defined category
These rules as defined only apply to the `OpenTelemetryLoggerProvider`.

```csharp
ILoggingBuilder.AddFilter<OpenTelemetryLoggerProvider>("*", LogLevel.Error);
ILoggingBuilder.AddFilter<OpenTelemetryLoggerProvider>("category name", LogLevel.Warning);
builder.AddFilter<OpenTelemetryLoggerProvider>("*", LogLevel.Error);
builder.AddFilter<OpenTelemetryLoggerProvider>("MyProduct.MyLibrary.MyClass", LogLevel.Warning);
```

## Learn more
Expand Down
Loading