LogDetailWindow.xaml.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System;
  2. using System.Text;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Media;
  6. using System.Windows.Threading;
  7. using TeamAAS.Logging;
  8. namespace TeamAAS.Dialogs.Dialogs
  9. {
  10. /// <summary>
  11. /// 日志条目详情窗(由实时日志控件 LogView 双击弹出):
  12. /// 显示 类别 / 时间 / 来源(公共=[source],插件=[流程][任务])/ 插件详细度 / 完整内容,支持一键复制。
  13. /// </summary>
  14. public partial class LogDetailWindow
  15. {
  16. private readonly LogEntry _entry;
  17. public LogDetailWindow(LogEntry entry)
  18. {
  19. InitializeComponent();
  20. UiScaler.Attach(this); // 分辨率/DPI 自适应
  21. _entry = entry;
  22. Populate();
  23. }
  24. private void Populate()
  25. {
  26. var e = _entry;
  27. if (e == null)
  28. {
  29. MessageText.Text = "";
  30. return;
  31. }
  32. Title = "日志详情 - " + e.CategoryText;
  33. CategoryText.Text = e.CategoryText;
  34. CategoryBadge.Background = CategoryBrush(e.Category);
  35. TimeText.Text = e.Time.ToString("yyyy-MM-dd HH:mm:ss.fff");
  36. // 来源:公共日志显示 [ProductManager] 之类,插件显示 [流程][任务]
  37. SourceValue.Text = string.IsNullOrWhiteSpace(e.OriginText) ? "—" : e.OriginText;
  38. if (e.IsPlugin)
  39. {
  40. FlowRow.Visibility = Visibility.Visible;
  41. FlowValue.Text = e.Flow + " / " + e.Task;
  42. LevelRow.Visibility = Visibility.Visible;
  43. LevelValue.Text = e.Level + " 级(" + (e.Tier == 2 ? "常规" : "详细") + " · " + e.TierText + ")";
  44. }
  45. else
  46. {
  47. FlowRow.Visibility = Visibility.Collapsed;
  48. LevelRow.Visibility = Visibility.Collapsed;
  49. }
  50. MessageText.Text = e.Message;
  51. }
  52. private static Brush CategoryBrush(LogLevel category)
  53. {
  54. string hex;
  55. switch (category)
  56. {
  57. case LogLevel.Warning: hex = "#FF9800"; break;
  58. case LogLevel.Error: hex = "#F44336"; break;
  59. default: hex = "#2D6CDF"; break;
  60. }
  61. var brush = new SolidColorBrush((Color)ColorConverter.ConvertFromString(hex));
  62. brush.Freeze();
  63. return brush;
  64. }
  65. private string BuildFullText()
  66. {
  67. var e = _entry;
  68. if (e == null) return "";
  69. var sb = new StringBuilder();
  70. sb.Append("[类别] ").Append(e.CategoryText).AppendLine();
  71. sb.Append("[时间] ").Append(e.Time.ToString("yyyy-MM-dd HH:mm:ss.fff")).AppendLine();
  72. sb.Append("[来源] ").Append(string.IsNullOrWhiteSpace(e.OriginText) ? "—" : e.OriginText).AppendLine();
  73. if (e.IsPlugin)
  74. {
  75. sb.Append("[流程/任务] ").Append(e.Flow).Append(" / ").Append(e.Task).AppendLine();
  76. sb.Append("[详细度] ").Append(e.Level).Append(" 级 / ").Append(e.TierText).AppendLine();
  77. }
  78. sb.Append("[内容]").AppendLine();
  79. sb.Append(e.Message);
  80. return sb.ToString();
  81. }
  82. private void BtnCopyMessage_Click(object sender, RoutedEventArgs e)
  83. => Copy(_entry?.Message, BtnCopyMessage, "复制内容");
  84. private void BtnCopyAll_Click(object sender, RoutedEventArgs e)
  85. => Copy(BuildFullText(), BtnCopyAll, "复制全部");
  86. private static void Copy(string text, Button button, string restoreLabel)
  87. {
  88. if (string.IsNullOrEmpty(text)) return;
  89. try
  90. {
  91. Clipboard.SetText(text);
  92. button.Content = "已复制";
  93. var timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(1.5) };
  94. timer.Tick += (s, args) => { button.Content = restoreLabel; timer.Stop(); };
  95. timer.Start();
  96. }
  97. catch (Exception ex)
  98. {
  99. System.Diagnostics.Debug.WriteLine("[LogDetailWindow] copy failed: " + ex.Message);
  100. }
  101. }
  102. private void BtnClose_Click(object sender, RoutedEventArgs e) => Close();
  103. }
  104. }