/// <summary>
        /// Enable NLog as logging provider in ASP.NET Core.
        /// </summary>
        /// <param name="factory"></param>
        /// <param name="options">NLog options</param>
        /// <returns></returns>
        public static ILoggerFactory AddNLog(this ILoggerFactory factory, NLogProviderOptions options)
        {
            //ignore this
            LogManager.AddHiddenAssembly(Assembly.Load(new AssemblyName("Microsoft.Extensions.Logging")));
            LogManager.AddHiddenAssembly(Assembly.Load(new AssemblyName("Microsoft.Extensions.Logging.Abstractions")));

            try
            {
                //try the Filter ext
                var filterAssembly = Assembly.Load(new AssemblyName("Microsoft.Extensions.Logging.Filter"));
                LogManager.AddHiddenAssembly(filterAssembly);
            }
            catch (Exception)
            {
                //ignore
            }

            LogManager.AddHiddenAssembly(typeof(AspNetExtensions).GetTypeInfo().Assembly);

            using (var provider = new NLogLoggerProvider(options))
            {
                factory.AddProvider(provider);
            }
            return(factory);
        }
 /// <summary>
 /// New provider with options
 /// </summary>
 /// <param name="options"></param>
 /// <param name="logFactory">Optional isolated NLog LogFactory</param>
 public NLogLoggerProvider(NLogProviderOptions options, LogFactory logFactory)
 {
     LogFactory        = logFactory ?? LogManager.LogFactory;
     Options           = options ?? NLogProviderOptions.Default;
     _beginScopeParser = new NLogBeginScopeParser(options);
     RegisterHiddenAssembliesForCallSite();
 }
Пример #3
0
 /// <summary>
 /// Enable NLog as logging provider in .NET Core.
 /// </summary>
 /// <param name="factory"></param>
 /// <param name="options">NLog options</param>
 /// <returns>ILoggerFactory for chaining</returns>
 public static ILoggingBuilder AddNLog(this ILoggingBuilder factory, NLogProviderOptions options)
 {
     using (var provider = new NLogLoggerProvider(options))
     {
         factory.AddProvider(provider);
     }
     return(factory);
 }
Пример #4
0
 /// <summary>
 /// Enable NLog as logging provider in .NET Core.
 /// </summary>
 /// <param name="factory"></param>
 /// <param name="options">NLog options</param>
 /// <returns>ILoggerFactory for chaining</returns>
 public static ILoggerFactory AddNLog(this ILoggerFactory factory, NLogProviderOptions options)
 {
     ConfigureHiddenAssemblies();
     using (var provider = new NLogLoggerProvider(options))
     {
         factory.AddProvider(provider);
     }
     return(factory);
 }
 /// <summary>
 /// New provider with options
 /// </summary>
 /// <param name="options"></param>
 public NLogLoggerProvider(NLogProviderOptions options)
     : this(options, null)
 {
 }
 /// <summary>
 /// New provider with options
 /// </summary>
 /// <param name="options"></param>
 public NLogLoggerProvider(NLogProviderOptions options)
 {
     Options = options;
 }
 /// <summary>
 /// Enable NLog as logging provider for Microsoft Extension Logging
 /// </summary>
 /// <param name="factory"></param>
 /// <param name="configuration">Configuration</param>
 /// <param name="options">NLog options</param>
 /// <returns>ILoggingBuilder for chaining</returns>
 public static ILoggingBuilder AddNLog(this ILoggingBuilder factory, IConfiguration configuration, NLogProviderOptions options)
 {
     AddNLogLoggerProvider(factory, configuration, options, CreateNLogLoggerProvider);
     return(factory);
 }
        private static NLogLoggerProvider CreateNLogLoggerProvider(IServiceProvider serviceProvider, IConfiguration configuration, NLogProviderOptions options, LogFactory logFactory)
        {
            NLogLoggerProvider provider = new NLogLoggerProvider(options ?? NLogProviderOptions.Default, logFactory ?? LogManager.LogFactory);

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

                provider.TryLoadConfigurationFromSection(configuration);
            }

            return(provider);
        }
 private static void AddNLogLoggerProvider(ILoggingBuilder builder, IConfiguration configuration, NLogProviderOptions options, Func <IServiceProvider, IConfiguration, NLogProviderOptions, NLogLoggerProvider> factory)
 {
     builder.Services.TryAddNLogLoggingProvider((svc, addlogging) => addlogging(builder), configuration, options ?? NLogProviderOptions.Default, factory);
 }
 /// <summary>
 /// New factory with options.
 /// </summary>
 /// <param name="options"></param>
 public NLogLoggerFactory(NLogProviderOptions options)
 {
     _provider = new NLogLoggerProvider(options);
 }
 private static void AddNLogLoggerProvider(IServiceCollection services, IConfiguration configuration, NLogProviderOptions options, Func <IServiceProvider, IConfiguration, NLogProviderOptions, NLogLoggerProvider> factory)
 {
     services.TryAddEnumerable(ServiceDescriptor.Singleton <ILoggerProvider, NLogLoggerProvider>(serviceProvider => factory(serviceProvider, configuration, options)));
 }
 /// <summary>
 /// Enable NLog as logging provider for Microsoft Extension Logging
 /// </summary>
 /// <param name="factory"></param>
 /// <param name="options">NLog options</param>
 /// <returns>ILoggerFactory for chaining</returns>
 public static ILoggingBuilder AddNLog(this ILoggingBuilder factory, NLogProviderOptions options)
 {
     // Try adding Singleton-implementation if not already exists
     factory.Services.TryAddEnumerable(ServiceDescriptor.Singleton <ILoggerProvider, NLogLoggerProvider>(s => new NLogLoggerProvider(options)));
     return(factory);
 }
