/// <summary>
        /// Registers NLog extensions from the assembly type name
        /// </summary>
        public static ISetupExtensionsBuilder RegisterAssembly(this ISetupExtensionsBuilder setupBuilder, string assemblyName)
        {
            Assembly assembly = AssemblyHelpers.LoadFromName(assemblyName);

            ConfigurationItemFactory.Default.RegisterItemsFromAssembly(assembly);
            return(setupBuilder);
        }
示例#2
0
        /// <summary>
        /// Register a custom layout renderer with a callback function <paramref name="layoutMethod"/>. The callback receives the logEvent and the current configuration.
        /// </summary>
        /// <param name="setupBuilder">Fluent interface parameter.</param>
        /// <param name="name">Name of the layout renderer - without ${}.</param>
        /// <param name="layoutMethod">Callback that returns the value for the layout renderer.</param>
        /// <param name="options">Options of the layout renderer.</param>
        public static ISetupExtensionsBuilder RegisterLayoutRenderer(this ISetupExtensionsBuilder setupBuilder, string name, Func <LogEventInfo, LoggingConfiguration, object> layoutMethod, LayoutRenderOptions options)
        {
            FuncLayoutRenderer layoutRenderer = Layout.CreateFuncLayoutRenderer(layoutMethod, options, name);

            setupBuilder.LogFactory.ServiceRepository.ConfigurationItemFactory.GetLayoutRenderers().RegisterFuncLayout(name, layoutRenderer);
            return(setupBuilder);
        }
        /// <summary>
        /// Register a custom layout renderer.
        /// </summary>
        /// <param name="setupBuilder">Fluent interface parameter.</param>
        /// <typeparam name="T"> Type of the layout renderer.</typeparam>
        /// <param name="name"> Name of the layout renderer - without ${}.</param>
        public static ISetupExtensionsBuilder RegisterLayoutRenderer <T>(this ISetupExtensionsBuilder setupBuilder, string name)
            where T : LayoutRenderer
        {
            var layoutRendererType = typeof(T);

            return(RegisterLayoutRenderer(setupBuilder, name, layoutRendererType));
        }
        /// <summary>
        /// Register a custom layout renderer with a callback function <paramref name="layoutMethod"/>. The callback receives the logEvent and the current configuration.
        /// </summary>
        /// <param name="setupBuilder">Fluent interface parameter.</param>
        /// <param name="name">Name of the layout renderer - without ${}.</param>
        /// <param name="layoutMethod">Callback that returns the value for the layout renderer.</param>
        public static ISetupExtensionsBuilder RegisterLayoutRenderer(this ISetupExtensionsBuilder setupBuilder, string name, Func <LogEventInfo, LoggingConfiguration, object> layoutMethod)
        {
            var layoutRenderer = new FuncLayoutRenderer(name, layoutMethod);

            ConfigurationItemFactory.Default.GetLayoutRenderers().RegisterFuncLayout(name, layoutRenderer);
            return(setupBuilder);
        }
示例#5
0
        /// <summary>
        /// Register a custom layout renderer.
        /// </summary>
        /// <typeparam name="T">Type of the layout renderer.</typeparam>
        /// <param name="setupBuilder">Fluent interface parameter.</param>
        /// <param name="overrideName">Symbol-name of the layout renderer - without ${}. Will extract from class-attribute when unassigned.</param>
        public static ISetupExtensionsBuilder RegisterLayoutRenderer <T>(this ISetupExtensionsBuilder setupBuilder, string overrideName = null)
            where T : LayoutRenderer
        {
            var layoutRendererType = typeof(T);

            return(RegisterLayoutRenderer(setupBuilder, layoutRendererType, overrideName));
        }
示例#6
0
 public static ISetupExtensionsBuilder AutoLoadAssemblies(this ISetupExtensionsBuilder setupBuilder, bool enable)
 {
     if (enable)
     {
         AutoLoadExtensions(setupBuilder);
     }
     return(setupBuilder);
 }
示例#7
0
 /// <summary>
 /// Register (or replaces) singleton-object for the specified service-type
 /// </summary>
 /// <typeparam name="T">Service interface type</typeparam>
 /// <param name="setupBuilder">Fluent interface parameter.</param>
 /// <param name="singletonService">Implementation of interface.</param>
 public static ISetupExtensionsBuilder RegisterSingletonService <T>(this ISetupExtensionsBuilder setupBuilder, T singletonService) where T : class
 {
     if (singletonService == null)
     {
         throw new ArgumentNullException(nameof(singletonService));
     }
     setupBuilder.LogFactory.ServiceRepository.RegisterSingleton <T>(singletonService);
     return(setupBuilder);
 }
 /// <summary>
 /// Register a custom condition method, that can use in condition filters
 /// </summary>
 /// <param name="setupBuilder">Fluent interface parameter.</param>
 /// <param name="name">Name of the condition filter method</param>
 /// <param name="conditionMethod">Lambda method.</param>
 public static ISetupExtensionsBuilder RegisterConditionMethod(this ISetupExtensionsBuilder setupBuilder, string name, Func <object> conditionMethod)
 {
     if (conditionMethod == null)
     {
         throw new ArgumentNullException(nameof(conditionMethod));
     }
     ReflectionHelpers.LateBoundMethod lateBound = (target, args) => conditionMethod();
     return(RegisterConditionMethod(setupBuilder, name, conditionMethod.Method, lateBound));
 }
        /// <summary>
        /// Register the NLog.Web.AspNetCore LayoutRenderers
        /// </summary>
        /// <remarks>
        /// If not providing <paramref name="serviceProvider"/>, then output from aspnet-layoutrenderers will remain empty
        /// </remarks>
        public static ISetupExtensionsBuilder RegisterNLogWeb(this ISetupExtensionsBuilder setupBuilder, IServiceProvider serviceProvider = null)
        {
            if (serviceProvider != null)
            {
                ServiceLocator.ServiceProvider = serviceProvider;
            }

            return(setupBuilder.RegisterAssembly(typeof(NLogAspNetCoreOptions).GetTypeInfo().Assembly));
        }
示例#10
0
        /// <summary>
        /// Register (or replaces) external service-repository for resolving dependency injection
        /// </summary>
        /// <param name="setupBuilder">Fluent interface parameter.</param>
        /// <param name="serviceProvider">External dependency injection repository</param>
        public static ISetupExtensionsBuilder RegisterServiceProvider(this ISetupExtensionsBuilder setupBuilder, IServiceProvider serviceProvider)
        {
            if (serviceProvider == null)
            {
                throw new ArgumentNullException(nameof(serviceProvider));
            }

            setupBuilder.LogFactory.ServiceRepository.RegisterSingleton(serviceProvider);
            return(setupBuilder);
        }
示例#11
0
 /// <summary>
 /// Register a custom layout renderer.
 /// </summary>
 /// <param name="setupBuilder">Fluent interface parameter.</param>
 /// <param name="layoutRendererType">Type of the layout renderer.</param>
 /// <param name="overrideName">Symbol-name of the layout renderer - without ${}. Will extract from class-attribute when unassigned.</param>
 public static ISetupExtensionsBuilder RegisterLayoutRenderer(this ISetupExtensionsBuilder setupBuilder, Type layoutRendererType, string overrideName = null)
 {
     overrideName = string.IsNullOrEmpty(overrideName) ? layoutRendererType.GetFirstCustomAttribute <LayoutRendererAttribute>()?.Name : overrideName;
     if (string.IsNullOrEmpty(overrideName))
     {
         throw new ArgumentException("Empty type name. Missing class attribute?", nameof(overrideName));
     }
     setupBuilder.LogFactory.ServiceRepository.ConfigurationItemFactory.LayoutRenderers.RegisterDefinition(overrideName, layoutRendererType);
     return(setupBuilder);
 }
        /// <summary>
        /// Register a custom condition method, that can use in condition filters
        /// </summary>
        /// <param name="setupBuilder">Fluent interface parameter.</param>
        /// <param name="name">Name of the condition filter method</param>
        /// <param name="conditionMethod">MethodInfo extracted by reflection - typeof(MyClass).GetMethod("MyFunc", BindingFlags.Static).</param>
        public static ISetupExtensionsBuilder RegisterConditionMethod(this ISetupExtensionsBuilder setupBuilder, string name, MethodInfo conditionMethod)
        {
            if (conditionMethod == null)
            {
                throw new ArgumentNullException(nameof(conditionMethod));
            }
            if (!conditionMethod.IsStatic)
            {
                throw new ArgumentException($"{conditionMethod.Name} must be static", nameof(conditionMethod));
            }

            ConfigurationItemFactory.Default.ConditionMethods.RegisterDefinition(name, conditionMethod);
            return(setupBuilder);
        }
