public void ValidAppsettingsFilter()
        {
            var configSection = LoggerConfigSectionSetup("ValidAppsettingsFilter.json", null).GetSection("LogLevel");

            if (!(configSection != null && configSection.GetChildren().Count() > 0))
            {
                configSection = null;
            }
            var categoryName = typeof(TestFilter).GetTypeInfo().FullName;
            var coreLogger   = new FakeCoreLogger();
            var logger       = new AWSLogger(
                categoryName,
                coreLogger,
                AWSLoggerProvider.CreateConfigSectionFilter(configSection, categoryName));

            logger.LogTrace("trace");
            logger.LogDebug("debug");
            logger.LogInformation("information");
            logger.LogWarning("warning");
            logger.LogError("error");
            logger.LogCritical("critical");

            Assert.Equal(5, coreLogger.ReceivedMessages.Count);
            Assert.True(coreLogger.ReceivedMessages.Contains("warning\r\n"));
            Assert.True(coreLogger.ReceivedMessages.Contains("error\r\n"));
            Assert.True(coreLogger.ReceivedMessages.Contains("critical\r\n"));
        }
        public void CustomFilter()
        {
            var coreLogger = new FakeCoreLogger();
            Func <string, LogLevel, bool> filter = (categoryName, level) =>
            {
                if (string.Equals(categoryName, "goodCategory", StringComparison.OrdinalIgnoreCase) && level >= LogLevel.Warning)
                {
                    return(true);
                }
                return(false);
            };
            var logger = new AWSLogger("goodCategory", coreLogger, filter);

            logger.LogTrace("trace");
            logger.LogWarning("warning");

            Assert.Equal(1, coreLogger.ReceivedMessages.Count);
            Assert.True(coreLogger.ReceivedMessages.Contains("warning\r\n"));
            string val;

            while (!coreLogger.ReceivedMessages.IsEmpty)
            {
                coreLogger.ReceivedMessages.TryDequeue(out val);
            }

            logger = new AWSLogger("badCategory", coreLogger, filter);

            logger.LogTrace("trace");
            logger.LogWarning("warning");

            Assert.Equal(0, coreLogger.ReceivedMessages.Count);
        }
Esempio n. 3
0
        public void DefaultFilterCheck()
        {
            var configSection = LoggerConfigSectionSetup("DefaultFilterCheck.json", null).GetSection("LogLevel");

            if (!(configSection != null && configSection.GetChildren().Count() > 0))
            {
                configSection = null;
            }
            var categoryName = "AWS.Log";
            var coreLogger   = new FakeCoreLogger();
            var logger       = new AWSLogger(
                categoryName,
                coreLogger,
                AWSLoggerProvider.CreateConfigSectionFilter(configSection, categoryName));

            logger.LogTrace("trace");
            logger.LogDebug("debug");
            logger.LogInformation("information");
            logger.LogWarning("warning");
            logger.LogError("error");
            logger.LogCritical("critical");

            Assert.Equal(5, coreLogger.ReceivedMessages.Count);
            Assert.True(coreLogger.ReceivedMessages.Contains("warning"));
            Assert.True(coreLogger.ReceivedMessages.Contains("error"));
            Assert.True(coreLogger.ReceivedMessages.Contains("critical"));
        }
        public void MissingLogLevelCheck()
        {
            var configSection = LoggerConfigSectionSetup("MissingLogLevelCheck.json", null).GetSection("LogLevel");

            if (!(configSection != null && configSection.GetChildren().Count() > 0))
            {
                configSection = null;
            }
            var categoryName = typeof(TestFilter).GetTypeInfo().FullName;
            var coreLogger   = new FakeCoreLogger();
            var logger       = new AWSLogger(
                categoryName,
                coreLogger,
                AWSLoggerProvider.CreateConfigSectionFilter(configSection, categoryName));

            logger.LogTrace("trace");
            logger.LogDebug("debug");
            logger.LogInformation("information");
            logger.LogWarning("warning");
            logger.LogError("error");
            logger.LogCritical("critical");

            Assert.Equal(6, coreLogger.ReceivedMessages.Count);
            Assert.Contains($"[Warning] AWS.Logger.AspNetCore.Tests.TestFilter: warning{Environment.NewLine}", coreLogger.ReceivedMessages);
            Assert.Contains($"[Error] AWS.Logger.AspNetCore.Tests.TestFilter: error{Environment.NewLine}", coreLogger.ReceivedMessages);
            Assert.Contains($"[Critical] AWS.Logger.AspNetCore.Tests.TestFilter: critical{Environment.NewLine}", coreLogger.ReceivedMessages);
        }
        public void FilterLogLevel()
        {
            var coreLogger = new FakeCoreLogger();
            var logger     = new AWSLogger("FilterLogLevel", coreLogger, AWSLoggerProvider.CreateLogLevelFilter(LogLevel.Warning));

            logger.LogTrace("trace");
            logger.LogDebug("debug");
            logger.LogInformation("information");
            logger.LogWarning("warning");
            logger.LogError("error");
            logger.LogCritical("critical");

            Assert.Equal(3, coreLogger.ReceivedMessages.Count);
            Assert.True(coreLogger.ReceivedMessages.Contains("warning\r\n"));
            Assert.True(coreLogger.ReceivedMessages.Contains("error\r\n"));
            Assert.True(coreLogger.ReceivedMessages.Contains("critical\r\n"));
        }
        public void FilterLogLevel()
        {
            var coreLogger = new FakeCoreLogger();
            var logger     = new AWSLogger("FilterLogLevel", coreLogger, AWSLoggerProvider.CreateLogLevelFilter(LogLevel.Warning));

            logger.LogTrace("trace");
            logger.LogDebug("debug");
            logger.LogInformation("information");
            logger.LogWarning("warning");
            logger.LogError("error");
            logger.LogCritical("critical");

            Assert.Equal(3, coreLogger.ReceivedMessages.Count);
            Assert.Contains($"[Warning] FilterLogLevel: warning{Environment.NewLine}", coreLogger.ReceivedMessages);
            Assert.Contains($"[Error] FilterLogLevel: error{Environment.NewLine}", coreLogger.ReceivedMessages);
            Assert.Contains($"[Critical] FilterLogLevel: critical{Environment.NewLine}", coreLogger.ReceivedMessages);
        }