Beispiel #1
0
        public void Log(LogLevel logLevel, string message, string memberName = null, int lineNumber = 0, string memberType = null)
        {
            if (string.IsNullOrWhiteSpace(message))
            {
                return;
            }

            LogMessage logMessage = new LogMessage(new LogMessage.CreateOptions
            {
                CategoryName = CategoryName,
                LogLevel     = logLevel,
                Message      = message,
                MemberType   = memberType,
                MemberName   = memberName,
                LineNumber   = lineNumber
            });

            DataContainer.Add(logMessage);

            Guid?httpRequestId = DataContainer.HttpProperties == null ? (Guid?)null : DataContainer.HttpProperties.Request.Id;

            InternalHelpers.WrapInTryCatch(() =>
            {
                NotifyOnMessage.Notify(logMessage, httpRequestId);
            });
        }
        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 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);
        }
        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);
        }
 public void ThrowsExceptionWhenLogMessageIsNull()
 {
     NotifyOnMessage.Notify(null);
 }