-
Notifications
You must be signed in to change notification settings - Fork 778
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
Doc updates to include ForceFlush() log practice #6012
Changes from 10 commits
1cfe029
ab2f2fc
1638ee8
6196af3
4fd23ec
655a8f6
aaecec7
0be8943
13737c2
619cd99
ea2d2d1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,17 +2,13 @@ | |
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
using Microsoft.Extensions.Logging; | ||
using OpenTelemetry; | ||
using OpenTelemetry.Logs; | ||
|
||
var loggerFactory = LoggerFactory.Create(builder => | ||
{ | ||
builder.AddOpenTelemetry(logging => | ||
{ | ||
logging.AddConsoleExporter(); | ||
}); | ||
}); | ||
var sdk = OpenTelemetrySdk.Create(builder => builder | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think entire examples should be moved to newer pattern. It maybe a good change, but definitely outside this PR. |
||
.WithLogging(logging => logging.AddConsoleExporter())); | ||
|
||
var logger = loggerFactory.CreateLogger<Program>(); | ||
var logger = sdk.GetLoggerFactory().CreateLogger<Program>(); | ||
|
||
var foodRecallNotice = new FoodRecallNotice | ||
{ | ||
|
@@ -25,9 +21,8 @@ | |
|
||
logger.FoodRecallNotice(foodRecallNotice); | ||
|
||
// Dispose logger factory before the application ends. | ||
// This will flush the remaining logs and shutdown the logging pipeline. | ||
loggerFactory.Dispose(); | ||
// Dispose SDK before the application ends. | ||
sdk.Dispose(); | ||
|
||
internal static partial class LoggerExtensions | ||
{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,33 +13,23 @@ public class Program | |
|
||
public static void Main() | ||
{ | ||
var tracerProvider = Sdk.CreateTracerProviderBuilder() | ||
.AddSource("MyCompany.MyProduct.MyLibrary") | ||
.AddConsoleExporter() | ||
.Build(); | ||
|
||
var loggerFactory = LoggerFactory.Create(builder => | ||
{ | ||
builder.AddOpenTelemetry(logging => | ||
var sdk = OpenTelemetrySdk.Create(builder => builder | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. keep existing code as-is. If there is a need to move them to new format, they can be done in separate PR. |
||
.WithLogging(logging => logging.AddConsoleExporter()) | ||
.WithTracing(tracing => | ||
{ | ||
logging.AddConsoleExporter(); | ||
}); | ||
}); | ||
tracing.AddSource("MyCompany.MyProduct.MyLibrary"); | ||
tracing.AddConsoleExporter(); | ||
})); | ||
|
||
var logger = loggerFactory.CreateLogger<Program>(); | ||
var logger = sdk.GetLoggerFactory().CreateLogger<Program>(); | ||
|
||
using (var activity = MyActivitySource.StartActivity("SayHello")) | ||
{ | ||
// Write a log within the context of an activity | ||
logger.FoodPriceChanged("artichoke", 9.99); | ||
} | ||
|
||
// Dispose logger factory before the application ends. | ||
// This will flush the remaining logs and shutdown the logging pipeline. | ||
loggerFactory.Dispose(); | ||
|
||
// Dispose tracer provider before the application ends. | ||
// This will flush the remaining spans and shutdown the tracing pipeline. | ||
tracerProvider.Dispose(); | ||
// Dispose SDK before the application ends. | ||
sdk.Dispose(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,24 +3,26 @@ | |
|
||
using Microsoft.Extensions.Logging; | ||
using OpenTelemetry; | ||
using OpenTelemetry.Logs; | ||
|
||
namespace ExtendingTheSdk; | ||
|
||
public class Program | ||
{ | ||
public static void Main() | ||
{ | ||
using var loggerFactory = LoggerFactory.Create(builder => | ||
builder.AddOpenTelemetry(options => | ||
{ | ||
options.IncludeScopes = true; | ||
options.AddProcessor(new MyProcessor("ProcessorA")) | ||
.AddProcessor(new MyProcessor("ProcessorB")) | ||
.AddProcessor(new SimpleLogRecordExportProcessor(new MyExporter("ExporterX"))) | ||
.AddMyExporter(); | ||
})); | ||
|
||
var logger = loggerFactory.CreateLogger<Program>(); | ||
var sdk = OpenTelemetrySdk.Create(builder => builder | ||
.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>(); | ||
|
||
// unstructured log | ||
logger.LogInformation("Hello, World!"); | ||
|
@@ -53,6 +55,13 @@ public static void Main() | |
|
||
// message will be redacted by MyRedactionProcessor | ||
logger.LogInformation("OpenTelemetry {sensitiveString}.", "<secret>"); | ||
|
||
// Attempt to flush the remaining logs when using an exporter that may require it, such as the custom one defined here. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suspect this is not exactly what you wanted @reyang but hopefully it's closer. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Definitely moving closer, what do you think regarding #6012 (review)? I want to gather ideas from folks here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I'm not super familiar with github PRs and I didn't have a reply option there - let me try to respond there as well. |
||
// 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(); | ||
} | ||
|
||
internal struct Food | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.