Exemplo n.º 1
0
        public void When_arguments_are_escaped_Then_should_not_replace_them()
        {
            Func <string> messageBuilder = () => "This is an {argument} and this an {{escaped_argument}}.";

            var formattedMessage = LogMessageFormatter.SimulateStructuredLogging(messageBuilder, new object[] { "arg0", "arg1" })();

            formattedMessage.ShouldBe("This is an arg0 and this an {escaped_argument}.");
        }
Exemplo n.º 2
0
        public void When_arguments_are_unique_and_not_escaped_Then_should_replace_them()
        {
            Func <string> messageBuilder = () => "This is an {1argument} and this another {argument2} and a last one {2}.";

            var formattedMessage = LogMessageFormatter.SimulateStructuredLogging(messageBuilder, new object[] { "arg0", "arg1", "arg2" })();

            formattedMessage.ShouldBe("This is an arg0 and this another arg1 and a last one arg2.");
        }
Exemplo n.º 3
0
        public void When_argument_is_multiple_time_Then_should_be_replaced_with_same_value()
        {
            var           date           = DateTime.Today;
            Func <string> messageBuilder = () => "{date:yyyy-MM-dd} {argument1} {date:yyyy}";

            var formattedMessage = LogMessageFormatter.SimulateStructuredLogging(messageBuilder, new object[] { date, "arg0" })();

            formattedMessage.ShouldBe(
                string.Format(CultureInfo.InvariantCulture, "{0:yyyy-MM-dd} {1} {0:yyyy}", date, "arg0"));
        }
Exemplo n.º 4
0
        public void When_argument_has_format_Then_should_preserve_format()
        {
            var           date           = DateTime.Today;
            Func <string> messageBuilder = () => "Formatted {date1:yyyy-MM-dd} and not formatted {date2}.";

            var formattedMessage = LogMessageFormatter.SimulateStructuredLogging(messageBuilder, new object[] { date, date })();

            formattedMessage.ShouldBe(
                string.Format(CultureInfo.InvariantCulture, "Formatted {0:yyyy-MM-dd} and not formatted {1}.", date, date));
        }
Exemplo n.º 5
0
            public bool Log(MvxLogLevel logLevel, Func <string> messageFunc, Exception exception,
                            params object[] formatParameters)
            {
                if (messageFunc == null)
                {
                    return(true);
                }

                messageFunc = LogMessageFormatter.SimulateStructuredLogging(messageFunc, formatParameters);

                Write(logLevel, messageFunc(), exception);
                return(true);
            }
Exemplo n.º 6
0
            public bool Log(LogLevel logLevel, Func <string> messageFunc, Exception exception, params object[] formatParameters)
            {
                if (messageFunc == null)
                {
                    return(IsLogLevelEnable(logLevel));
                }
                messageFunc = LogMessageFormatter.SimulateStructuredLogging(messageFunc, formatParameters);

                if (exception != null)
                {
                    return(LogException(logLevel, messageFunc, exception));
                }
                switch (logLevel)
                {
                case LogLevel.Debug:
                    if (logger.IsDebugEnabled)
                    {
                        logger.Debug(messageFunc());
                        return(true);
                    }
                    break;

                case LogLevel.Info:
                    if (logger.IsInfoEnabled)
                    {
                        logger.Info(messageFunc());
                        return(true);
                    }
                    break;

                case LogLevel.Warn:
                    if (logger.IsWarnEnabled)
                    {
                        logger.Warn(messageFunc());
                        return(true);
                    }
                    break;

                case LogLevel.Error:
                    if (logger.IsErrorEnabled)
                    {
                        logger.Error(messageFunc());
                        return(true);
                    }
                    break;

                case LogLevel.Fatal:
                    if (logger.IsFatalEnabled)
                    {
                        logger.Fatal(messageFunc());
                        return(true);
                    }
                    break;

                default:
                    if (logger.IsTraceEnabled)
                    {
                        logger.Trace(messageFunc());
                        return(true);
                    }
                    break;
                }
                return(false);
            }