Пример #1
0
        public void Should_respect_case_sensitivity_in_templates()
        {
            var event1 = new LogEvent(LogLevel.Info, DateTimeOffset.Now, "Hello, {User}!").WithProperty("User", "Kontur");
            var event2 = new LogEvent(LogLevel.Info, DateTimeOffset.Now, "HELLO, {User}!").WithProperty("User", "Kontur");

            LogMessageFormatter.Format(event1).Should().Be("Hello, Kontur!");
            LogMessageFormatter.Format(event2).Should().Be("HELLO, Kontur!");
        }
Пример #2
0
        public void Should_correctly_format_log_event_without_properties_and_produce_repeatable_results()
        {
            var @event = new LogEvent(LogLevel.Info, DateTimeOffset.Now, "Hello!")
                         .WithProperty("User", "Kontur")
                         .WithProperty("@User_Id", 100500)
                         .WithProperty("Unread.Count", 50);

            for (var i = 0; i < 10; i++)
            {
                LogMessageFormatter.Format(@event).Should().Be("Hello!");
            }
        }
Пример #3
0
        public void Should_correctly_format_log_event_message_using_its_properties_and_produce_repeatable_results()
        {
            var @event = new LogEvent(LogLevel.Info, DateTimeOffset.Now, "Hello, {User} {@User_Id}! You have {Unread.Count:D5} messages to read.")
                         .WithProperty("User", "Kontur")
                         .WithProperty("@User_Id", 100500)
                         .WithProperty("Unread.Count", 50);

            for (var i = 0; i < 10; i++)
            {
                LogMessageFormatter.Format(@event).Should().Be("Hello, Kontur 100500! You have 00050 messages to read.");
            }
        }
Пример #4
0
        public static LoggingEvent TranslateEvent([NotNull] ILogger logger, [NotNull] LogEvent @event, bool useVostokTemplate = false)
        {
            var level   = TranslateLevel(@event.Level);
            var message = useVostokTemplate
                ? LogEventFormatter.Format(@event, Template)
                : LogMessageFormatter.Format(@event);
            var timestamp = @event.Timestamp.UtcDateTime;

            var loggingEvent = new LoggingEvent(typeof(Logger), logger.Repository, logger.Name, level, message, @event.Exception);

            FillProperties(loggingEvent, @event.Properties);

            // (iloktionov): Unfortunately, log4net's LoggingEvent does not have a constructor that allows to pass both structured exception and timestamp.
            // (iloktionov): Constructor with Exception parameter just uses DateTime.UtcNow for timestamp.
            // (iloktionov): Constructor with LoggingEventData only allows to pass exception string instead of exception object.
            // (iloktionov): So we take the first ctor and use some dirty expressions to set timestamp in private LoggingEventData instance.

            timestampSetter?.Invoke(loggingEvent, timestamp);

            return(loggingEvent);
        }
Пример #5
0
        public void Log(LogEvent @event)
        {
            if (@event is null)
            {
                return;
            }

            if (!IsEnabledFor(@event.Level))
            {
                return;
            }

            var logLevel = ConvertLogLevel(@event.Level);

            var message = settings.UseVostokTemplate
                ? LogEventFormatter.Format(@event, Template)
                : LogMessageFormatter.Format(@event);

            var state = new FormattedLogValues(message, Array.Empty <object>());

            logger.Log(logLevel, NullEventId, state, @event.Exception, MessageFormatter);
        }
Пример #6
0
        public static IHerculesTagsBuilder AddLogEventData(
            this IHerculesEventBuilder builder,
            LogEvent @event,
            IReadOnlyCollection <string> filteredProperties,
            IFormatProvider formatProvider)
        {
            builder
            .SetTimestamp(@event.Timestamp)
            .AddValue(LogEventTagNames.UtcOffset, @event.Timestamp.Offset.Ticks)
            .AddValue(LogEventTagNames.Level, @event.Level.ToString())
            .AddValue(LogEventTagNames.Message, LogMessageFormatter.Format(@event, formatProvider));

            if (@event.MessageTemplate != null)
            {
                builder.AddValue(LogEventTagNames.MessageTemplate, @event.MessageTemplate);
            }

            if (@event.Exception != null)
            {
                builder.AddContainer(
                    LogEventTagNames.Exception,
                    tagsBuilder => tagsBuilder.AddExceptionData(@event.Exception));

                if (@event.Exception.StackTrace != null)
                {
                    builder.AddValue(LogEventTagNames.StackTrace, @event.Exception.ToString());
                }
            }

            if (@event.Properties != null)
            {
                builder.AddContainer(
                    LogEventTagNames.Properties,
                    tagsBuilder => tagsBuilder.AddProperties(@event, filteredProperties, formatProvider));
            }

            return(builder);
        }
Пример #7
0
        public void Log(LogEvent @event)
        {
            if (@event is null)
            {
                return;
            }

            var logLevel = TranslateLogLevel(@event.Level);

            if (!IsEnabledFor(logLevel))
            {
                return;
            }

            logger.Log(logLevel, NullEventId, new VostokLogEventWrapper(@event), @event.Exception, FormatMessage);

            string FormatMessage(VostokLogEventWrapper logEventWrapper, Exception ex)
            {
                return(settings.UseVostokTemplate
                    ? LogEventFormatter.Format(logEventWrapper.LogEvent, Template)
                    : LogMessageFormatter.Format(logEventWrapper.LogEvent));
            }
        }
Пример #8
0
        public void Should_support_null_message_template()
        {
            var event1 = new LogEvent(LogLevel.Info, DateTimeOffset.Now, null);

            LogMessageFormatter.Format(event1).Should().BeEmpty();
        }
Пример #9
0
 public override void Render(LogEvent @event, TextWriter writer, IFormatProvider formatProvider) =>
 LogMessageFormatter.Format(@event, writer, formatProvider);