Пример #13
0
 /// <summary>
 /// New provider with options
 /// </summary>
 /// <param name="options"></param>
 public NLogLoggerProvider(NLogProviderOptions options)
 {
     Options = options;
     RegisterHiddenAssembliesForCallSite();
 }
Пример #14
0
 /// <summary>
 /// New provider with options
 /// </summary>
 /// <param name="options"></param>
 public NLogLoggerProvider(NLogProviderOptions options)
 {
     Options           = options;
     _beginScopeParser = new NLogBeginScopeParser(options);
     RegisterHiddenAssembliesForCallSite();
 }
 /// <summary>
 /// Enable NLog as logging provider for Microsoft Extension Logging
 /// </summary>
 /// <param name="factory"></param>
 /// <param name="options">NLog options</param>
 /// <returns>ILoggingBuilder for chaining</returns>
 public static ILoggingBuilder AddNLog(this ILoggingBuilder factory, NLogProviderOptions options)
 {
     AddNLogLoggerProvider(factory, null, options, CreateNLogLoggerProvider);
     return(factory);
 }
 /// <summary>
 /// Enable NLog as logging provider for Microsoft Extension Logging
 /// </summary>
 /// <param name="builder"></param>
 /// <param name="configuration">New NLog config.</param>
 /// <param name="options">NLog options</param>
 /// <returns>ILoggingBuilder for chaining</returns>
 public static ILoggingBuilder AddNLog(this ILoggingBuilder builder, LoggingConfiguration configuration, NLogProviderOptions options)
 {
     AddNLogLoggerProvider(builder, null, options, (serviceProvider, config, options) =>
     {
         var logFactory = configuration?.LogFactory ?? LogManager.LogFactory;
         var provider   = CreateNLogLoggerProvider(serviceProvider, config, options, logFactory);
         // Delay initialization of targets until we have loaded config-settings
         logFactory.Configuration = configuration;
         return(provider);
     });
     return(builder);
 }
        private static NLogLoggerProvider CreateNLogLoggerProvider(IServiceProvider serviceProvider, IConfiguration configuration, NLogProviderOptions options, LogFactory logFactory)
        {
            NLogLoggerProvider provider = new NLogLoggerProvider(options ?? NLogProviderOptions.Default, logFactory ?? LogManager.LogFactory);

            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);
            }

            return(provider);
        }
 private static NLogLoggerProvider CreateNLogLoggerProvider(IServiceProvider serviceProvider, IConfiguration configuration, NLogProviderOptions options)
 {
     return(CreateNLogLoggerProvider(serviceProvider, configuration, options, null));
 }
 public NLogBeginScopeParser(NLogProviderOptions options)
 {
     _options = options ?? NLogProviderOptions.Default;
 }
 public static ILoggerFactory AddNLog(this ILoggerFactory factory, NLogProviderOptions options)
 {
     factory.AddProvider(new NLogLoggerProvider(options));
     return(factory);
 }
 public NLogLogger(Logger logger, NLogProviderOptions options, NLogBeginScopeParser beginScopeParser)
 {
     _logger           = logger;
     _options          = options ?? NLogProviderOptions.Default;
     _beginScopeParser = beginScopeParser;
 }
Пример #22
0
 public NLogLogger(Logger logger, NLogProviderOptions options)
 {
     _logger  = logger;
     _options = options ?? NLogProviderOptions.Default;
 }
 /// <summary>
 /// New factory with options.
 /// </summary>
 /// <param name="options"></param>
 public NLogLoggerFactory(NLogProviderOptions options)
     : this(new NLogLoggerProvider(options))
 {
 }
        internal static void TryAddNLogLoggingProvider(this IServiceCollection services, Action <IServiceCollection, Action <ILoggingBuilder> > addLogging, IConfiguration configuration, NLogProviderOptions options, Func <IServiceProvider, IConfiguration, NLogProviderOptions, NLogLoggerProvider> factory)
        {
            var sharedFactory = factory;

            if (options?.ReplaceLoggerFactory == true)
            {
                NLogLoggerProvider singleInstance = null;   // Ensure that registration of ILoggerFactory and ILoggerProvider shares the same single instance
                sharedFactory = (provider, cfg, opt) => singleInstance ?? (singleInstance = factory(provider, cfg, opt));

                addLogging?.Invoke(services, (builder) => builder?.ClearProviders());  // Cleanup the existing LoggerFactory, before replacing it with NLogLoggerFactory
                services.Replace(ServiceDescriptor.Singleton <ILoggerFactory, NLogLoggerFactory>(serviceProvider => new NLogLoggerFactory(sharedFactory(serviceProvider, configuration, options))));
            }

            services.TryAddEnumerable(ServiceDescriptor.Singleton <ILoggerProvider, NLogLoggerProvider>(serviceProvider => sharedFactory(serviceProvider, configuration, options)));

            if (options?.RemoveLoggerFactoryFilter == true)
            {
                // Will forward all messages to NLog if not specifically overridden by user
                addLogging?.Invoke(services, (builder) => builder?.AddFilter <NLogLoggerProvider>(null, Microsoft.Extensions.Logging.LogLevel.Trace));
            }
        }
Пример #25
0
        private static NLogLoggerProvider CreateNLogLoggerProvider(IServiceProvider serviceProvider, IConfiguration configuration, NLogProviderOptions options)
        {
            NLogLoggerProvider provider = new NLogLoggerProvider(options ?? NLogProviderOptions.Default);

            configuration = configuration ?? (serviceProvider?.GetService(typeof(IConfiguration)) as IConfiguration);
            if (configuration != null)
            {
                ConfigSettingLayoutRenderer.DefaultConfiguration = configuration;
                if (options == null)
                {
                    provider.Configure(configuration.GetSection("Logging:NLog"));
                }
            }
            return(provider);
        }