title: .NET Core 6 - How to get an ILogger instance without Dependency Injection in Program.cs during Startup
body: <p>I've updated the content of my original question as I was starting to cause some confusion amongst the people trying to help me.</p>
  <p>I am using the library &quot;Microsoft.ApplicationInsights.AspNetCore&quot; to send logs to Azure. One of the challenges it seems in using this library is that it doesn't send any log events to Azure until a service has been created for the library.</p>
  <p>The chicken and egg situation is that I need to write logs and send them to Azure at the very earliest stage of the startup process in the Net Core 6 web app i.e. before the service that App Insights needs to function has actually been created.</p>
  <p>The reason I need to write logs at an early stage in the app startup process is to capture details of the user login, in which the Microsoft sign in page will popup as soon as the .Net Core app has started.</p>
  <p>In the code example below, you can see that I create an instance of the logger factory so that i can write some logs locally in the program.cs file before the remaining services are built and started. Although using this methodology works for writing to the console, no events are sent to App Insights. I think this becuase App insights library isnt' established until the requird service is created which is at a later stage in the program.cs file.</p>
  <pre><code>var builder = WebApplication.CreateBuilder(args);
  
  // I create an instance of logger facroty
  using var loggerFactory = LoggerFactory.Create(loggingBuilder =&gt; loggingBuilder
      .SetMinimumLevel(LogLevel.Trace)
      .AddConsole()
      .AddApplicationInsights(builder.Configuration[&quot;APPINSIGHTS_CONNECTIONSTRING&quot;]));
      
  // I use the logger factory to create an instance of Ilogger
  ILogger logger = loggerFactory.CreateLogger&lt;Program&gt;();
  
  // This code section here is related to Microsoft Identity Web library and is responsible for
  // triggering methods based upon when a user signs into Mirosoft (as well as signing out)
  // When them methods are triggered in this service, i need to write logs and send them to Azure.
  // The issue is this service runs before Application Insights service has been created/started, see code section below...
  builder.Services.Configure&lt;OpenIdConnectOptions&gt;(OpenIdConnectDefaults.AuthenticationScheme, options =&gt;
  {
      // The claim in the Jwt token where App roles are available.
      options.TokenValidationParameters.RoleClaimType = &quot;roles&quot;;
      // Advanced config - capturing user events. See OpenIdEvents class.
      options.Events ??= new OpenIdConnectEvents();
      options.Events.OnTokenValidated += openIdEvents.OnTokenValidatedFunc;
      // This is event is fired when the user is redirected to the MS Signout Page (before they've physically signed out)
      options.Events.OnRedirectToIdentityProviderForSignOut += openIdEvents.OnRedirectToIdentityProviderForSignOutFunc;
      // DO NOT DELETE - May use in the future.
      // OnSignedOutCallbackRedirect doesn't produce any user claims to read from for the user after they have signed out.
      options.Events.OnSignedOutCallbackRedirect += openIdEvents.OnSignedOutCallbackRedirectFunc;
  });
  
  // --- IMPORTANT NOTE -----
  This log event is succesfully written to the console, BUT it does not get sent to Azure App Insights.
  // --------------------------------------------------------------------------------------
  The methods triggered in the code section above by Microsoft Identity Web are actually stored in a seperate class,
  // however being unbale to write a test log message here means that it wont work in a seperate class either.
  logger.LogInformation(&quot;This is test message&quot;);
  
  
  // ----- Other general servics being created required for my app -----
  // Add the AuthorizationPolicies for the AppRoles
  builder.Services.AddAuthorizationClaimPolicies();
  
  builder.Services.AddAuthorization(options =&gt;
  {
      // By default, all incoming requests will be authorized according to the default policy.
      options.FallbackPolicy = options.DefaultPolicy;
  });
  builder.Services.AddRazorPages()
      .AddMicrosoftIdentityUI();
      
  // HERE IS THE PART WHERE APPLICATION INSIGHTS SERVICE IS CREATED, 
  // SO HAVING CREATED AN INSTANCE OF ILOGGER FACTORY BEFORE THIS STEP DOES NOT WORK
  // ----- Configure Application Insights Telemetry -----
  Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions aiOptions = new();
  aiOptions.ConnectionString = builder.Configuration[&quot;APPINSIGHTS_CONNECTIONSTRING&quot;];
  aiOptions.EnablePerformanceCounterCollectionModule = builder.Configuration.GetSection(&quot;ApplicationInsights&quot;).GetValue&lt;bool&gt;(&quot;EnablePerformanceCounterCollectionModule&quot;);
  aiOptions.EnableRequestTrackingTelemetryModule = builder.Configuration.GetSection(&quot;ApplicationInsights&quot;).GetValue&lt;bool&gt;(&quot;EnableRequestTrackingTelemetryModule&quot;);
  aiOptions.EnableEventCounterCollectionModule = builder.Configuration.GetSection(&quot;ApplicationInsights&quot;).GetValue&lt;bool&gt;(&quot;EnableEventCounterCollectionModule&quot;);
  aiOptions.EnableDependencyTrackingTelemetryModule = builder.Configuration.GetSection(&quot;ApplicationInsights&quot;).GetValue&lt;bool&gt;(&quot;EnableDependencyTrackingTelemetryModule&quot;);
  aiOptions.EnableAppServicesHeartbeatTelemetryModule = builder.Configuration.GetSection(&quot;ApplicationInsights&quot;).GetValue&lt;bool&gt;(&quot;EnableAppServicesHeartbeatTelemetryModule&quot;);
  aiOptions.EnableAzureInstanceMetadataTelemetryModule = builder.Configuration.GetSection(&quot;ApplicationInsights&quot;).GetValue&lt;bool&gt;(&quot;EnableAzureInstanceMetadataTelemetryModule&quot;);
  aiOptions.EnableQuickPulseMetricStream = builder.Configuration.GetSection(&quot;ApplicationInsights&quot;).GetValue&lt;bool&gt;(&quot;EnableQuickPulseMetricStream&quot;);
  aiOptions.EnableAdaptiveSampling = builder.Configuration.GetSection(&quot;ApplicationInsights&quot;).GetValue&lt;bool&gt;(&quot;EnableAdaptiveSampling&quot;);
  aiOptions.EnableHeartbeat = builder.Configuration.GetSection(&quot;ApplicationInsights&quot;).GetValue&lt;bool&gt;(&quot;EnableHeartbeat&quot;);
  aiOptions.AddAutoCollectedMetricExtractor = builder.Configuration.GetSection(&quot;ApplicationInsights&quot;).GetValue&lt;bool&gt;(&quot;AddAutoCollectedMetricExtractor&quot;);
  aiOptions.RequestCollectionOptions.TrackExceptions = builder.Configuration.GetSection(&quot;ApplicationInsights&quot;).GetValue&lt;bool&gt;(&quot;RequestCollectionOptions.TrackExceptions&quot;);
  aiOptions.EnableDiagnosticsTelemetryModule = builder.Configuration.GetSection(&quot;ApplicationInsights&quot;).GetValue&lt;bool&gt;(&quot;EnableDiagnosticsTelemetryModule&quot;);
  // Build the serive with the options from above.
  builder.Services.AddApplicationInsightsTelemetry(aiOptions);
  
  var app = builder.Build();
  
  // Configure the HTTP request pipeline.
  if (!app.Environment.IsDevelopment())
  {
      app.UseExceptionHandler(&quot;/Error&quot;);
      // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
      app.UseHsts();
  }
  
  app.UseHttpsRedirection();
  app.UseStaticFiles();
  
  app.UseRouting();
  
  app.UseAuthentication();
  app.UseAuthorization();
  
  app.MapRazorPages();
  //app.MapControllers(); // Default mapping, not in use, see below...
  app.MapControllerRoute(name: &quot;default&quot;, pattern: &quot;{controller=Home}/{action=Index}/{id?}&quot;);
  
  app.Run();
  </code></pre>