public void LogListenerInterceptorShouldLogIsEvaluated(LogLevel logLevel, bool expectedResult)
        {
            CommonTestHelpers.ResetContext();

            List <LogMessage> messageArgs = new List <LogMessage>();

            ILogListener listener = new CustomLogListener(onMessage: (LogMessage message) => { messageArgs.Add(message); })
            {
                Interceptor = new CustomLogListenerInterceptor
                {
                    ShouldLogMessage = (LogMessage message) =>
                    {
                        return(message.LogLevel >= LogLevel.Warning);
                    }
                }
            };

            KissLogConfiguration.Listeners.Add(listener);

            LogMessage message = new LogMessage(new LogMessage.CreateOptions
            {
                CategoryName = Constants.DefaultLoggerCategoryName,
                LogLevel     = logLevel,
                Message      = "Message"
            });

            NotifyOnMessage.Notify(message);

            Assert.AreEqual(expectedResult, messageArgs.Count == 1);
        }
        public void LogListenerInterceptorShouldLogIsEvaluated()
        {
            CommonTestHelpers.ResetContext();

            List <HttpRequest> httpRequestArgs = new List <HttpRequest>();

            ILogListener listener = new CustomLogListener(onBeginRequest: (arg) => { httpRequestArgs.Add(arg); })
            {
                Interceptor = new CustomLogListenerInterceptor
                {
                    ShouldLogBeginRequest = (HttpRequest httpRequest) =>
                    {
                        return(httpRequest.Url.LocalPath == "/App/Method1" || httpRequest.Url.LocalPath == "/App/Method3");
                    }
                }
            };

            KissLogConfiguration.Listeners.Add(listener);

            string[] urls = new[] { "/App/Method1", "/App/Method2", "/App/Method3", "/App/Method4" };

            foreach (string url in urls)
            {
                HttpRequest httpRequest = new HttpRequest(new HttpRequest.CreateOptions
                {
                    Url        = UrlParser.GenerateUri(url),
                    HttpMethod = "GET"
                });

                NotifyBeginRequest.Notify(httpRequest);
            }

            Assert.AreEqual(2, httpRequestArgs.Count);
        }
Exemplo n.º 3
0
        public void LogMessagesAreFilteredByInterceptorShouldLog()
        {
            CommonTestHelpers.ResetContext();

            List <FlushLogArgs> args = new List <FlushLogArgs>();

            KissLogConfiguration.Listeners.Add(new CustomLogListener(onFlush: (FlushLogArgs arg) => args.Add(arg))
            {
                Interceptor = new CustomLogListenerInterceptor
                {
                    ShouldLogMessage = (LogMessage message) =>
                    {
                        return(message.LogLevel >= LogLevel.Warning);
                    }
                }
            });

            Logger logger = new Logger();

            logger.Trace("Trace");
            logger.Debug("Debug");
            logger.Info("Info");
            logger.Warn("Warn");
            logger.Error("Error");
            logger.Critical("Critical");

            NotifyFlush.Notify(new[] { logger });

            Assert.AreEqual(3, args[0].MessagesGroups.ElementAt(0).Messages.Count());
        }
        public void NotifyShouldNotBeInvokedIfInterceptorShouldLogBeginRequestReturnsFalse()
        {
            CommonTestHelpers.ResetContext();

            List <LogMessage> messageArgs = new List <LogMessage>();

            ILogListener listener1 = new CustomLogListener(onMessage: (LogMessage message) => { messageArgs.Add(message); })
            {
                Interceptor = new CustomLogListenerInterceptor
                {
                    ShouldLogBeginRequest = (HttpRequest httpRequest) => httpRequest.Url.LocalPath == "/App/Method2"
                }
            };

            KissLogConfiguration.Listeners.Add(listener1);

            ILogListener listener2 = new CustomLogListener(onMessage: (LogMessage message) => { messageArgs.Add(message); });

            KissLogConfiguration.Listeners.Add(listener2);

            Logger logger = new Logger(url: "/App/Method1");

            logger.Trace("Message 1");
            logger.Trace("Message 2");

            logger = new Logger(url: "/App/Method2");
            logger.Trace("Message 1");
            logger.Trace("Message 2");

            Assert.AreEqual(6, messageArgs.Count);
        }
