Saltar al contenido

Conecte e inyecte NLog en la aplicación de consola de .NET Core

Solución:

Un ejemplo minimalista completo de NLog en una aplicación de consola .NET Core 1 (basada en el repositorio NLog.Extensions.Logging):

var services = new ServiceCollection();
services.AddLogging();
var provider = services.BuildServiceProvider();

var factory = provider.GetService<ILoggerFactory>();
factory.AddNLog();
factory.ConfigureNLog("nlog.config");

var logger = provider.GetService<ILogger<Program>>();
logger.LogCritical("hello nlog");

Referencias:

    <ItemGroup>
        <PackageReference Include="NLog.Extensions.Logging" Version="1.0.0-rtm-beta5" />
        <PackageReference Include="Microsoft.Extensions.Configuration" Version="1.1.2" />
        <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="1.1.1" />
        <PackageReference Include="Microsoft.Extensions.Logging" Version="1.1.2" />
    </ItemGroup>

nlog.config:

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      internalLogFile="internal-nlog.txt">

  <variable name="Layout"
            value="${longdate}|${level:uppercase=true}|${logger}|${message}"/>

  <!-- the targets to write to -->
  <targets>
    <!-- write logs to file -->
    <target xsi:type="File" 
            name="allfile" 
            fileName="nlog-all-${shortdate}.log"
            layout="${Layout}" />

    <!-- write to the void aka just remove -->
    <target xsi:type="Null" name="blackhole" />
  </targets>

  <!-- rules to map from logger name to target -->
  <rules>
    <!--All logs, including from Microsoft-->
    <logger name="*" minlevel="Trace" writeTo="allfile" />

    <!--Skip Microsoft logs and so log only own logs-->
    <logger name="Microsoft.*" minlevel="Trace" writeTo="blackhole" final="true" />
  </rules>
</nlog>

En DotNet Core 2, puede usar la clase de inicio ahora y limpiar un poco el código para que se parezca más a la web.

Y como beneficio adicional, una forma de iniciar su aplicación dentro del contenedor DI usando ConsoleApp

Program.cs

static void Main(string[] args)
{
    IServiceCollection services = new ServiceCollection();

    Startup startup = new Startup();
    startup.ConfigureServices(services);

    IServiceProvider serviceProvider = services.BuildServiceProvider();

    // entry to run app
    serviceProvider.GetService<ConsoleApp>().Run();
}

Startup.cs

public class Startup
{
    IConfigurationRoot Configuration { get; }

    public Startup()
    {
        var builder = new ConfigurationBuilder()
          .SetBasePath(Directory.GetCurrentDirectory())
          .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);

        Configuration = builder.Build();
    }


    public void ConfigureServices(IServiceCollection services)
    {
        services.AddSingleton<IConfigurationRoot>(Configuration);

        services.AddSingleton<IMyConfiguration, MyConfiguration>();

        services.AddLogging(loggingBuilder => {
           loggingBuilder.AddNLog("nlog.config");
        });

        services.AddTransient<ConsoleApp>();
    }
}

ConsoleApp.cs

public class ConsoleApp
{
    private readonly ILogger<ConsoleApp> _logger;
    private readonly IMyConfiguration _config;

    public ConsoleApp(IMyConfiguration configurationRoot, ILogger<ConsoleApp> logger)
    {
        _logger = logger;
        _config = configurationRoot;
    }


    public void Run()
    {
        var test = _config.YourItem;

        _logger.LogCritical(test);

        System.Console.ReadKey();
    }
}

Configuration.cs

public class MyConfiguration : IMyConfiguration
{

    IConfigurationRoot _configurationRoot;
    public MyConfiguration(IConfigurationRoot configurationRoot)
    {
        _configurationRoot = configurationRoot;
    }

    public string YourItem => _configurationRoot["YourItem"];
}


public interface IMyConfiguration
{
    string YourItem { get; }
}
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)



Utiliza Nuestro Buscador

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *