/// <summary>
        /// Creates new or updates an existing configuration with the console target name "ConsoleTarget".
        /// It will be like the [target xsi:type="Console" name="ConsoleTarget" layout="${date:universalTime=true:format=HH\:mm\:ss}|${level:uppercase=true}|${message}" ]
        /// The rule will be set to like  [logger name="*" minlevel="Info" writeTo="ConsoleTarget"]
        /// </summary>
        /// <param name="nLogSettings">The settings for <see cref="NLogLogger"/>.</param>
        /// <param name="configuration">The NLog logging configuration.</param>
        /// <returns></returns>
        public static LoggingConfiguration GenerateUpdateConsoleLoggingConfiguration(NLogLoggerSettings nLogSettings, LoggingConfiguration configuration = null)
        {
            LoggingConfiguration nlogConfiguration = configuration ?? new LoggingConfiguration();
            var consoleTarget = new ConsoleTarget();

            consoleTarget.Name   = nameof(ConsoleTarget);
            consoleTarget.Layout = @"${date:universalTime=true:format=HH\:mm\:ss}|${level:uppercase=true}|${message}";
            nlogConfiguration.RemoveTarget(consoleTarget.Name);
            nlogConfiguration.AddTarget(consoleTarget.Name, consoleTarget);

            if (nLogSettings.AcceptedCategoryNames.Count == 0)
            {
                var rule = new LoggingRule("*", NLog.LogLevel.Info, NLog.LogLevel.Fatal, consoleTarget);
                nlogConfiguration.LoggingRules.Add(rule);
            }
            else
            {
                // Create rules for all categories in the settings.
                foreach (var loggerNamePattern in nLogSettings.AcceptedCategoryNames)
                {
                    var rule = new LoggingRule(loggerNamePattern, NLog.LogLevel.Info, NLog.LogLevel.Fatal, consoleTarget);
                    nlogConfiguration.LoggingRules.Add(rule);
                }
            }
            return(nlogConfiguration);
        }
Beispiel #2
0
 /// <summary>
 /// /
 /// </summary>
 /// <param name="categoryName"></param>
 /// <param name="nLogSettings"></param>
 /// <param name="externalScopeProvider">The scope data provider.</param>
 public NLogLogger(string categoryName, NLogLoggerSettings nLogSettings, IExternalScopeProvider externalScopeProvider)
 {
     CategoryName   = categoryName;
     _settings      = nLogSettings ?? throw new ArgumentNullException(nameof(nLogSettings));
     _scopeProvider = externalScopeProvider ?? throw new ArgumentNullException(nameof(externalScopeProvider));
     Logger         = LogManager.GetLogger(categoryName);
 }
        /// <summary>
        /// Adds a NLog logger named 'NLogLogger' to the factory.
        /// </summary>
        /// <param name="services">The <see cref="IServiceCollection"/> to use.</param>
        /// <param name="nLoggingConfiguration">Add NLog logging configuration.</param>
        /// <param name="configurationSection">The configuration section that maps to <see cref="NLogLoggerSettings"/></param>
        public static IServiceCollection AddNLogLogger(this IServiceCollection services, IConfigurationSection configurationSection, LoggingConfiguration nLoggingConfiguration = null)
        {
            NLogLoggerSettings nLogSettings = configurationSection != null?configurationSection.Get <NLogLoggerSettings>() : new NLogLoggerSettings();

            var provider = new NLogLoggerProvider(nLogSettings);

            if (nLoggingConfiguration != null)
            {
                provider.SetupLoggingConfiguration(nLoggingConfiguration);
            }
            services.AddSingleton(provider);
            return(services);
        }
        /// <summary>
        /// Adds a NLog logger named 'NLogLogger' to the factory.
        /// </summary>
        /// <param name="builder">The <see cref="ILoggingBuilder"/> to use.</param>
        /// <param name="nLoggingConfiguration">Add NLog logging configuration.</param>
        /// <param name="configurationSection">The configuration section that maps to <see cref="NLogLoggerSettings"/></param>
        public static ILoggingBuilder AddNLogLogger(this ILoggingBuilder builder, IConfigurationSection configurationSection, LoggingConfiguration nLoggingConfiguration = null)
        {
            NLogLoggerSettings nLogSettings = configurationSection != null?configurationSection.Get <NLogLoggerSettings>() : new NLogLoggerSettings();

            var provider = new NLogLoggerProvider(nLogSettings);

            if (nLoggingConfiguration != null)
            {
                provider.SetupLoggingConfiguration(nLoggingConfiguration);
            }
            builder.AddProvider(provider);
            return(builder);
        }