Exemplo n.º 5
0
        public void OptionsAreEvaluated()
        {
            CommonTestHelpers.ResetContext();

            FlushLogArgs flushLogArgs = null;

            KissLogConfiguration.Options.ShouldLogRequestHeader((OptionsArgs.LogListenerHeaderArgs args) => false);
            KissLogConfiguration.Options.ShouldLogRequestCookie((OptionsArgs.LogListenerCookieArgs args) => false);
            KissLogConfiguration.Options.ShouldLogFormData((OptionsArgs.LogListenerFormDataArgs args) => false);
            KissLogConfiguration.Options.ShouldLogServerVariable((OptionsArgs.LogListenerServerVariableArgs args) => false);
            KissLogConfiguration.Options.ShouldLogClaim((OptionsArgs.LogListenerClaimArgs args) => false);
            KissLogConfiguration.Options.ShouldLogInputStream((OptionsArgs.LogListenerInputStreamArgs args) => false);
            KissLogConfiguration.Options.ShouldLogResponseHeader((OptionsArgs.LogListenerHeaderArgs args) => false);

            KissLogConfiguration.Listeners.Add(new CustomLogListener(onFlush: (FlushLogArgs arg) => flushLogArgs = arg));

            Logger logger = new Logger();

            logger.DataContainer.SetHttpProperties(CommonTestHelpers.Factory.CreateHttpProperties());

            NotifyFlush.Notify(new[] { logger });

            Assert.AreEqual(0, flushLogArgs.HttpProperties.Request.Properties.Headers.Count());
            Assert.AreEqual(0, flushLogArgs.HttpProperties.Request.Properties.Cookies.Count());
            Assert.AreEqual(0, flushLogArgs.HttpProperties.Request.Properties.FormData.Count());
            Assert.AreEqual(0, flushLogArgs.HttpProperties.Request.Properties.ServerVariables.Count());
            Assert.AreEqual(0, flushLogArgs.HttpProperties.Request.Properties.Claims.Count());
            Assert.IsNull(flushLogArgs.HttpProperties.Request.Properties.InputStream);

            Assert.AreEqual(0, flushLogArgs.HttpProperties.Response.Properties.Headers.Count());
        }
Exemplo n.º 6
0
        public void NotifyShouldNotBeInvokedIfInterceptorShouldLogBeginRequestReturnsFalse()
        {
            CommonTestHelpers.ResetContext();

            List <FlushLogArgs> flushLogArgs = new List <FlushLogArgs>();

            ILogListener listener1 = new CustomLogListener(onFlush: (FlushLogArgs args) => { flushLogArgs.Add(args); })
            {
                Interceptor = new CustomLogListenerInterceptor
                {
                    ShouldLogBeginRequest = (HttpRequest httpRequest) => httpRequest.Url.LocalPath == "/App/Method2"
                }
            };

            KissLogConfiguration.Listeners.Add(listener1);

            ILogListener listener2 = new CustomLogListener(onFlush: (FlushLogArgs args) => { flushLogArgs.Add(args); });

            KissLogConfiguration.Listeners.Add(listener2);

            Logger logger = new Logger(url: "/App/Method1");

            NotifyFlush.Notify(new[] { logger });

            logger = new Logger(url: "/App/Method2");
            NotifyFlush.Notify(new[] { logger });

            logger = new Logger(url: "/App/Method3");
            NotifyFlush.Notify(new[] { logger });

            Assert.AreEqual(4, flushLogArgs.Count);
        }
