Correctly Configuring NLog to IHostBuilder using JSON Format

Viewed 807

I have the following code that is supposed to take in a JSON appsettings configuration for NLog, and then use that to create a NLog.LogFactory. This NLog factory is supposed to be passed into the MyService class in order to create a logger there.


class Program
{
        public static void Main(string[] args)
        {
            var builder = Host.CreateDefaultBuilder();

            ConfigureAppSettings(builder);
            ConfigureLogging(builder);
            ConfigureServices(builder);

            var host = builder.Build();

            var svc = ActivatorUtilities.CreateInstance<BarService>(host.Services);
            svc.Run();

        }

        public static void ConfigureAppSettings(IHostBuilder builder)
        {
            builder.ConfigureHostConfiguration(configBuilder =>
            {
                configBuilder.SetBasePath(Directory.GetCurrentDirectory());
                configBuilder.AddJsonFile("appsettings.json");
                configBuilder.AddEnvironmentVariables();
            });
        }

        public static void ConfigureLogging(IHostBuilder builder)
        {
            builder.ConfigureLogging((context, builder) =>
            {
                builder.AddNLog(context.Configuration.GetSection("NLog"));
            });
        }

        public static void ConfigureServices(IHostBuilder builder)
        {
            builder.ConfigureServices((config, services) =>
            {
                services.AddSingleton<LogFactory>();
                services.AddSingleton<MyService>();
            });
        }
}

When I run my MyService project, as an entry point, none of its loggers log to my target. How do I fix this bug?

1 Answers

While waiting for NLog.Extensions.Logging ver. 1.7.3 and LoggingConfigurationSectionName, then I guess you have to do this:

    public static void ConfigureLogging(IHostBuilder builder)
    {
        builder.ConfigureLogging((context, builder) =>
        {
            var rootConfig = context.Configuration;
            builder.AddNLog(rootConfig);
            var nlogSection = rootConfig.GetSection("NLog");
            var nlogConfig = new NLog.Extensions.Logging.NLogLoggingConfiguration(nlogSection);
            NLog.LogFactory.Configuration = nlogConfig;
        });
    }

When ver. 1.7.3 has been released then you can do this:

    public static void ConfigureLogging(IHostBuilder builder)
    {
        builder.ConfigureLogging((context, builder) =>
        {
            var nlogOptions = new NLog.Extensions.Logging.NLogProviderOptions();
            nlogOptions.LoggingConfigurationSectionName = "NLog";
            builder.AddNLog(context.Configuration, nlogOptions);
        });
    }

When ver. 5.0 has been released then you can do this (Will automatically load from "NLog"-section):

    public static void ConfigureLogging(IHostBuilder builder)
    {
        builder.ConfigureLogging((context, builder) =>
        {
            builder.AddNLog(context.Configuration);
        });
    }
Related