diff --git a/docs/logs/customizing-the-sdk/Program.cs b/docs/logs/customizing-the-sdk/Program.cs index cd631255b3..1b22fdbfda 100644 --- a/docs/logs/customizing-the-sdk/Program.cs +++ b/docs/logs/customizing-the-sdk/Program.cs @@ -5,37 +5,52 @@ 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(); - - 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> - { - new KeyValuePair("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(); + }); +}); + +var logger = loggerFactory.CreateLogger(); + +logger.FoodPriceChanged("artichoke", 9.99); + +using (logger.BeginScope(new List> +{ + new KeyValuePair("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); } diff --git a/docs/logs/customizing-the-sdk/README.md b/docs/logs/customizing-the-sdk/README.md index 1c2b20b9ed..cd48fd608a 100644 --- a/docs/logs/customizing-the-sdk/README.md +++ b/docs/logs/customizing-the-sdk/README.md @@ -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(...); }); }); ``` @@ -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")); }); @@ -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("*", LogLevel.Error); -ILoggingBuilder.AddFilter("category name", LogLevel.Warning); +builder.AddFilter("*", LogLevel.Error); +builder.AddFilter("MyProduct.MyLibrary.MyClass", LogLevel.Warning); ``` ## Learn more