DetailForm.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System;
  2. using System.ComponentModel;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Text;
  6. using System.Windows.Forms;
  7. using VCommon.Logging.Viewer.Utility;
  8. namespace VCommon.Logging.Viewer
  9. {
  10. public partial class DetailForm : Form
  11. {
  12. private static readonly string WebBrowserHtml;
  13. private LogEntity LogEntity { get; }
  14. private readonly Action<DetailForm> _closingRemove;
  15. static DetailForm()
  16. {
  17. WebBrowserHtml = WebBrowserUtility.LoadWebBrowserResource();
  18. }
  19. public DetailForm()
  20. {
  21. InitializeComponent();
  22. WbJson.DocumentText = WebBrowserHtml;
  23. }
  24. internal DetailForm(LogEntity logEntity, Action<DetailForm> closingRemove) : this()
  25. {
  26. LogEntity = logEntity;
  27. _closingRemove = closingRemove;
  28. StringBuilder sbSummary = new StringBuilder();
  29. PropertyInfo[] propertyInfos = LogEntity.GetType()
  30. .GetProperties(BindingFlags.Instance | BindingFlags.Public);
  31. var descriptFileNameLength = 0;
  32. var fieldCount = 0;
  33. foreach (var propertyInfo in propertyInfos.Where(c => c.Name != nameof(LogEntity.Detail)))
  34. {
  35. var desc = propertyInfo.GetCustomAttributes<DescriptionAttribute>().FirstOrDefault()?.Description ?? propertyInfo.Name;
  36. //TODO: 精简详情头部文本格式化逻辑
  37. sbSummary.Append(
  38. $"{desc}:" +
  39. $"{propertyInfo.GetValue(LogEntity, null)}\t");
  40. if (++fieldCount == 3)
  41. {
  42. sbSummary.Append(Environment.NewLine);
  43. }
  44. if (fieldCount == 4)
  45. {
  46. var padLeftLength = logEntity.FileName.Length + descriptFileNameLength + 1 - (logEntity.ThreadName.Length + desc.Length) - 8;
  47. if (padLeftLength > 0)
  48. {
  49. sbSummary.Append(new string(' ', padLeftLength) + "\t");
  50. }
  51. }
  52. if (propertyInfo.Name == nameof(logEntity.FileName))
  53. {
  54. descriptFileNameLength = desc.Length;
  55. }
  56. }
  57. txtDetail.Text = sbSummary.ToString();
  58. txtDetail.Select(logEntity.FileName.Length + 1 + descriptFileNameLength, 0);
  59. Application.DoEvents();
  60. }
  61. private void DetailForm_Shown(object sender, EventArgs e)
  62. {
  63. Application.DoEvents();
  64. BackgroundWorker bgw = new BackgroundWorker();
  65. bgw.RunWorkerCompleted += Bgw_RunWorkerCompleted;
  66. TaskExtend.Delay(50).ContinueWith(t =>
  67. {
  68. bgw.RunWorkerAsync();
  69. });
  70. }
  71. private void Bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  72. {
  73. Application.DoEvents();
  74. Invoke(new Action(() =>
  75. {
  76. // ReSharper disable once PossibleNullReferenceException
  77. WbJson.Document.InvokeScript("GetOutputResult", new object[] { LogEntity.Detail });
  78. }));
  79. }
  80. public void PreFilterMessage(ref Message m)
  81. {
  82. WebBrowserUtility.SendMouseWheel(m, WbJson);
  83. }
  84. private void DetailForm_FormClosing(object sender, FormClosingEventArgs e) => _closingRemove?.Invoke(this);
  85. protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
  86. {
  87. int WM_KEYDOWN = 256;
  88. int WM_SYSKEYDOWN = 260;
  89. if (msg.Msg == WM_KEYDOWN | msg.Msg == WM_SYSKEYDOWN)
  90. {
  91. switch (keyData)
  92. {
  93. //esc关闭窗体
  94. case Keys.Escape:
  95. Close();
  96. break;
  97. }
  98. }
  99. return false;
  100. }
  101. }
  102. }