PageLogManage.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 (FileStream fs = new FileStream(logPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
  98. using (StreamReader sr = new StreamReader(fs, Encoding.UTF8))
  99. {
  100. content = await sr.ReadToEndAsync();
  101. }
  102. this.txt_ShowLog.Text = content;
  103. }
  104. catch (Exception ex)
  105. {
  106. LogExtension.ShowMessage(ex.Message, Microsoft.Extensions.Logging.LogLevel.Error);
  107. }
  108. }
  109. private void btn_OpenDire_Click(object sender, EventArgs e)
  110. {
  111. string logPath = Path.Combine(Application.StartupPath, "Logs");
  112. ProcessStartInfo startInfo = new ProcessStartInfo()
  113. {
  114. FileName = logPath,
  115. UseShellExecute = true,
  116. };
  117. Process process = new Process
  118. {
  119. StartInfo = startInfo
  120. };
  121. process.Start();
  122. }
  123. private void btn_ShowToDgv_Click(object sender, EventArgs e)
  124. {
  125. if (this.txt_ShowLog.Text.Length == 0)
  126. {
  127. UIMessageTip.ShowError("请先选择日志文件");
  128. return;
  129. }
  130. string[] lines = this.txt_ShowLog.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
  131. DataTable dt = new DataTable();
  132. dt.Columns.Add("时间");
  133. dt.Columns.Add("日志等级");
  134. dt.Columns.Add("日志来源");
  135. dt.Columns.Add("日志内容");
  136. lines.ToArray().ForEach(line =>
  137. {
  138. string[] parts = line.Split(new string[] { "|" }, StringSplitOptions.None);
  139. if (parts.Length == 4)
  140. {
  141. DataRow row = dt.NewRow();
  142. row["时间"] = parts[0];
  143. row["日志等级"] = parts[1];
  144. row["日志来源"] = parts[2];
  145. row["日志内容"] = parts[3];
  146. dt.Rows.Add(row);
  147. }
  148. });
  149. this.dgv_ShowLog.DataSource = dt;
  150. this.dgv_ShowLog.Columns["日志内容"].Width = 500; // 设置日志内容列的宽度
  151. }
  152. protected override CreateParams CreateParams
  153. {
  154. get
  155. {
  156. CreateParams paras = base.CreateParams;
  157. paras.ExStyle |= 0x02000000;
  158. return paras;
  159. }
  160. }
  161. private void btn_ShowToTXT_Click(object sender, EventArgs e)
  162. {
  163. if (this.txt_ShowLog.Text.Length == 0)
  164. {
  165. UIMessageTip.ShowError("请先选择日志文件");
  166. return;
  167. }
  168. SaveFileDialog saveFileDialog = new SaveFileDialog();
  169. saveFileDialog.Filter = "文本文件 |*.txt";
  170. saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
  171. if (saveFileDialog.ShowDialog() == DialogResult.OK)
  172. {
  173. try
  174. {
  175. using (StreamWriter sw = new StreamWriter(saveFileDialog.FileName, false, Encoding.UTF8))
  176. {
  177. sw.Write(this.txt_ShowLog.Text);
  178. }
  179. UIMessageTip.ShowOk("日志已保存到 " + saveFileDialog.FileName);
  180. LogExtension.ShowMessage("日志导出成功", Microsoft.Extensions.Logging.LogLevel.Information);
  181. ProcessStartInfo startInfo = new ProcessStartInfo
  182. {
  183. FileName = saveFileDialog.FileName,
  184. UseShellExecute = true
  185. };
  186. Process process = new Process
  187. {
  188. StartInfo = startInfo
  189. };
  190. process.Start();
  191. }
  192. catch (Exception ex)
  193. {
  194. UIMessageTip.ShowError("日志导出失败: " + ex.Message);
  195. LogExtension.ShowMessage(ex.Message, Microsoft.Extensions.Logging.LogLevel.Error);
  196. }
  197. }
  198. }
  199. private async void btn_ExportExcel_Click(object sender, EventArgs e)
  200. {
  201. if(this.txt_ShowLog.Text.Length==0)
  202. {
  203. UIMessageTip.ShowError("请先选择日志文件");
  204. return;
  205. }
  206. SaveFileDialog saveFileDialog = new SaveFileDialog();
  207. saveFileDialog.Filter = "Excel文件|*.xlsx";
  208. saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
  209. if(saveFileDialog.ShowDialog()==DialogResult.OK)
  210. {
  211. try
  212. {
  213. if(this.dgv_ShowLog.DataSource==null)
  214. {
  215. UIMessageTip.ShowError("请先将日志显示到表格中");
  216. return;
  217. }else
  218. {
  219. DataTable dt = this.dgv_ShowLog.DataSource as DataTable;
  220. await MiniExcel.SaveAsAsync(saveFileDialog.FileName,dt);
  221. }
  222. UIMessageTip.ShowOk("日志已保存到 " + saveFileDialog.FileName);
  223. LogExtension.ShowMessage("日志导出成功", Microsoft.Extensions.Logging.LogLevel.Information);
  224. ProcessStartInfo startInfo = new ProcessStartInfo
  225. {
  226. FileName = saveFileDialog.FileName,
  227. UseShellExecute = true
  228. };
  229. Process process = new Process
  230. {
  231. StartInfo = startInfo
  232. };
  233. process.Start();
  234. }
  235. catch (Exception ex)
  236. {
  237. UIMessageTip.ShowError("日志导出失败: " + ex.Message);
  238. LogExtension.ShowMessage(ex.Message, Microsoft.Extensions.Logging.LogLevel.Error);
  239. }
  240. }
  241. }
  242. }
  243. }