LogLevelToColorConvertExtension.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using System;
  2. using System.Globalization;
  3. using System.Windows.Data;
  4. using System.Windows.Markup;
  5. using Brushes = System.Windows.Media.Brushes;
  6. namespace Flyinsono.Client.Test.Extensions
  7. {
  8. public class LogLevelToColorConvertExtension : MarkupExtension, IValueConverter
  9. {
  10. [ThreadStatic]
  11. private static LogLevelToColorConvertExtension _converter;
  12. public override object ProvideValue(IServiceProvider serviceProvider)
  13. {
  14. return _converter ?? (_converter = new LogLevelToColorConvertExtension());
  15. }
  16. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  17. {
  18. if (value != null)
  19. {
  20. if (Enum.TryParse(value.ToString(), out LogLevel level))
  21. {
  22. switch (level)
  23. {
  24. case LogLevel.Info:
  25. return Brushes.Blue;
  26. case LogLevel.Warn:
  27. return Brushes.Goldenrod;
  28. case LogLevel.Error:
  29. return Brushes.Red;
  30. }
  31. }
  32. }
  33. return Brushes.Black;
  34. }
  35. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  36. {
  37. throw new NotImplementedException();
  38. }
  39. }
  40. }