示例#13
0
 /// <summary>
 /// Register (or replaces) singleton-object for the specified service-type
 /// </summary>
 /// <param name="setupBuilder">Fluent interface parameter.</param>
 /// <param name="interfaceType">Service interface type.</param>
 /// <param name="singletonService">Implementation of interface.</param>
 public static ISetupExtensionsBuilder RegisterSingletonService(this ISetupExtensionsBuilder setupBuilder, Type interfaceType, object singletonService)
 {
     if (interfaceType == null)
     {
         throw new ArgumentNullException(nameof(interfaceType));
     }
     if (singletonService == null)
     {
         throw new ArgumentNullException(nameof(singletonService));
     }
     if (!interfaceType.IsAssignableFrom(singletonService.GetType()))
     {
         throw new ArgumentException("Service instance not matching type", nameof(singletonService));
     }
     setupBuilder.LogFactory.ServiceRepository.RegisterService(interfaceType, singletonService);
     return(setupBuilder);
 }
示例#14
0
 /// <summary>
 /// Registers NLog extensions from the assembly.
 /// </summary>
 public static ISetupExtensionsBuilder RegisterAssembly(this ISetupExtensionsBuilder setupBuilder, Assembly assembly)
 {
     setupBuilder.LogFactory.ServiceRepository.ConfigurationItemFactory.RegisterItemsFromAssembly(assembly);
     return(setupBuilder);
 }
 private static ISetupExtensionsBuilder RegisterConditionMethod(this ISetupExtensionsBuilder setupBuilder, string name, MethodInfo conditionMethod, ReflectionHelpers.LateBoundMethod lateBoundMethod)
 {
     ConfigurationItemFactory.Default.ConditionMethodDelegates.RegisterDefinition(name, conditionMethod, lateBoundMethod);
     return(setupBuilder);
 }
 /// <summary>
 /// Register a custom layout renderer with a callback function <paramref name="layoutMethod"/>. The callback receives the logEvent.
 /// </summary>
 /// <param name="setupBuilder">Fluent interface parameter.</param>
 /// <param name="name">Name of the layout renderer - without ${}.</param>
 /// <param name="layoutMethod">Callback that returns the value for the layout renderer.</param>
 public static ISetupExtensionsBuilder RegisterLayoutRenderer(this ISetupExtensionsBuilder setupBuilder, string name, Func <LogEventInfo, object> layoutMethod)
 {
     return(RegisterLayoutRenderer(setupBuilder, name, (info, configuration) => layoutMethod(info)));
 }
 /// <summary>
 /// Register a custom layout renderer.
 /// </summary>
 /// <param name="setupBuilder">Fluent interface parameter.</param>
 /// <param name="layoutRendererType"> Type of the layout renderer.</param>
 /// <param name="name"> Name of the layout renderer - without ${}.</param>
 public static ISetupExtensionsBuilder RegisterLayoutRenderer(this ISetupExtensionsBuilder setupBuilder, string name, Type layoutRendererType)
 {
     ConfigurationItemFactory.Default.LayoutRenderers.RegisterDefinition(name, layoutRendererType);
     return(setupBuilder);
 }
示例#18
0
 /// <summary>
 /// Register a custom layout renderer with a callback function <paramref name="layoutMethod"/>. The callback receives the logEvent and the current configuration.
 /// </summary>
 /// <param name="setupBuilder">Fluent interface parameter.</param>
 /// <param name="name">Name of the layout renderer - without ${}.</param>
 /// <param name="layoutMethod">Callback that returns the value for the layout renderer.</param>
 public static ISetupExtensionsBuilder RegisterLayoutRenderer(this ISetupExtensionsBuilder setupBuilder, string name, Func <LogEventInfo, LoggingConfiguration, object> layoutMethod)
 {
     return(RegisterLayoutRenderer(setupBuilder, name, layoutMethod, LayoutRenderOptions.None));
 }
        /// <summary>
        /// Register a custom Target.
        /// </summary>
        /// <remarks>Short-cut for registering to default <see cref="ConfigurationItemFactory"/></remarks>
        /// <param name="extensionsBuilder">Fluent interface parameter.</param>
        /// <typeparam name="T"> Type of the Target.</typeparam>
        /// <param name="name"> Name of the Target.</param>
        public static ISetupExtensionsBuilder RegisterTarget <T>(this ISetupExtensionsBuilder extensionsBuilder, string name) where T : Target
        {
            var layoutRendererType = typeof(T);

            return(RegisterTarget(extensionsBuilder, name, layoutRendererType));
        }