Beispiel #5
0
        /// <summary>
        /// Checks if the given <paramref name="level"/> is enabled.
        /// </summary>
        /// <param name="nLogSettings">The memory logger settings</param>
        /// <param name="level">level to be checked.</param>
        /// <returns><c>true</c> if enabled.</returns>

        public bool IsEnabled(Microsoft.Extensions.Logging.LogLevel level, NLogLoggerSettings nLogSettings)
        {
            if (nLogSettings.MinLevel != null && level < nLogSettings.MinLevel)
            {
                return(false);
            }

            if (nLogSettings.Filter != null)
            {
                return(nLogSettings.Filter(CategoryName, level));
            }

            return(true);
        }
        /// <summary>
        /// Adds a NLog logger named 'NLogLogger' to the factory.
        /// </summary>
        /// <param name="services">The <see cref="IServiceCollection"/> to use.</param>
        /// <param name="nLoggingConfiguration">Add NLog logging configuration.</param>
        /// <param name="acceptedCategoryNames">The list of accepted category names.</param>
        /// <param name="minLevel">The logging severity level.</param>
        /// <param name="filter">The filter based on the log level and category name.</param>
        public static IServiceCollection AddNLogLogger(this IServiceCollection services, List <string> acceptedCategoryNames, LoggingConfiguration nLoggingConfiguration = null, LogLevel?minLevel = null, Func <string, LogLevel, bool> filter = null)
        {
            NLogLoggerSettings nLogSettings = new NLogLoggerSettings()
            {
                AcceptedCategoryNames = new List <string>(acceptedCategoryNames),
                MinLevel = minLevel,
                Filter   = filter
            };
            var provider = new NLogLoggerProvider(nLogSettings);

            if (nLoggingConfiguration != null)
            {
                provider.SetupLoggingConfiguration(nLoggingConfiguration);
            }
            services.AddSingleton(provider);
            return(services);
        }
        /// <summary>
        /// Adds a NLog logger named 'NLogLogger' to the factory.
        /// </summary>
        /// <param name="builder">The <see cref="ILoggingBuilder"/> to use.</param>
        /// <param name="nLoggingConfiguration">Add NLog logging configuration.</param>
        /// <param name="acceptedCategoryNames">The list of accepted category names.</param>
        /// <param name="minLevel">The logging severity level.</param>
        /// <param name="filter">The filter based on the log level and category name.</param>
        public static ILoggingBuilder AddNLogLogger(this ILoggingBuilder builder, List <string> acceptedCategoryNames, LoggingConfiguration nLoggingConfiguration = null, LogLevel?minLevel = null, Func <string, LogLevel, bool> filter = null)
        {
            NLogLoggerSettings nLogSettings = new NLogLoggerSettings()
            {
                AcceptedCategoryNames = new List <string>(acceptedCategoryNames),
                MinLevel = minLevel,
                Filter   = filter
            };
            var provider = new NLogLoggerProvider(nLogSettings);

            if (nLoggingConfiguration != null)
            {
                provider.SetupLoggingConfiguration(nLoggingConfiguration);
            }
            builder.AddProvider(provider);
            return(builder);
        }
Beispiel #8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="NLogLoggerProvider"/> class.
 /// </summary>
 /// <param name="nLogSettings">The logger settings <see cref="NLogLoggerSettings"/></param>
 public NLogLoggerProvider(NLogLoggerSettings nLogSettings)
 {
     _nLogSettings = nLogSettings ?? throw new ArgumentNullException(nameof(nLogSettings));
 }