Register OpenTelemetry Before Startup.cs without Dependency Injection in dotnet 7 #4603
-
By design, we are adding additional logs before startup. I tried to create logger and register it but logs do not show up. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
@emmayspark You could use something like this: // Manually create logger factory
using var loggerFactory = LoggerFactory.Create(builder => builder.AddOpenTelemetry(logging => logging.AddConsoleExporter()));
var programLogger = loggerFactory.CreateLogger("Program");
programLogger.LogInformation("Starting...");
var appBuilder = WebApplication.CreateBuilder(args);
// Register manually created logger factory as the ILoggerFactory for the app
appBuilder.Services.AddSingleton<ILoggerFactory>(loggerFactory);
var app = appBuilder.Build();
app.Run();
programLogger.LogInformation("Stopping..."); |
Beta Was this translation helpful? Give feedback.
-
There is a note at the bottom of this page, https://github.com/open-telemetry/opentelemetry-dotnet/blob/main/docs/logs/getting-started-console/README.md, that says users should not create a new LoggerFactory.
|
Beta Was this translation helpful? Give feedback.
if you want to do some logging even before the asp.net core pipeline is setup, then you need to setup own LoggerFactory. And this may be thrown away after initialization is done, when you can leverage the existing logging pipeline.