Exemplo n.º 7
0
        public void LogListenerInterceptorShouldLogIsEvaluated(int statusCode, bool expectedResult)
        {
            CommonTestHelpers.ResetContext();

            List <FlushLogArgs> args = new List <FlushLogArgs>();

            KissLogConfiguration.Listeners.Add(new CustomLogListener(onFlush: (FlushLogArgs arg) => args.Add(arg))
            {
                Interceptor = new CustomLogListenerInterceptor
                {
                    ShouldLogFlush = (FlushLogArgs args) =>
                    {
                        return(args.HttpProperties.Response.StatusCode >= 400);
                    }
                }
            });

            Logger logger = new Logger(url: $"/MyUrl/{statusCode}");

            logger.DataContainer.HttpProperties.SetResponse(new HttpResponse(new HttpResponse.CreateOptions
            {
                StatusCode = statusCode
            }));

            NotifyFlush.Notify(new[] { logger });

            Assert.AreEqual(expectedResult, args.Count == 1);
        }
Exemplo n.º 8
0
        public void FactoryIsNotNull()
        {
            CommonTestHelpers.ResetContext();

            ILoggerFactory factory = Logger.Factory;

            Assert.IsNotNull(factory);
        }
Exemplo n.º 9
0
        public void NotifyOnBeginRequestExceptionIsSilentlySwallowed()
        {
            CommonTestHelpers.ResetContext();

            KissLogConfiguration.Listeners.Add(new CustomLogListener(onBeginRequest: (HttpRequest arg) => { throw new Exception(); }));

            Logger logger = new Logger(url: "App/Method1");
        }
Exemplo n.º 10
0
        public void NotifyListenersExceptionIsSilentlySwallowed()
        {
            CommonTestHelpers.ResetContext();

            KissLogConfiguration.Listeners.Add(new CustomLogListener(onFlush: (FlushLogArgs arg) => { throw new Exception(); }));

            Logger logger = new Logger();

            Logger.NotifyListeners(logger);
        }
Exemplo n.º 11
0
        public void NotifyMessageExceptionIsSilentlySwallowed()
        {
            CommonTestHelpers.ResetContext();

            KissLogConfiguration.Listeners.Add(new CustomLogListener(onMessage: (LogMessage arg) => { throw new Exception(); }));

            Logger logger = new Logger();

            logger.Log(LogLevel.Trace, "Message");
        }
Exemplo n.º 12
0
        public void ExceptionIsSilentlySwallowed()
        {
            CommonTestHelpers.ResetContext();

            KissLogConfiguration.InternalLog = (message) =>
            {
                throw new Exception();
            };

            InternalLogger.LogException(new Exception());
        }
Exemplo n.º 13
0
        public void NotifyOnBeginRequestContainsTheLoggerUrl()
        {
            CommonTestHelpers.ResetContext();

            HttpRequest httpRequestArgs = null;

            KissLogConfiguration.Listeners.Add(new CustomLogListener(onBeginRequest: (HttpRequest arg) => { httpRequestArgs = arg; }));

            Logger logger = new Logger(url: "/App/Method1");

            Assert.AreEqual(httpRequestArgs.Url.LocalPath, "/App/Method1");
        }
Exemplo n.º 14
0
        public void EvaluatesOptionsShouldLogInputStream()
        {
            CommonTestHelpers.ResetContext();

            KissLogConfiguration.Options.ShouldLogInputStream((OptionsArgs.LogListenerInputStreamArgs args) => false);

            FlushLogArgs flushLogArgs = CommonTestHelpers.Factory.CreateFlushLogArgs();

            FlushLogArgs result = NotifyFlush.CreateArgsForListener(flushLogArgs, new CustomLogListener());

            Assert.IsNull(result.HttpProperties.Request.Properties.InputStream);
            Assert.IsNotNull(flushLogArgs.HttpProperties.Request.Properties.InputStream);
        }
Exemplo n.º 15
0
        public void EvaluatesOptionsShouldLogClaim()
        {
            CommonTestHelpers.ResetContext();

            KissLogConfiguration.Options.ShouldLogClaim((OptionsArgs.LogListenerClaimArgs args) => false);

            FlushLogArgs flushLogArgs = CommonTestHelpers.Factory.CreateFlushLogArgs();

            FlushLogArgs result = NotifyFlush.CreateArgsForListener(flushLogArgs, new CustomLogListener());

            Assert.AreEqual(0, result.HttpProperties.Request.Properties.Claims.Count());
            Assert.AreNotEqual(0, flushLogArgs.HttpProperties.Request.Properties.Claims.Count());
        }
