Skip to content

Commit

Permalink
Fix include scopes, service disposal, remove flush from irrelevant ex…
Browse files Browse the repository at this point in the history
…amples
  • Loading branch information
ShiningMassXAcc committed Dec 6, 2024
1 parent 655a8f6 commit aaecec7
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 43 deletions.
3 changes: 0 additions & 3 deletions docs/logs/complex-objects/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@

logger.FoodRecallNotice(foodRecallNotice);

// This will flush the remaining logs.
sdk.LoggerProvider.ForceFlush();

// Dispose SDK before the application ends.
sdk.Dispose();

Expand Down
6 changes: 0 additions & 6 deletions docs/logs/correlation/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@ public static void Main()
logger.FoodPriceChanged("artichoke", 9.99);
}

// This will flush the remaining logs.
sdk.LoggerProvider.ForceFlush();

// This will flush the remaining traces.
sdk.TracerProvider.ForceFlush();

// Dispose SDK before the application ends.
sdk.Dispose();
}
Expand Down
20 changes: 9 additions & 11 deletions docs/logs/customizing-the-sdk/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
using OpenTelemetry.Resources;

var sdk = OpenTelemetrySdk.Create(builder => builder
.WithLogging(logging =>
{
// logging.IncludeScopes = true;
logging.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(
serviceName: "MyService",
serviceVersion: "1.0.0"));
logging.AddConsoleExporter();
}));
.WithLogging(
logging =>
{
logging.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(
serviceName: "MyService",
serviceVersion: "1.0.0"));
logging.AddConsoleExporter();
},
options => options.IncludeScopes = true));

var logger = sdk.GetLoggerFactory().CreateLogger<Program>();

Expand All @@ -35,9 +36,6 @@
recallReasonDescription: "due to a possible health risk from Listeria monocytogenes",
companyName: "Contoso Fresh Vegetables, Inc.");

// This will flush the remaining logs.
sdk.LoggerProvider.ForceFlush();

// Dispose SDK before the application ends.
sdk.Dispose();

Expand Down
22 changes: 12 additions & 10 deletions docs/logs/extending-the-sdk/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ public class Program
public static void Main()
{
var sdk = OpenTelemetrySdk.Create(builder => builder
.WithLogging(logging =>
{
// logging.IncludeScopes = true;
logging.AddProcessor(new MyProcessor("ProcessorA"));
logging.AddProcessor(new MyProcessor("ProcessorB"));
logging.AddProcessor(new SimpleLogRecordExportProcessor(new MyExporter("ExporterX")));
logging.AddMyExporter();
}));
.WithLogging(
logging =>
{
logging.AddProcessor(new MyProcessor("ProcessorA"));
logging.AddProcessor(new MyProcessor("ProcessorB"));
logging.AddProcessor(new SimpleLogRecordExportProcessor(new MyExporter("ExporterX")));
logging.AddMyExporter();
},
options => options.IncludeScopes = true));

var logger = sdk.GetLoggerFactory().CreateLogger<Program>();

Expand Down Expand Up @@ -55,8 +56,9 @@ public static void Main()
// message will be redacted by MyRedactionProcessor
logger.LogInformation("OpenTelemetry {sensitiveString}.", "<secret>");

// This will flush the remaining logs.
sdk.LoggerProvider.ForceFlush();
// Attempt to flush the remaining logs when using an exporter that may require it, such as the custom one defined here.
// Ignoring success or failure for flush after 5 seconds, but a richer application should choose to handle this in some manner.
sdk.LoggerProvider.ForceFlush(5000);

// Dispose SDK before the application ends.
sdk.Dispose();
Expand Down
10 changes: 3 additions & 7 deletions docs/logs/getting-started-console-di/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,21 @@

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using OpenTelemetry;
using OpenTelemetry.Logs;

// Add services to the container, including OpenTelemetry with logging
var services = new ServiceCollection();
services.AddOpenTelemetry().WithLogging(logging => logging.AddConsoleExporter());
services.AddSingleton<ExampleService>();
IServiceProvider serviceProvider = services.BuildServiceProvider();
ServiceProvider serviceProvider = services.BuildServiceProvider();

// Get the ExampleService object from the container
ExampleService service = serviceProvider.GetRequiredService<ExampleService>();

service.DoSomeWork();

// This will flush the remaining logs.
serviceProvider.GetRequiredService<LoggerProvider>().ForceFlush();

// Dispose SDK before the application ends.
serviceProvider.GetRequiredService<OpenTelemetrySdk>().Dispose();
// Dispose before the application ends.
serviceProvider.Dispose();

internal static partial class LoggerExtensions
{
Expand Down
3 changes: 0 additions & 3 deletions docs/logs/getting-started-console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
recallReasonDescription: "due to a possible health risk from Listeria monocytogenes",
companyName: "Contoso Fresh Vegetables, Inc.");

// This will flush the remaining logs.
sdk.LoggerProvider.ForceFlush();

// Dispose SDK before the application ends.
sdk.Dispose();

Expand Down
3 changes: 0 additions & 3 deletions docs/logs/redaction/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
// Message will be redacted by MyRedactionProcessor
logger.FoodPriceChanged("<secret>", 9.99);

// This will flush the remaining logs.
sdk.LoggerProvider.ForceFlush();

// Dispose SDK before the application ends.
sdk.Dispose();

Expand Down

0 comments on commit aaecec7

Please sign in to comment.