LogUI.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using HslCommunication.LogNet;
  11. using ProjectManagementSystem.Common.Extenions;
  12. using ProjectManagementSystem.Common.Function;
  13. using System.IO;
  14. namespace ProjectManagementSystem.UI
  15. {
  16. public partial class LogUI : UserControl
  17. {
  18. public int DisplayDegree { get; set; }
  19. public LogUI()
  20. {
  21. InitializeComponent();
  22. }
  23. public void AddLog(string log)
  24. {
  25. this.BeginInvoke((Action)(() =>
  26. {
  27. if(richTextBox1.Lines.Length > 1000)
  28. {
  29. richTextBox1.Clear();
  30. }
  31. richTextBox1.AppendText(log);
  32. richTextBox1.AppendText(Environment.NewLine);
  33. richTextBox1.ScrollToCaret();
  34. }));
  35. }
  36. private void LogUI_Load(object sender, EventArgs e)
  37. {
  38. this.comboBox1.ValueMember = "Value";
  39. this.comboBox1.DisplayMember = "Display";
  40. this.comboBox1.DataSource = typeof(HslMessageDegree).EnumToDataTable("Value", "Display");
  41. this.comboBox1.SelectedIndex = this.comboBox1.Items.Count - 2;
  42. }
  43. private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
  44. {
  45. DisplayDegree = (int)this.comboBox1.SelectedValue;
  46. }
  47. private void button1_Click(object sender, EventArgs e)
  48. {
  49. try
  50. {
  51. string filePath = Common.Log.CLog.Instance.LogDir;
  52. System.Diagnostics.Process.Start(filePath);
  53. }
  54. catch (Exception ex)
  55. {
  56. MessageBox.Show($"操作失败:{ex.Message}");
  57. }
  58. }
  59. private void button2_Click(object sender, EventArgs e)
  60. {
  61. using(FormLogNetView logNetView = new FormLogNetView())
  62. {
  63. logNetView.StartPosition = FormStartPosition.CenterScreen;
  64. logNetView.WindowState = FormWindowState.Normal;
  65. logNetView.ShowDialog();
  66. }
  67. }
  68. private void button3_Click(object sender, EventArgs e)
  69. {
  70. string text = $"确定要开始打包所有日志?(日志压缩包:Log.zip)";
  71. var dialogResult = MessageBox.Show(text, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
  72. if (dialogResult != DialogResult.OK)
  73. {
  74. return;
  75. }
  76. Task.Factory.StartNew(() =>
  77. {
  78. try
  79. {
  80. string logDir = Common.Log.CLog.Instance.LogDir;
  81. FileInfo finfo = new FileInfo(logDir);
  82. string logZip = Path.Combine(finfo.Directory.FullName, $"{finfo.Name}.zip");
  83. if (File.Exists(logZip))
  84. {
  85. File.Delete(logZip);
  86. }
  87. ZipUtility zipUtility = new ZipUtility();
  88. zipUtility.ZipFileFromDirectory(Common.Log.CLog.Instance.LogDir, logZip, 9);
  89. System.Diagnostics.Process.Start(finfo.Directory.FullName, logZip);
  90. }
  91. catch (Exception ex)
  92. {
  93. MessageBox.Show($"操作失败:{ex.Message}");
  94. }
  95. });
  96. }
  97. }
  98. }