Exemplo n.º 16
0
        public void NotifyOnBeginRequestIsNotInvokedForLoggersWithoutUrl()
        {
            CommonTestHelpers.ResetContext();

            List <HttpRequest> onBeginRequestArgs = new List <HttpRequest>();

            KissLogConfiguration.Listeners.Add(new CustomLogListener(onBeginRequest: (HttpRequest arg) => { onBeginRequestArgs.Add(arg); }));

            Logger logger1 = new Logger();
            Logger logger2 = new Logger();

            Assert.AreEqual(0, onBeginRequestArgs.Count);
        }
Exemplo n.º 17
0
        public void NotifyMessageIsInvoked()
        {
            CommonTestHelpers.ResetContext();

            List <LogMessage> messageArgs = new List <LogMessage>();

            KissLogConfiguration.Listeners.Add(new CustomLogListener(onMessage: (LogMessage arg) => { messageArgs.Add(arg); }));

            Logger logger = new Logger();

            logger.Log(LogLevel.Trace, "Message");

            Assert.AreEqual(1, messageArgs.Count);
        }
Exemplo n.º 18
0
        public void NotifyContinuesForOtherListenersWhenOneThrowsAnException()
        {
            CommonTestHelpers.ResetContext();

            List <FlushLogArgs> flushArgs = new List <FlushLogArgs>();

            KissLogConfiguration.Listeners.Add(new CustomLogListener(onFlush: (FlushLogArgs arg) => { throw new Exception(); }));
            KissLogConfiguration.Listeners.Add(new CustomLogListener(onFlush: (FlushLogArgs arg) => { flushArgs.Add(arg); }));
            KissLogConfiguration.Listeners.Add(new CustomLogListener(onFlush: (FlushLogArgs arg) => { flushArgs.Add(arg); }));

            Logger logger = new Logger();

            NotifyFlush.Notify(new[] { logger });

            Assert.AreEqual(2, flushArgs.Count);
        }
Exemplo n.º 19
0
        public void NotifyIsInvokedForEachLogListener()
        {
            CommonTestHelpers.ResetContext();

            List <FlushLogArgs> flushArgs = new List <FlushLogArgs>();

            KissLogConfiguration.Listeners.Add(new CustomLogListener(onFlush: (FlushLogArgs arg) => { flushArgs.Add(arg); }));
            KissLogConfiguration.Listeners.Add(new CustomLogListener(onFlush: (FlushLogArgs arg) => { flushArgs.Add(arg); }));
            KissLogConfiguration.Listeners.Add(new CustomLogListener(onFlush: (FlushLogArgs arg) => { flushArgs.Add(arg); }));

            Logger logger = new Logger();

            NotifyFlush.Notify(new[] { logger });

            Assert.AreEqual(3, flushArgs.Count);
        }
