Format() public static method

Apply upper or lower casing to value when format is provided. Returns value when no or invalid format provided
public static Format ( string value, string format = null ) : string
value string
format string
return string
コード例 #1
0
        /// <summary>
        /// This method will apply only upper or lower case formatting, not fixed width
        /// </summary>
        public override void Render(TextWriter output, string format = null, IFormatProvider formatProvider = null)
        {
            if (format != null && (format.Length == 2 || format.Length == 3))
            {
                // Using int.Parse() here requires allocating a string to exclude the first character prefix.
                // Junk like "wxy" will be accepted but produce benign results.
                var width = format[1] - '0';
                if (format.Length == 3)
                {
                    width *= 10;
                    width += format[2] - '0';
                }

                if (width < 1)
                {
                    return;
                }

                if (width > 4)
                {
                    var value = _value.ToString();
                    if (value.Length > width)
                    {
                        value = value.Substring(0, width);
                    }
                    output.Write(Casing.Format(value));
                    return;
                }

                var index = (int)_value;
                if (index >= 0 && index <= (int)LogEventLevel.Fatal)
                {
                    switch (format[0])
                    {
                    case 'w':
                        output.Write(_lowercaseLevelMap[index][width - 1]);
                        return;

                    case 'u':
                        output.Write(_uppercaseLevelMap[index][width - 1]);
                        return;

                    case 't':
                        output.Write(_titleCaseLevelMap[index][width - 1]);
                        return;
                    }
                }
            }

            output.Write(Casing.Format(_value.ToString(), format));
        }
コード例 #2
0
        public static string GetLevelMoniker(LogEventLevel value, string format = null)
        {
            if (format == null || format.Length != 2 && format.Length != 3)
            {
                return(Casing.Format(value.ToString(), format));
            }

            // Using int.Parse() here requires allocating a string to exclude the first character prefix.
            // Junk like "wxy" will be accepted but produce benign results.
            var width = format[1] - '0';

            if (format.Length == 3)
            {
                width *= 10;
                width += format[2] - '0';
            }

            if (width < 1)
            {
                return(string.Empty);
            }

            if (width > 4)
            {
                var stringValue = value.ToString();
                if (stringValue.Length > width)
                {
                    stringValue = stringValue.Substring(0, width);
                }
                return(Casing.Format(stringValue));
            }

            var index = (int)value;

            if (index >= 0 && index <= (int)LogEventLevel.Fatal)
            {
                switch (format[0])
                {
                case 'w':
                    return(_lowercaseLevelMap[index][width - 1]);

                case 'u':
                    return(_uppercaseLevelMap[index][width - 1]);

                case 't':
                    return(_titleCaseLevelMap[index][width - 1]);
                }
            }

            return(Casing.Format(value.ToString(), format));
        }
コード例 #3
0
        public static string GetLevelMoniker(LogEventLevel value, string format = null)
        {
            var index = (int)value;

            if (index < 0 || index > (int)LogEventLevel.Fatal)
            {
                return(Casing.Format(value.ToString(), format));
            }

            if (format == null || format.Length != 2 && format.Length != 3)
            {
                return(Casing.Format(GetLevelMoniker(_titleCaseLevelMap, index), format));
            }

            // Using int.Parse() here requires allocating a string to exclude the first character prefix.
            // Junk like "wxy" will be accepted but produce benign results.
            var width = format[1] - '0';

            if (format.Length == 3)
            {
                width *= 10;
                width += format[2] - '0';
            }

            if (width < 1)
            {
                return(string.Empty);
            }

            switch (format[0])
            {
            case 'w':
                return(GetLevelMoniker(_lowerCaseLevelMap, index, width));

            case 'u':
                return(GetLevelMoniker(_upperCaseLevelMap, index, width));

            case 't':
                return(GetLevelMoniker(_titleCaseLevelMap, index, width));

            default:
                return(Casing.Format(GetLevelMoniker(_titleCaseLevelMap, index), format));
            }
        }
        /// <summary>
        /// Format the log event into the output.
        /// </summary>
        /// <param name="logEvent">The event to format.</param>
        /// <param name="output">The output.</param>
        public void Format(LogEvent logEvent, TextWriter output)
        {
            if (logEvent == null)
            {
                throw new ArgumentNullException(nameof(logEvent));
            }
            if (output == null)
            {
                throw new ArgumentNullException(nameof(output));
            }

            foreach (var token in _outputTemplate.Tokens)
            {
                if (token is TextToken tt)
                {
                    MessageTemplateRenderer.RenderTextToken(tt, output);
                    continue;
                }

                var pt = (PropertyToken)token;
                if (pt.PropertyName == OutputProperties.LevelPropertyName)
                {
                    var moniker = LevelOutputFormat.GetLevelMoniker(logEvent.Level, pt.Format);
                    Padding.Apply(output, moniker, pt.Alignment);
                }
                else if (pt.PropertyName == OutputProperties.NewLinePropertyName)
                {
                    Padding.Apply(output, Environment.NewLine, pt.Alignment);
                }
                else if (pt.PropertyName == OutputProperties.ExceptionPropertyName)
                {
                    var exception = logEvent.Exception == null ? "" : logEvent.Exception + Environment.NewLine;
                    Padding.Apply(output, exception, pt.Alignment);
                }
                else
                {
                    // In this block, `writer` may be used to buffer output so that
                    // padding can be applied.
                    var writer = pt.Alignment.HasValue ? new StringWriter() : output;

                    if (pt.PropertyName == OutputProperties.MessagePropertyName)
                    {
                        MessageTemplateRenderer.Render(logEvent.MessageTemplate, logEvent.Properties, writer, pt.Format, _formatProvider);
                    }
                    else if (pt.PropertyName == OutputProperties.TimestampPropertyName)
                    {
                        ScalarValue.Render(logEvent.Timestamp, writer, pt.Format, _formatProvider);
                    }
                    else if (pt.PropertyName == OutputProperties.PropertiesPropertyName)
                    {
                        PropertiesOutputFormat.Render(logEvent.MessageTemplate, logEvent.Properties, _outputTemplate, writer, pt.Format, _formatProvider);
                    }
                    else
                    {
                        // If a property is missing, don't render anything (message templates render the raw token here).
                        if (!logEvent.Properties.TryGetValue(pt.PropertyName, out var propertyValue))
                        {
                            continue;
                        }

                        // If the value is a scalar string, support some additional formats: 'u' for uppercase
                        // and 'w' for lowercase.
                        var sv = propertyValue as ScalarValue;
                        if (sv?.Value is string literalString)
                        {
                            var cased = Casing.Format(literalString, pt.Format);
                            writer.Write(cased);
                        }
                        else
                        {
                            propertyValue.Render(writer, pt.Format, _formatProvider);
                        }
                    }

                    if (pt.Alignment.HasValue)
                    {
                        Padding.Apply(output, ((StringWriter)writer).ToString(), pt.Alignment);
                    }
                }
            }
        }
コード例 #5
0
 public override void Render(TextWriter output, string format = null, IFormatProvider formatProvider = null)
 {
     output.Write(Casing.Format(_value, format));
 }