QvcMainForm.Logs.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using QVCopier.Models;
  2. using System;
  3. using System.Linq;
  4. using System.Windows.Forms;
  5. using QVCopier.Utility;
  6. namespace QVCopier
  7. {
  8. public partial class QvcMainForm : Form
  9. {
  10. private void InitLogger()
  11. {
  12. UpdateLogStat();
  13. Logger.LogAdded += Logger_LogAdded;
  14. Logger.Info("Application started.");
  15. }
  16. private void Logger_LogAdded(object sender, LogEntry e)
  17. {
  18. if (InvokeRequired)
  19. {
  20. Invoke(new Action<LogEntry>(AddLog), e);
  21. }
  22. else
  23. {
  24. AddLog(e);
  25. }
  26. }
  27. private void AddLog(LogEntry e)
  28. {
  29. _logs.Add(e);
  30. UpdateLogStat();
  31. switch (e.Level)
  32. {
  33. case LogLevel.Debug:
  34. if (DebugCheckBox.Checked) LogListView.Items.Add(CreateLogListViewItem(e));
  35. break;
  36. case LogLevel.Info:
  37. if (InfoCheckBox.Checked) LogListView.Items.Add(CreateLogListViewItem(e));
  38. break;
  39. case LogLevel.Warning:
  40. if (WarningCheckBox.Checked) LogListView.Items.Add(CreateLogListViewItem(e));
  41. break;
  42. case LogLevel.Error:
  43. if (ErrorCheckBox.Checked) LogListView.Items.Add(CreateLogListViewItem(e));
  44. break;
  45. case LogLevel.Fatal:
  46. if (FatalCheckBox.Checked) LogListView.Items.Add(CreateLogListViewItem(e));
  47. break;
  48. default:
  49. throw new ArgumentOutOfRangeException();
  50. }
  51. }
  52. private void UpdateLogStat()
  53. {
  54. FatalCheckBox.Text = $"Fatal({_logs.Count(p => p.Level == LogLevel.Fatal)})";
  55. ErrorCheckBox.Text = $"Error({_logs.Count(p => p.Level == LogLevel.Error)})";
  56. WarningCheckBox.Text = $"Warning({_logs.Count(p => p.Level == LogLevel.Warning)})";
  57. InfoCheckBox.Text = $"Info({_logs.Count(p => p.Level == LogLevel.Info)})";
  58. DebugCheckBox.Text = $"Debug({_logs.Count(p => p.Level == LogLevel.Debug)})";
  59. }
  60. private ListViewItem CreateLogListViewItem(LogEntry logEntry)
  61. {
  62. return new()
  63. {
  64. Text = logEntry.Time.ToString("yyyy-MM-dd HH:mm:ss.ff"),
  65. SubItems =
  66. {
  67. logEntry.Level.ToString(),
  68. logEntry.Log
  69. }
  70. };
  71. }
  72. private void LogFilterCheckBox_CheckedChanged(object sender, EventArgs e)
  73. {
  74. LogListView.Items.Clear();
  75. LogListView.Items.AddRange(_logs.Where(p =>
  76. {
  77. switch (p.Level)
  78. {
  79. case LogLevel.Debug: return DebugCheckBox.Checked;
  80. case LogLevel.Info: return InfoCheckBox.Checked;
  81. case LogLevel.Warning: return WarningCheckBox.Checked;
  82. case LogLevel.Error: return ErrorCheckBox.Checked;
  83. case LogLevel.Fatal: return FatalCheckBox.Checked;
  84. default:
  85. throw new ArgumentOutOfRangeException();
  86. }
  87. }).Select(CreateLogListViewItem).ToArray());
  88. }
  89. }
  90. }