PageLogManage.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Diagnostics;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. using HZY.Framework.DependencyInjection;
  12. using Masuit.Tools.Reflection;
  13. using MiniExcelLibs;
  14. using Sunny.UI;
  15. using Sunny.UI.Win32;
  16. namespace Scada.Page
  17. {
  18. public partial class PageLogManage : UIPage, ISingletonSelfDependency
  19. {
  20. public PageLogManage()
  21. {
  22. InitializeComponent();
  23. InitCbData();
  24. }
  25. private void InitCbData()
  26. {
  27. string logpath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Logs");
  28. if (!Directory.Exists(logpath))
  29. {
  30. Directory.CreateDirectory(logpath);
  31. }
  32. string[] dirNames = Directory.GetDirectories(logpath);
  33. dirNames.ForEach(dir =>
  34. {
  35. this.cb_Date.Items.Add(Path.GetFileName(dir));
  36. });
  37. }
  38. private void PageLogManage_Load(object sender, EventArgs e)
  39. {
  40. }
  41. private void PageLogManage_Initialize(object sender, EventArgs e)
  42. {
  43. }
  44. private void cb_Date_SelectedIndexChanged(object sender, EventArgs e)
  45. {
  46. string logPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Logs", this.cb_Date.SelectedItem.ToString());
  47. string[] dirNames = Directory.GetDirectories(logPath);
  48. this.cb_LogLev.Items.Clear();
  49. dirNames.ForEach(x =>
  50. {
  51. this.cb_LogLev.Items.Add(Path.GetFileName(x));
  52. });
  53. }
  54. private void cb_LogLev_SelectedIndexChanged(object sender, EventArgs e)
  55. {
  56. if (this.cb_Date.SelectedItem == null)
  57. {
  58. UIMessageTip.ShowError("请先选择日期");
  59. return;
  60. }
  61. if (this.cb_LogLev.SelectedItem == null)
  62. {
  63. UIMessageTip.ShowError("请先选择日志级别");
  64. return;
  65. }
  66. string logPath = Path.Combine(Application.StartupPath, "Logs", cb_Date.SelectedItem.ToString(), cb_LogLev.SelectedItem.ToString());
  67. this.lb_Files.Items.Clear();
  68. string[] fileNames = Directory.GetFiles(logPath, "*.log");
  69. fileNames.OrderBy(x => x).ToList().ForEach(x =>
  70. {
  71. this.lb_Files.Items.Add(Path.GetFileName(x));
  72. });
  73. }
  74. private async void lb_Files_SelectedIndexChanged(object sender, EventArgs e)
  75. {
  76. if (this.cb_Date.SelectedItem == null)
  77. {
  78. UIMessageTip.ShowError("请先选择日期");
  79. return;
  80. }
  81. if (this.cb_LogLev.SelectedItem == null)
  82. {
  83. UIMessageTip.ShowError("请先选择日志级别");
  84. return;
  85. }
  86. if (this.lb_Files.SelectedItem == null)
  87. {
  88. UIMessageTip.ShowError("请先选择日志文件");
  89. return;
  90. }
  91. try
  92. {
  93. string logPath = Path.Combine(Application.StartupPath, "Logs", cb_Date.SelectedItem.ToString()
  94. , cb_LogLev.SelectedItem.ToString(), lb_Files.SelectedItem.ToString());
  95. string content = string.Empty;
  96. this.txt_ShowLog.Text = "";
  97. using (StreamReader sr = new StreamReader(logPath, Encoding.UTF8))
  98. {
  99. content = await sr.ReadToEndAsync();
  100. }
  101. this.txt_ShowLog.Text = content;
  102. }
  103. catch (Exception ex)
  104. {
  105. LogExtension.ShowMessage(ex.Message, Microsoft.Extensions.Logging.LogLevel.Error);
  106. }
  107. }
  108. private void btn_OpenDire_Click(object sender, EventArgs e)
  109. {
  110. string logPath = Path.Combine(Application.StartupPath, "Logs");
  111. ProcessStartInfo startInfo = new ProcessStartInfo()
  112. {
  113. FileName = logPath,
  114. UseShellExecute = true,
  115. };
  116. Process process = new Process
  117. {
  118. StartInfo = startInfo
  119. };
  120. process.Start();
  121. }
  122. private void btn_ShowToDgv_Click(object sender, EventArgs e)
  123. {
  124. if (this.txt_ShowLog.Text.Length == 0)
  125. {
  126. UIMessageTip.ShowError("请先选择日志文件");
  127. return;
  128. }
  129. string[] lines = this.txt_ShowLog.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
  130. DataTable dt = new DataTable();
  131. dt.Columns.Add("时间");
  132. dt.Columns.Add("日志等级");
  133. dt.Columns.Add("日志来源");
  134. dt.Columns.Add("日志内容");
  135. lines.ToArray().ForEach(line =>
  136. {
  137. string[] parts = line.Split(new string[] { "|" }, StringSplitOptions.None);
  138. if (parts.Length == 4)
  139. {
  140. DataRow row = dt.NewRow();
  141. row["时间"] = parts[0];
  142. row["日志等级"] = parts[1];
  143. row["日志来源"] = parts[2];
  144. row["日志内容"] = parts[3];
  145. dt.Rows.Add(row);
  146. }
  147. });
  148. this.dgv_ShowLog.DataSource = dt;
  149. this.dgv_ShowLog.Columns["日志内容"].Width = 500; // 设置日志内容列的宽度
  150. }
  151. private void btn_ShowToTXT_Click(object sender, EventArgs e)
  152. {
  153. if (this.txt_ShowLog.Text.Length == 0)
  154. {
  155. UIMessageTip.ShowError("请先选择日志文件");
  156. return;
  157. }
  158. SaveFileDialog saveFileDialog = new SaveFileDialog();
  159. saveFileDialog.Filter = "文本文件 |*.txt";
  160. saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
  161. if (saveFileDialog.ShowDialog() == DialogResult.OK)
  162. {
  163. try
  164. {
  165. using (StreamWriter sw = new StreamWriter(saveFileDialog.FileName, false, Encoding.UTF8))
  166. {
  167. sw.Write(this.txt_ShowLog.Text);
  168. }
  169. UIMessageTip.ShowOk("日志已保存到 " + saveFileDialog.FileName);
  170. LogExtension.ShowMessage("日志导出成功", Microsoft.Extensions.Logging.LogLevel.Information);
  171. ProcessStartInfo startInfo = new ProcessStartInfo
  172. {
  173. FileName = saveFileDialog.FileName,
  174. UseShellExecute = true
  175. };
  176. Process process = new Process
  177. {
  178. StartInfo = startInfo
  179. };
  180. process.Start();
  181. }
  182. catch (Exception ex)
  183. {
  184. UIMessageTip.ShowError("日志导出失败: " + ex.Message);
  185. LogExtension.ShowMessage(ex.Message, Microsoft.Extensions.Logging.LogLevel.Error);
  186. }
  187. }
  188. }
  189. private async void btn_ExportExcel_Click(object sender, EventArgs e)
  190. {
  191. if(this.txt_ShowLog.Text.Length==0)
  192. {
  193. UIMessageTip.ShowError("请先选择日志文件");
  194. return;
  195. }
  196. SaveFileDialog saveFileDialog = new SaveFileDialog();
  197. saveFileDialog.Filter = "Excel文件|*.xlsx";
  198. saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
  199. if(saveFileDialog.ShowDialog()==DialogResult.OK)
  200. {
  201. try
  202. {
  203. if(this.dgv_ShowLog.DataSource==null)
  204. {
  205. UIMessageTip.ShowError("请先将日志显示到表格中");
  206. return;
  207. }else
  208. {
  209. DataTable dt = this.dgv_ShowLog.DataSource as DataTable;
  210. await MiniExcel.SaveAsAsync(saveFileDialog.FileName,dt);
  211. }
  212. UIMessageTip.ShowOk("日志已保存到 " + saveFileDialog.FileName);
  213. LogExtension.ShowMessage("日志导出成功", Microsoft.Extensions.Logging.LogLevel.Information);
  214. ProcessStartInfo startInfo = new ProcessStartInfo
  215. {
  216. FileName = saveFileDialog.FileName,
  217. UseShellExecute = true
  218. };
  219. Process process = new Process
  220. {
  221. StartInfo = startInfo
  222. };
  223. process.Start();
  224. }
  225. catch (Exception ex)
  226. {
  227. UIMessageTip.ShowError("日志导出失败: " + ex.Message);
  228. LogExtension.ShowMessage(ex.Message, Microsoft.Extensions.Logging.LogLevel.Error);
  229. }
  230. }
  231. }
  232. }
  233. }