Wiring and injected NLog into a .Net Core console application : <p>I created a consumer/job that I will have running as a process on linux written in C#. </p>

<p>The process will:</p>

<ol>
<li>Read a message from RabbitMQ </li>
<li>Make changes to the database </li>
<li>Log any errors</li>
</ol>

<p>All the documentation on nlog about .net core are on aspnet core. When I try to get an <code>ILogger</code> implementation, it returns null.</p>

<p>Here is an except of wiring and usage:</p>

<pre><code>static void ConfigureServices()
{
        string environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");

        var builder = new ConfigurationBuilder()
             .SetBasePath(Path.Combine(AppContext.BaseDirectory))
             .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
             .AddJsonFile($"appsettings.{environment}.json", optional: true);

        var services = new ServiceCollection();

        Configuration = builder.Build();

        [...]

        services.AddLogging();

        ServiceProvider = services.BuildServiceProvider();

        var loggerFactory = ServiceProvider.GetService&lt;ILoggerFactory&gt;();
        loggerFactory.AddNLog();
}

static void Main(string[] args)
{
    ConfigureServices();

    var logger = ServiceProvider.GetService&lt;NLog.ILogger&gt;();

    logger.Debug("Logging");

    [...]
}
</code></pre>

<p>Do not be confused with the environment variable <code>ASPNETCORE_ENVIRONMENT</code>; it is used solely to determine which <code>appsettings.json</code> to use.</p>

<p>I've based my code on this <a href="https://github.com/NLog/NLog.Extensions.Logging/issues/111" rel="noreferrer">issue report</a>.</p>

<p>Finally, these are the packages I currently have installed.</p>

<pre><code>&lt;ItemGroup&gt;
    &lt;PackageReference Include="Microsoft.EntityFrameworkCore" Version="1.1.2" /&gt;
    &lt;PackageReference Include="Microsoft.Extensions.Configuration" Version="1.1.2" /&gt;
    &lt;PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="1.1.2" /&gt;
    &lt;PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="1.1.2" /&gt;
    &lt;PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.1.2" /&gt;
    &lt;PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="1.1.2" /&gt;
    &lt;PackageReference Include="NLog" Version="5.0.0-beta09" /&gt;
    &lt;PackageReference Include="NLog.Extensions.Logging" Version="1.0.0-rtm-beta5" /&gt;
    &lt;PackageReference Include="Npgsql" Version="3.2.4.1" /&gt;
    &lt;PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="1.1.0" /&gt;
  &lt;/ItemGroup&gt;
</code></pre>
