title: Asp.net core: Do creating a new instance of ILogger<Type> each request cost expensive?
body: <p>Recently I have been working on a project based on a template written by another group in my company. This project built with <code>asp.net core 2.2</code> and it uses <code>NLog</code> as a logging framework behind the <code>ILogger</code> registered in the <code>ILoggerFactory</code>.</p>
  
  <p>I noticed an interesting point that any service depend on <code>ILogger&lt;service&gt; logger</code> gets a new instance of the <code>logger</code>. The service indeed is registered as <code>transient</code> so its creation per request seems normal, but as I read <a href="https://stackoverflow.com/questions/47901666/does-injecting-iloggert-create-a-new-logger-each-time">here</a> I expect that the <code>logger</code> will be the same (i.e. <code>singelton</code>) instance for all instances of this service.</p>
  
  <p>I can't upload all the code seems it's big and private. But it seems that the relevant lines are those:</p>
  
  <pre><code> public static IWebHostBuilder CreateBuildWebHost(string[] args)
      {
          return WebHost.CreateDefaultBuilder&lt;Startup&gt;(args).UseUnityServiceProvider()
              .ConfigureAppConfiguration((hostingContext, configurationBuilder) =&gt;
              {
                  //... 
              })
              .ConfigureLogging((hostingContext, logging) =&gt;
              {
                  logging.ClearProviders();
                  logging.SetMinimumLevel(LogLevel.Information);
                  logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
  
                  LoadExternalNLogConfigFile(hostingContext);
              })
              .UseNLog(); // NLog: setup NLog for Dependency injection
      }
  </code></pre>
  
  <p>And those:</p>
  
  <pre><code>private static void LoadExternalNLogConfigFile(WebHostBuilderContext hostingContext)
      {
          var isRunningInContainer = hostingContext.Configuration.GetValue&lt;bool&gt;("DOTNET_RUNNING_IN_CONTAINER");
  
          var xmlNLogConfigFilenameWithEnvName =
              $"nlog.{(isRunningInContainer ? "Docker." : "")}{hostingContext.HostingEnvironment.EnvironmentName}.config";
          var xmlNLogConfigFilename = $"nlog.{(isRunningInContainer ? "Docker." : "")}config";
          try
          {
              hostingContext.HostingEnvironment.ConfigureNLog(xmlNLogConfigFilenameWithEnvName);
          }
          catch
          {
              hostingContext.HostingEnvironment.ConfigureNLog(xmlNLogConfigFilename);
          }
      }
  </code></pre>
  
  <p>In order to check the equality of the <code>logger</code> instance, I've used the solutions described <a href="https://stackoverflow.com/questions/15578895/when-debugging-is-there-a-way-to-tell-if-an-object-is-a-different-instance">here</a>. </p>
  
  <p>I've tested also the <code>Nlog</code> "Hello world" project, as guided <a href="https://stackoverflow.com/questions/34679727/use-nlog-in-asp-net-core-application">here</a>. The test shown that the logger instance is identical for each request.       </p>
  
  <p><strong>My question</strong>: Do creation of a new <code>ILogger</code> instance per object request costs expensive? Should I have to look for solution for that issue, or is it a negligible problem?</p>