Beispiel #1
0
        public void FormatReturnsValueWithPadding(int scopeLevel)
        {
            var config       = new LoggingConfig();
            var padding      = new string(' ', config.ScopePaddingSpaces *scopeLevel);
            var categoryName = Guid.NewGuid().ToString();
            var logLevel     = LogLevel.Information;
            var eventId      = Model.Create <EventId>();
            var message      = Guid.NewGuid().ToString();
            var exception    = new ArgumentNullException(Guid.NewGuid().ToString(), Guid.NewGuid().ToString());

            var sut = new DefaultFormatter(config);

            var actual = sut.Format(scopeLevel, categoryName, logLevel, eventId, message, exception);

            _output.WriteLine(actual);

            if (scopeLevel > 0)
            {
                actual.Should().StartWith(padding);
            }
            else
            {
                actual.Should().NotStartWith(" ");
            }
        }
Beispiel #2
0
        public void FormatHidesSensitiveDataInException(string?sensitiveValue, string message, string expected)
        {
            var config       = new LoggingConfig();
            var scopeLevel   = 1;
            var categoryName = Guid.NewGuid().ToString();
            var logLevel     = LogLevel.Information;
            var eventId      = Model.Create <EventId>();
            var logMessage   = Guid.NewGuid().ToString();
            var exception    = new InvalidOperationException(message);

            if (sensitiveValue != null)
            {
                config.SensitiveValues.Add(sensitiveValue);
            }

            var sut = new DefaultFormatter(config);

            var actual = sut.Format(scopeLevel, categoryName, logLevel, eventId, logMessage, exception);

            actual.Should().Contain(expected);

            if (sensitiveValue != null)
            {
                actual.Should().NotContain(sensitiveValue);
            }
        }
        /// <summary>
        /// Pick the best available formatter to format the given piece of code.
        /// </summary>
        /// <param name="code">The code to be formatted. This parameter cannot be null.</param>
        /// <param name="language">
        /// The language into which code has been written. Ex: "C#", "Java".
        /// If no such formatter is available, a default formatting is applied.
        /// This parameter cannot be null.
        /// </param>
        /// <returns>
        /// The formatting for this piece of code.
        /// </returns>
        public FormattedCode Format(string code, string language)
        {
            UiExceptionHelper.CheckNotNull(code, "code");
            UiExceptionHelper.CheckNotNull(language, "language");

            if (_formatters.HasLanguage(language))
            {
                return(_formatters[language].Format(code));
            }

            return(DefaultFormatter.Format(code));
        }
        /// <summary>
        /// A convenient method to make the formatting of a piece of code when
        /// only the file extension is known.
        /// </summary>
        /// <param name="code">The piece of code to be formatted. This parameter
        /// cannot be null.</param>
        /// <param name="extension">The file extension associated to this piece of code.
        /// Ex: "cs", "cpp". This is used to pick the formatter assigned to. If no such
        /// formatter exists, the default one is picked up.</param>
        /// <returns>The FormattedCode for this piece of code.</returns>
        public FormattedCode FormatFromExtension(string code, string extension)
        {
            UiExceptionHelper.CheckNotNull(code, "code");
            UiExceptionHelper.CheckNotNull(extension, "extension");

            if (_formatters.HasExtension(extension))
            {
                return(_formatters.GetFromExtension(extension).Format(code));
            }

            return(DefaultFormatter.Format(code));
        }
Beispiel #5
0
        public void FormatReturnsEmptyWhenMessageIsNullEmptyOrWhiteSpace(string message)
        {
            var config       = new LoggingConfig().Set(x => x.ScopePaddingSpaces = 2);
            var scopeLevel   = 1;
            var categoryName = Guid.NewGuid().ToString();
            var logLevel     = LogLevel.Information;
            var eventId      = Model.Create <EventId>();

            var sut = new DefaultFormatter(config);

            var actual = sut.Format(scopeLevel, categoryName, logLevel, eventId, message, null);

            actual.Should().BeEmpty();
        }
