public void CheckLogCapacity()
        {
            LoggerFactory        factory              = new LoggerFactory();
            string               category             = String.Empty;
            MemoryLoggerSettings memoryLoggerSettings = new MemoryLoggerSettings
            {
                MemoryCacheSize       = 10,
                AcceptedCategoryNames = new System.Collections.Generic.List <string> {
                    nameof(MemoryLogLoggerTest)
                },
            };
            MemoryLoggerProvider memoryLoggerProvider = new MemoryLoggerProvider(memoryLoggerSettings);

            factory.AddProvider(memoryLoggerProvider);

            // Create real logger
            ILogger logger = factory.CreateLogger(nameof(MemoryLogLoggerTest));

            // Send the log message
            EventId   eventId   = new EventId(1, nameof(EventId));
            Exception exception = new Exception();
            string    message   = "Message-{param}";

            object[] args = new object[] { "param" };

            for (int i = 0; i < memoryLoggerSettings.MemoryCacheSize + memoryLoggerSettings.MemoryCacheSize; i++)
            {
                logger.LogDebug(eventId, exception, message, args);
            }

            MemoryLogger lst;

            Assert.IsTrue(memoryLoggerProvider.MemoryLoggers[nameof(MemoryLogLoggerTest)].TryGetTarget(out lst));
            Assert.AreEqual(memoryLoggerSettings.MemoryCacheSize, lst.LogMessages.Count);
        }
        /// <summary>
        /// Adds a Memory logger named 'MemoryLogger' to the factory.
        /// </summary>
        /// <param name="builder">The <see cref="ILoggingBuilder"/> to use.</param>
        /// <param name="configurationSection">The configuration section that maps to <see cref="MemoryLoggerSettings"/></param>
        public static ILoggingBuilder AddMemoryLogger(this ILoggingBuilder builder, IConfigurationSection configurationSection)
        {
            MemoryLoggerSettings mLogSettings = configurationSection != null?configurationSection.Get <MemoryLoggerSettings>() : new MemoryLoggerSettings();

            var provider = new MemoryLoggerProvider(mLogSettings);

            builder.AddProvider(provider);
            return(builder);
        }
        /// <summary>
        /// Adds a Memory logger named 'MemoryLogger' to the service collection.
        /// </summary>
        /// <param name="services">The <see cref="IServiceCollection"/> to use.</param>
        /// <param name="configurationSection">The configuration section that maps to <see cref="MemoryLoggerSettings"/></param>
        public static IServiceCollection AddMemoryLogger(this IServiceCollection services, IConfigurationSection configurationSection)
        {
            MemoryLoggerSettings mLogSettings = configurationSection != null?configurationSection.Get <MemoryLoggerSettings>() : new MemoryLoggerSettings();

            var provider = new MemoryLoggerProvider(mLogSettings);

            services.AddSingleton(provider);
            return(services);
        }
        /// <summary>
        /// Adds a Memory logger named 'MemoryLogger' to the service collection.
        /// </summary>
        /// <param name="services">The <see cref="IServiceCollection"/> to use.</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 AddMemoryLogger(this IServiceCollection services, List <string> acceptedCategoryNames, LogLevel?minLevel = null, Func <string, LogLevel, bool> filter = null)
        {
            MemoryLoggerSettings mLogSettings = new MemoryLoggerSettings()
            {
                AcceptedCategoryNames = new List <string>(acceptedCategoryNames),
                MinLevel = minLevel,
                Filter   = filter
            };
            var provider = new MemoryLoggerProvider(mLogSettings);

            services.AddSingleton(provider);
            return(services);
        }
        /// <summary>
        /// Adds a Memory logger named 'MemoryLogger' to the factory.
        /// </summary>
        /// <param name="builder">The <see cref="ILoggingBuilder"/> to use.</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 AddMemoryLogger(this ILoggingBuilder builder, List <string> acceptedCategoryNames, LogLevel?minLevel = null, Func <string, LogLevel, bool> filter = null)
        {
            MemoryLoggerSettings mLogSettings = new MemoryLoggerSettings()
            {
                AcceptedCategoryNames = new List <string>(acceptedCategoryNames),
                MinLevel = minLevel,
                Filter   = filter
            };
            var provider = new MemoryLoggerProvider(mLogSettings);

            builder.AddProvider(provider);
            return(builder);
        }
        public void CreateMemoryLogger()
        {
            LoggerFactory        factory              = new LoggerFactory();
            string               category             = String.Empty;
            LogLevel             logLevel             = LogLevel.None;
            MemoryLoggerSettings memoryLoggerSettings = new MemoryLoggerSettings
            {
                MemoryCacheSize       = 10,
                AcceptedCategoryNames = new System.Collections.Generic.List <string> {
                    nameof(MemoryLogLoggerTest)
                },
                Filter = (s, l) => { category = s; logLevel = l; return(true); }
            };
            MemoryLoggerProvider memoryLoggerProvider = new MemoryLoggerProvider(memoryLoggerSettings);

            factory.AddProvider(memoryLoggerProvider);

            // Create real logger
            ILogger logger = factory.CreateLogger(nameof(MemoryLogLoggerTest));

            Assert.AreEqual(1, memoryLoggerProvider.MemoryLoggers.Count);

            // Send the log message
            EventId   eventId   = new EventId(1, nameof(EventId));
            Exception exception = new Exception();
            string    message   = "Message-{param}";

            object[] args = new object[] { "param" };

            logger.LogDebug(eventId, exception, message, args);

            MemoryLogger lst;

            Assert.IsTrue(memoryLoggerProvider.MemoryLoggers[nameof(MemoryLogLoggerTest)].TryGetTarget(out lst));

            // check the filter
            Assert.AreEqual(nameof(MemoryLogLoggerTest), category);
            Assert.AreEqual(LogLevel.Debug, logLevel);

            // check message
            MemoryLogEntry msg;

            Assert.IsTrue(lst.LogMessages.TryPeek(out msg));

            // check correct message
            Assert.AreEqual(eventId, msg.EventId);
            Assert.AreEqual(exception, msg.Exception);
            Assert.AreEqual("Message-param", msg.Message);
            Assert.AreEqual(LogLevel.Debug, msg.LogLevel);
            Assert.AreEqual(category, msg.LogName);
        }
        public void CreateAnyLogger()
        {
            LoggerFactory        factory = new LoggerFactory();
            MemoryLoggerSettings memoryLoggerSettings = new MemoryLoggerSettings
            {
                MemoryCacheSize = 10,
            };
            MemoryLoggerProvider memoryLoggerProvider = new MemoryLoggerProvider(memoryLoggerSettings);

            factory.AddProvider(memoryLoggerProvider);

            // Create fake logger
            ILogger logger = factory.CreateLogger("Fake");

            Assert.AreEqual(1, memoryLoggerProvider.MemoryLoggers.Count);
        }
        public void CreateNullLogger()
        {
            LoggerFactory        factory = new LoggerFactory();
            MemoryLoggerSettings memoryLoggerSettings = new MemoryLoggerSettings
            {
                MemoryCacheSize       = 10,
                AcceptedCategoryNames = new System.Collections.Generic.List <string> {
                    nameof(MemoryLogLoggerTest)
                }
            };
            MemoryLoggerProvider memoryLoggerProvider = new MemoryLoggerProvider(memoryLoggerSettings);

            factory.AddProvider(memoryLoggerProvider);

            // Create fake logger
            ILogger logger = factory.CreateLogger("Fake");

            Assert.AreEqual(0, memoryLoggerProvider.MemoryLoggers.Count);
        }