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);
        }
        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);
        }
Пример #3
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);
        }
        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);
        }