Exemplo n.º 20
0
        public void NotifyMessageIsInvokedForEachMessage()
        {
            CommonTestHelpers.ResetContext();

            List <LogMessage> messageArgs = new List <LogMessage>();

            KissLogConfiguration.Listeners.Add(new CustomLogListener(onMessage: (LogMessage arg) => { messageArgs.Add(arg); }));

            Logger logger = new Logger();

            logger.Log(LogLevel.Trace, "Message");
            logger.Log(LogLevel.Debug, Car.Dacia);
            logger.Log(LogLevel.Information, new Args("Message", 100, new Exception()));
            logger.Log(LogLevel.Warning, new Exception());

            Assert.AreEqual(4, messageArgs.Count);
        }
        public void AppendExceptionDetailsOptions()
        {
            CommonTestHelpers.ResetContext();

            KissLogConfiguration.Options.AppendExceptionDetails((Exception ex) =>
            {
                if (ex is NotImplementedException implementedException)
                {
                    return("We should implement this method");
                }

                return(null);
            });

            var    formatter = new ExceptionFormatter();
            string result    = formatter.Format(new NotImplementedException(), new Logger());

            Assert.IsTrue(result.Contains("We should implement this method"));
        }
        public void NotifyContinuesForOtherListenersWhenOneThrowsAnException()
        {
            CommonTestHelpers.ResetContext();

            List <HttpRequest> httpRequestArgs = new List <HttpRequest>();

            KissLogConfiguration.Listeners.Add(new CustomLogListener(onBeginRequest: (HttpRequest arg) => { throw new Exception(); }));
            KissLogConfiguration.Listeners.Add(new CustomLogListener(onBeginRequest: (HttpRequest arg) => { httpRequestArgs.Add(arg); }));
            KissLogConfiguration.Listeners.Add(new CustomLogListener(onBeginRequest: (HttpRequest arg) => { httpRequestArgs.Add(arg); }));

            HttpRequest httpRequest = new HttpRequest(new HttpRequest.CreateOptions
            {
                Url        = UrlParser.GenerateUri(null),
                HttpMethod = "GET"
            });

            NotifyBeginRequest.Notify(httpRequest);

            Assert.AreEqual(2, httpRequestArgs.Count);
        }
        public void NotifyIsInvokedForEachLogListener()
        {
            CommonTestHelpers.ResetContext();

            List <HttpRequest> httpRequestArgs = new List <HttpRequest>();

            KissLogConfiguration.Listeners.Add(new CustomLogListener(onBeginRequest: (HttpRequest arg) => { httpRequestArgs.Add(arg); }));
            KissLogConfiguration.Listeners.Add(new CustomLogListener(onBeginRequest: (HttpRequest arg) => { httpRequestArgs.Add(arg); }));
            KissLogConfiguration.Listeners.Add(new CustomLogListener(onBeginRequest: (HttpRequest arg) => { httpRequestArgs.Add(arg); }));

            HttpRequest httpRequest = new HttpRequest(new HttpRequest.CreateOptions
            {
                Url        = UrlParser.GenerateUri(null),
                HttpMethod = "GET"
            });

            NotifyBeginRequest.Notify(httpRequest);

            Assert.AreEqual(3, httpRequestArgs.Count);
        }
Exemplo n.º 24
0
        public void NotifyIsInvokedForEachLogListener()
        {
            CommonTestHelpers.ResetContext();

            List <LogMessage> messageArgs = new List <LogMessage>();

            KissLogConfiguration.Listeners.Add(new CustomLogListener(onMessage: (LogMessage arg) => { messageArgs.Add(arg); }));
            KissLogConfiguration.Listeners.Add(new CustomLogListener(onMessage: (LogMessage arg) => { messageArgs.Add(arg); }));
            KissLogConfiguration.Listeners.Add(new CustomLogListener(onMessage: (LogMessage arg) => { messageArgs.Add(arg); }));

            LogMessage message = new LogMessage(new LogMessage.CreateOptions
            {
                CategoryName = Constants.DefaultLoggerCategoryName,
                LogLevel     = LogLevel.Trace,
                Message      = "Message"
            });

            NotifyOnMessage.Notify(message);

            Assert.AreEqual(3, messageArgs.Count);
        }
Exemplo n.º 25
0
        public void NotifyContinuesForOtherListenersWhenOneThrowsAnException()
        {
            CommonTestHelpers.ResetContext();

            List <LogMessage> messageArgs = new List <LogMessage>();

            KissLogConfiguration.Listeners.Add(new CustomLogListener(onMessage: (LogMessage arg) => { throw new Exception(); }));
            KissLogConfiguration.Listeners.Add(new CustomLogListener(onMessage: (LogMessage arg) => { messageArgs.Add(arg); }));
            KissLogConfiguration.Listeners.Add(new CustomLogListener(onMessage: (LogMessage arg) => { messageArgs.Add(arg); }));

            LogMessage message = new LogMessage(new LogMessage.CreateOptions
            {
                CategoryName = Constants.DefaultLoggerCategoryName,
                LogLevel     = LogLevel.Trace,
                Message      = "Message"
            });

            NotifyOnMessage.Notify(message);

            Assert.AreEqual(2, messageArgs.Count);
        }