public MessageTemplateOutputTokenRenderer(RichTextBoxTheme theme, PropertyToken token, IFormatProvider formatProvider)
        {
            _theme = theme ?? throw new ArgumentNullException(nameof(theme));
            _token = token ?? throw new ArgumentNullException(nameof(token));

            var isLiteral = false;
            var isJson    = false;

            if (token.Format != null)
            {
                // ReSharper disable once ForCanBeConvertedToForeach
                for (var i = 0; i < token.Format.Length; ++i)
                {
                    switch (token.Format[i])
                    {
                    case 'l':
                        isLiteral = true;
                        break;

                    case 'j':
                        isJson = true;
                        break;
                    }
                }
            }

            var valueFormatter = isJson
                ? (ThemedValueFormatter) new ThemedJsonValueFormatter(theme, formatProvider)
                : new ThemedDisplayValueFormatter(theme, formatProvider);

            _renderer = new ThemedMessageTemplateRenderer(theme, valueFormatter, isLiteral);
        }
Пример #2
0
 public ThemedMessageTemplateRenderer(RichTextBoxTheme theme, ThemedValueFormatter valueFormatter, bool isLiteral)
 {
     _theme                  = theme ?? throw new ArgumentNullException(nameof(theme));
     _valueFormatter         = valueFormatter;
     _isLiteral              = isLiteral;
     _unthemedValueFormatter = valueFormatter.SwitchTheme(_noTheme);
 }
Пример #3
0
        /// <summary>
        /// Writes log events to an <see cref="IRichTextBox"/> control, used only for unit-testing purposes.
        /// </summary>
        /// <param name="sinkConfiguration">Logger sink configuration.</param>
        /// <param name="richTextBox">The RichTextBox control to write to.</param>
        /// <param name="restrictedToMinimumLevel">The minimum level for
        /// events passed through the sink. Ignored when <paramref name="levelSwitch"/> is specified.</param>
        /// <param name="outputTemplate">A message template describing the format used to write to the sink.
        /// The default is <code>"[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}"</code>.</param>
        /// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
        /// <param name="levelSwitch">A switch allowing the pass-through minimum level
        /// to be changed at runtime.</param>
        /// <param name="theme">The theme to apply to the styled output. If not specified,
        /// uses <see cref="RichTextBoxConsoleTheme.Literate"/>.</param>
        /// <param name="dispatcherPriority">The priority at which messages will be sent to the UI thread when logging from a non-UI thread.</param>
        /// <param name="syncRoot">An object that will be used to `lock` (sync) access to the <see cref="IRichTextBox"/> instance. If you specify this, you
        /// will have the ability to lock on this object, and guarantee that the RichTextBox sink will not be about to output anything while
        /// the lock is held.</param>
        /// <returns>Configuration object allowing method chaining.</returns>
        /// <exception cref="ArgumentNullException">When <paramref name="sinkConfiguration"/> is <code>null</code></exception>
        /// <exception cref="ArgumentNullException">When <paramref name="outputTemplate"/> is <code>null</code></exception>
        internal static LoggerConfiguration RichTextBox(
            this LoggerSinkConfiguration sinkConfiguration,
            IRichTextBox richTextBox,
            LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
            string outputTemplate                 = _defaultRichTextBoxOutputTemplate,
            IFormatProvider formatProvider        = null,
            LoggingLevelSwitch levelSwitch        = null,
            RichTextBoxTheme theme                = null,
            DispatcherPriority dispatcherPriority = DispatcherPriority.Background,
            object syncRoot = null)
        {
            if (sinkConfiguration is null)
            {
                throw new ArgumentNullException(nameof(sinkConfiguration));
            }

            if (outputTemplate is null)
            {
                throw new ArgumentNullException(nameof(outputTemplate));
            }

            var appliedTheme = theme ?? RichTextBoxConsoleThemes.Literate;

            syncRoot ??= _defaultSyncRoot;

            var formatter = new XamlOutputTemplateRenderer(appliedTheme, outputTemplate, formatProvider);

            return(sinkConfiguration.Sink(new RichTextBoxSink(richTextBox, formatter, dispatcherPriority, syncRoot),
                                          restrictedToMinimumLevel, levelSwitch));
        }
        public XamlOutputTemplateRenderer(RichTextBoxTheme theme, string outputTemplate, IFormatProvider formatProvider)
        {
            if (outputTemplate is null)
            {
                throw new ArgumentNullException(nameof(outputTemplate));
            }

            var template = new MessageTemplateParser().Parse(outputTemplate);

            var renderers = new List <OutputTemplateTokenRenderer>();

            foreach (var token in template.Tokens)
            {
                if (token is TextToken tt)
                {
                    renderers.Add(new TextTokenRenderer(theme, tt.Text));
                    continue;
                }

                var pt = (PropertyToken)token;

                switch (pt.PropertyName)
                {
                case OutputProperties.LevelPropertyName:
                    renderers.Add(new LevelTokenRenderer(theme, pt));
                    break;

                case OutputProperties.NewLinePropertyName:
                    renderers.Add(new NewLineTokenRenderer(pt.Alignment));
                    break;

                case OutputProperties.ExceptionPropertyName:
                    renderers.Add(new ExceptionTokenRenderer(theme));
                    break;

                case OutputProperties.MessagePropertyName:
                    renderers.Add(new MessageTemplateOutputTokenRenderer(theme, pt, formatProvider));
                    break;

                case OutputProperties.TimestampPropertyName:
                    renderers.Add(new TimestampTokenRenderer(theme, pt, formatProvider));
                    break;

                case "Properties":
                    renderers.Add(new PropertiesTokenRenderer(theme, pt, template, formatProvider));
                    break;

                default:
                    renderers.Add(new EventPropertyTokenRenderer(theme, pt, formatProvider));
                    break;
                }
            }

            _renderers = renderers.ToArray();
        }
