private static NLogLoggerProvider CreateNLogLoggerProvider(IServiceProvider serviceProvider, IConfiguration configuration, IHostEnvironment hostEnvironment, NLogProviderOptions options)
        {
            NLogLoggerProvider provider = new NLogLoggerProvider(options);

            configuration = SetupConfiguration(serviceProvider, configuration);

            if (serviceProvider != null && provider.Options.RegisterServiceProvider)
            {
                provider.LogFactory.ServiceRepository.RegisterService(typeof(IServiceProvider), serviceProvider);
            }

            if (configuration != null)
            {
                provider.Configure(configuration.GetSection("Logging:NLog"));
                provider.TryLoadConfigurationFromSection(configuration);
            }

            var contentRootPath = hostEnvironment?.ContentRootPath;

            if (!string.IsNullOrEmpty(contentRootPath))
            {
                TryLoadConfigurationFromContentRootPath(provider.LogFactory, contentRootPath);
            }

            if (provider.Options.ShutdownOnDispose)
            {
                provider.LogFactory.AutoShutdown = false;
            }

            return(provider);
        }
예제 #2
0
        private static void ConfigureServicesNLog(NLogAspNetCoreOptions options, IServiceCollection services, Func <IServiceProvider, IConfiguration> lookupConfiguration)
        {
            ConfigurationItemFactory.Default.RegisterItemsFromAssembly(typeof(AspNetExtensions).GetTypeInfo().Assembly);
            LogManager.AddHiddenAssembly(typeof(AspNetExtensions).GetTypeInfo().Assembly);

            services.AddSingleton <ILoggerProvider>(serviceProvider =>
            {
                ServiceLocator.ServiceProvider = serviceProvider;

                var provider      = new NLogLoggerProvider(options ?? new NLogProviderOptions());
                var configuration = lookupConfiguration(serviceProvider);
                if (configuration != null)
                {
                    ConfigSettingLayoutRenderer.DefaultConfiguration = configuration;
                    if (options == null)
                    {
                        provider.Configure(configuration.GetSection("Logging:NLog"));
                    }
                }

                return(provider);
            });

            //note: this one is called before  services.AddSingleton<ILoggerFactory>
            if ((options ?? NLogAspNetCoreOptions.Default).RegisterHttpContextAccessor)
            {
                services.AddHttpContextAccessor();
            }
        }
예제 #3
0
        /// <summary>
        ///     Enable and configure NLog as a logging provider for buildable generic host (.NET Core 2.1+).
        ///     Can be used in discrete containers as well.
        /// </summary>
        /// <param name="builder"></param>
        /// <param name="options">NLogProviderOptions object to configure NLog behavior</param>
        /// <returns>IHostBuilder for chaining</returns>
        public static IHostBuilder UseNLog(this IHostBuilder builder, NLogProviderOptions options)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            builder.ConfigureServices((hostbuilder, services) =>
            {
                ConfigurationItemFactory.Default.RegisterItemsFromAssembly(typeof(ConfigureExtensions).GetTypeInfo()
                                                                           .Assembly);

                services.AddSingleton <ILoggerProvider>(serviceProvider =>
                {
                    var provider = new NLogLoggerProvider(options ?? new NLogProviderOptions());
                    if (hostbuilder.Configuration != null)
                    {
                        ConfigSettingLayoutRenderer.DefaultConfiguration = hostbuilder.Configuration;
                        if (options == null)
                        {
                            provider.Configure(hostbuilder.Configuration.GetSection("Logging:NLog"));
                        }
                    }
                    return(provider);
                });
            });

            return(builder);
        }
예제 #4
0
        private static NLogLoggerProvider CreateNLogLoggerProvider(IServiceProvider serviceProvider, IConfiguration configuration, NLogAspNetCoreOptions options, NLog.LogFactory logFactory)
        {
            configuration = SetupConfiguration(serviceProvider, configuration);
            NLogLoggerProvider provider = new NLogLoggerProvider(options ?? NLogAspNetCoreOptions.Default, logFactory ?? LogManager.LogFactory);

            if (configuration != null && options == null)
            {
                provider.Configure(configuration.GetSection("Logging:NLog"));
            }
            return(provider);
        }
예제 #5
0
        private static NLogLoggerProvider CreateNLogLoggerProvider(IServiceProvider serviceProvider, IConfiguration configuration, NLogProviderOptions options)
        {
            configuration = SetupConfiguration(serviceProvider, configuration);
            NLogLoggerProvider provider = new NLogLoggerProvider(options);

            if (configuration != null && options == null)
            {
                provider.Configure(configuration.GetSection("Logging:NLog"));
            }
            return(provider);
        }
        private static NLogLoggerProvider CreateNLogLoggerProvider(IServiceProvider serviceProvider, IConfiguration configuration, NLogProviderOptions options)
        {
            NLogLoggerProvider provider = new NLogLoggerProvider(options);

            if (configuration != null)
            {
                ConfigSettingLayoutRenderer.DefaultConfiguration = configuration;
                if (options == null)
                {
                    provider.Configure(configuration.GetSection("Logging:NLog"));
                }
            }
            return(provider);
        }
예제 #7
0
        private static NLogLoggerProvider CreateNLogLoggerProvider(IServiceProvider serviceProvider, IConfiguration configuration, NLogAspNetCoreOptions options, NLog.LogFactory logFactory)
        {
            configuration = SetupConfiguration(serviceProvider, configuration);
            NLogLoggerProvider provider = new NLogLoggerProvider(options ?? NLogAspNetCoreOptions.Default, logFactory ?? LogManager.LogFactory);

            if (configuration != null)
            {
                provider.Configure(configuration.GetSection("Logging:NLog"));
                TryLoadConfigurationFromSection(provider, configuration);
            }

            if (provider.Options.ShutdownOnDispose)
            {
                provider.LogFactory.AutoShutdown = false;
            }

            return(provider);
        }