示例#20
0
 /// <summary>
 /// Replace with version from NLog.Extension.Logging when it has been released with NLog 4.7
 /// </summary>
 internal static ISetupExtensionsBuilder RegisterConfigSettings(this ISetupExtensionsBuilder setupBuilder, IConfiguration configuration)
 {
     ConfigSettingLayoutRenderer.DefaultConfiguration = configuration;
     ConfigurationItemFactory.Default.RegisterType(typeof(ConfigSettingLayoutRenderer), string.Empty);
     return(setupBuilder);
 }
 /// <summary>
 /// Register a custom layout renderer using custom delegate-method <paramref name="layoutMethod" />
 /// </summary>
 /// <param name="setupBuilder">Fluent style</param>
 /// <param name="name">Name of the layout renderer - without ${}.</param>
 /// <param name="layoutMethod">Delegate method that returns layout renderer output.</param>
 public static ISetupExtensionsBuilder RegisterAspNetLayoutRenderer(this ISetupExtensionsBuilder setupBuilder, string name, Func <LogEventInfo, HttpContextBase, LoggingConfiguration, object> layoutMethod)
 {
     AspNetLayoutRendererBase.Register(name, layoutMethod);
     return(setupBuilder);
 }
 /// <summary>
 /// Enable/disables autoloading of NLog extensions by scanning and loading available assemblies
 /// </summary>
 /// <remarks>
 /// Enabled by default, and gives a huge performance hit during startup. Recommended to disable this when running in the cloud.
 /// </remarks>
 public static ISetupExtensionsBuilder AutoLoadAssemblies(this ISetupExtensionsBuilder setupBuilder, bool enable)
 {
     ConfigurationItemFactory.Default = enable ? null : new ConfigurationItemFactory(typeof(SetupBuilderExtensions).GetAssembly());
     return(setupBuilder);
 }
示例#23
0
 /// <summary>
 /// Enable/disables autoloading of NLog extensions by scanning and loading available assemblies
 /// </summary>
 /// <remarks>
 /// Disabled by default as it can give a huge performance hit during startup. Recommended to keep it disabled especially when running in the cloud.
 /// </remarks>
 public static ISetupExtensionsBuilder AutoLoadExtensions(this ISetupExtensionsBuilder setupBuilder)
 {
     ConfigurationItemFactory.ScanForAutoLoadExtensions(setupBuilder.LogFactory);
     return(setupBuilder);
 }
 /// <summary>
 /// Registers NLog extensions from the assembly.
 /// </summary>
 public static ISetupExtensionsBuilder RegisterAssembly(this ISetupExtensionsBuilder setupBuilder, Assembly assembly)
 {
     ConfigurationItemFactory.Default.RegisterItemsFromAssembly(assembly);
     return(setupBuilder);
 }
示例#25
0
 /// <summary>
 /// Setup the MEL-configuration for the ${configsetting} layoutrenderer
 /// </summary>
 public static ISetupExtensionsBuilder RegisterConfigSettings(this ISetupExtensionsBuilder setupBuilder, IConfiguration configuration)
 {
     ConfigSettingLayoutRenderer.DefaultConfiguration = configuration;
     return(setupBuilder.RegisterLayoutRenderer <ConfigSettingLayoutRenderer>("configsetting"));
 }
 /// <summary>
 /// Register a custom Target.
 /// </summary>
 /// <param name="setupBuilder">Fluent interface parameter.</param>
 /// <param name="targetType"> Type of the Target.</param>
 /// <param name="name"> Name of the Target.</param>
 public static ISetupExtensionsBuilder RegisterTarget(this ISetupExtensionsBuilder setupBuilder, string name, Type targetType)
 {
     ConfigurationItemFactory.Default.Targets.RegisterDefinition(name, targetType);
     return(setupBuilder);
 }
 /// <summary>
 /// Register the NLog.Web LayoutRenderers
 /// </summary>
 public static ISetupExtensionsBuilder RegisterNLogWeb(this ISetupExtensionsBuilder setupBuilder)
 {
     return(setupBuilder.RegisterAssembly(typeof(SetupExtensionsBuilderExtensions).Assembly));
 }