Beispiel #6
0
        public void FormatReturnsValueWithLogLevel(LogLevel logLevel)
        {
            var config       = new LoggingConfig();
            var scopeLevel   = 1;
            var eventId      = Model.Create <EventId>();
            var message      = Guid.NewGuid().ToString();
            var categoryName = Guid.NewGuid().ToString();

            var sut = new DefaultFormatter(config);

            var actual = sut.Format(scopeLevel, categoryName, logLevel, eventId, message, null);

            _output.WriteLine(actual);

            actual.Should().Contain(logLevel.ToString());
        }
        protected virtual string Format(object value, string format, string language)
        {
            if (_formatters != null)
            {
                for (var i = 0; i < _formatters.Count; i++)
                {
                    var formatter = _formatters[i];

                    if (formatter.CanFormat(value, format, language))
                    {
                        return(formatter.Format(value, format, language));
                    }
                }
            }

            return(DefaultFormatter.Format(value, format, language));
        }
Beispiel #8
0
        public void FormatReturnsValueWithoutException()
        {
            var config     = new LoggingConfig();
            var scopeLevel = 1;
            var name       = Guid.NewGuid().ToString();
            var logLevel   = LogLevel.Information;
            var eventId    = Model.Create <EventId>();
            var message    = Guid.NewGuid().ToString();

            var sut = new DefaultFormatter(config);

            var actual = sut.Format(scopeLevel, name, logLevel, eventId, message, null);

            _output.WriteLine(actual);

            actual.Should().NotContain("Exception");
        }
Beispiel #9
0
        public void FormatReturnsValueWithoutName()
        {
            var config       = new LoggingConfig();
            var scopeLevel   = 1;
            var categoryName = Guid.NewGuid().ToString();
            var logLevel     = LogLevel.Information;
            var eventId      = Model.Create <EventId>();
            var message      = Guid.NewGuid().ToString();
            var exception    = new ArgumentNullException(Guid.NewGuid().ToString(), Guid.NewGuid().ToString());

            var sut = new DefaultFormatter(config);

            var actual = sut.Format(scopeLevel, categoryName, logLevel, eventId, message, exception);

            _output.WriteLine(actual);

            actual.Should().NotContain(categoryName);
        }
Beispiel #10
0
        public void FormatHidesSensitiveDataInMessage(string?sensitiveValue, string message, string expected)
        {
            var config       = new LoggingConfig();
            var scopeLevel   = 1;
            var categoryName = Guid.NewGuid().ToString();
            var logLevel     = LogLevel.Information;
            var eventId      = Model.Create <EventId>();

            if (sensitiveValue != null)
            {
                config.SensitiveValues.Add(sensitiveValue);
            }

            var sut = new DefaultFormatter(config);

            var actual = sut.Format(scopeLevel, categoryName, logLevel, eventId, message, null);

            actual.Should().Be($"   Information [{eventId.Id}]: {expected}");
        }
Beispiel #11
0
        public void FormatIncludesNewLineBetweenMessageAndException(string message, bool exceptionExists)
        {
            var       config       = new LoggingConfig();
            var       scopeLevel   = 1;
            var       categoryName = Guid.NewGuid().ToString();
            var       logLevel     = LogLevel.Information;
            var       eventId      = Model.Create <EventId>();
            Exception?exception    = exceptionExists
                ? new ArgumentNullException(Guid.NewGuid().ToString(), Guid.NewGuid().ToString())
                : null;

            var sut = new DefaultFormatter(config);

            var actual = sut.Format(scopeLevel, categoryName, logLevel, eventId, message, exception);

            actual.Should().NotStartWith(Environment.NewLine);
            actual.Should().NotEndWith(Environment.NewLine);

            if (string.IsNullOrWhiteSpace(message))
            {
                if (exception != null)
                {
                    actual.Should().Be($"   Information [{eventId.Id}]: {exception}");
                }
                else
                {
                    actual.Should().BeEmpty();
                }
            }
            else if (exception != null)
            {
                actual.Should()
                .Be(
                    $"   Information [{eventId.Id}]: stuff{Environment.NewLine}   Information [{eventId.Id}]: {exception}");
            }
            else
            {
                actual.Should().Be($"   Information [{eventId.Id}]: stuff");
            }
        }
Beispiel #12
0
 public string Format()
 => DefaultFormatter.Format(this);
Beispiel #13
0
 private string GetExpectedMessage(MessageType messageType, string tag, string message)
 {
     return(_formatter.Format(messageType, tag, message));
 }