title: NLog Configuration specifically for .net core 3.1 specifying the nlog file by name/env, and enabling DI
body: <p>How can I configure an asp.net core 3.1 application to use NLog, specifically setting a custom log filename (i.e. nlog.[environment].config, and also enable for DI.  I have tried many things without success.</p>
  <p>I know there are a lot SO questions and online articles about nlog configuration, but I cannot seem to find an example for my specific scenario and requirements.  I am open to using altermative methods such as configuring via appsettings.[env].json, but want to avoid multiple web.config files</p>
  <p>One example of what I have tried:</p>
  <pre><code>public static void Main(string[] args)
  {
      var environment = Environment.GetEnvironmentVariable(&quot;ASPNETCORE_ENVIRONMENT&quot;);   
  
      var logFile = &quot;nlog.config&quot;;
      if (environment.Contains(&quot;Debug&quot;) || environment.Contains(&quot;Local&quot;))
          logFile = &quot;nlog.debug.config&quot;;
  
      // logger works only in Main() method, not as DI
      var logger = NLogBuilder.ConfigureNLog(logFile).GetCurrentClassLogger();
  
      logger.Info(&quot;Starting My App&quot;);
      CreateHostBuilder(args).Build().Run();
  }
  
  public static IHostBuilder CreateHostBuilder(string[] args) =&gt;
  
  </code></pre>
  <pre><code>public static IHostBuilder CreateHostBuilder(string[] args) =&gt;
      Host.CreateDefaultBuilder(args)
      .ConfigureAppConfiguration((hostingContext, config) =&gt;
      {
          config.Sources.Clear();
  
          var env = hostingContext.HostingEnvironment;
  
          config.AddJsonFile(&quot;appsettings.json&quot;, optional: true, reloadOnChange: true)
              .AddJsonFile($&quot;appsettings.{env.EnvironmentName}.json&quot;,
                              optional: true, reloadOnChange: true);
  
          config.AddEnvironmentVariables();
  
          if (args != null)
          {
              config.AddCommandLine(args);
          }
      })
      ... 
      .ConfigureLogging((hostingContext, logging) =&gt;
      {
          logging.ClearProviders();
          logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Debug);
          logging.AddConsole();
      })
      .UseNLog(config); // &lt;&lt; config has error, I can't seem to find a way to instanciate config. 
  }
  </code></pre>