Пример #5
0
        private int RenderValue(RichTextBoxTheme theme, ThemedValueFormatter valueFormatter, LogEventPropertyValue propertyValue, TextWriter output, string format)
        {
            if (_isLiteral && propertyValue is ScalarValue {
                Value : string
            } sv)
            {
                var count = 0;
                using (theme.Apply(output, RichTextBoxThemeStyle.String, ref count))
                {
                    var text = SpecialCharsEscaping.Apply(sv.Value.ToString(), ref count);
                    output.Write(text);
                }

                return(count);
            }

            return(valueFormatter.Format(propertyValue, output, format, _isLiteral));
        }
Пример #6
0
        public PropertiesTokenRenderer(RichTextBoxTheme theme, PropertyToken token, MessageTemplate outputTemplate, IFormatProvider formatProvider)
        {
            _outputTemplate = outputTemplate;
            _theme          = theme ?? throw new ArgumentNullException(nameof(theme));
            _token          = token ?? throw new ArgumentNullException(nameof(token));

            var isJson = false;

            if (token.Format != null)
            {
                // ReSharper disable once ForCanBeConvertedToForeach
                for (var i = 0; i < token.Format.Length; ++i)
                {
                    if (token.Format[i] == 'j')
                    {
                        isJson = true;
                    }
                }
            }

            _valueFormatter = isJson
                ? (ThemedValueFormatter) new ThemedJsonValueFormatter(theme, formatProvider)
                : new ThemedDisplayValueFormatter(theme, formatProvider);
        }
Пример #7
0
 public EventPropertyTokenRenderer(RichTextBoxTheme theme, PropertyToken token, IFormatProvider formatProvider)
 {
     _theme          = theme;
     _token          = token;
     _formatProvider = formatProvider;
 }
Пример #8
0
 public ExceptionTokenRenderer(RichTextBoxTheme theme)
 {
     _theme = theme;
 }
Пример #9
0
 public override ThemedValueFormatter SwitchTheme(RichTextBoxTheme theme)
 {
     return new ThemedDisplayValueFormatter(theme, _formatProvider);
 }
Пример #10
0
 public ThemedDisplayValueFormatter(RichTextBoxTheme theme, IFormatProvider formatProvider)
     : base(theme)
 {
     _formatProvider = formatProvider;
 }
Пример #11
0
 public LevelTokenRenderer(RichTextBoxTheme theme, PropertyToken levelToken)
 {
     _theme      = theme;
     _levelToken = levelToken;
 }
 public TextTokenRenderer(RichTextBoxTheme theme, string text)
 {
     _theme = theme;
     _text = text;
 }
Пример #13
0
 public override ThemedValueFormatter SwitchTheme(RichTextBoxTheme theme)
 {
     return(new ThemedJsonValueFormatter(theme, _formatProvider));
 }
Пример #14
0
 public ThemedJsonValueFormatter(RichTextBoxTheme theme, IFormatProvider formatProvider)
     : base(theme)
 {
     _displayFormatter = new ThemedDisplayValueFormatter(theme, formatProvider);
     _formatProvider   = formatProvider;
 }
 public abstract ThemedValueFormatter SwitchTheme(RichTextBoxTheme theme);
 protected ThemedValueFormatter(RichTextBoxTheme theme)
 {
     _theme = theme ?? throw new ArgumentNullException(nameof(theme));
 }