123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Diagnostics;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using HZY.Framework.DependencyInjection;
- using Masuit.Tools.Reflection;
- using MiniExcelLibs;
- using Sunny.UI;
- using Sunny.UI.Win32;
- namespace Scada.Page
- {
- public partial class PageLogManage : UIPage, ISingletonSelfDependency
- {
- public PageLogManage()
- {
- InitializeComponent();
- InitCbData();
- }
- private void InitCbData()
- {
- string logpath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Logs");
- if (!Directory.Exists(logpath))
- {
- Directory.CreateDirectory(logpath);
- }
- string[] dirNames = Directory.GetDirectories(logpath);
- dirNames.ForEach(dir =>
- {
- this.cb_Date.Items.Add(Path.GetFileName(dir));
- });
- }
- private void PageLogManage_Load(object sender, EventArgs e)
- {
- }
- private void PageLogManage_Initialize(object sender, EventArgs e)
- {
- }
- private void cb_Date_SelectedIndexChanged(object sender, EventArgs e)
- {
- string logPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Logs", this.cb_Date.SelectedItem.ToString());
- string[] dirNames = Directory.GetDirectories(logPath);
- this.cb_LogLev.Items.Clear();
- dirNames.ForEach(x =>
- {
- this.cb_LogLev.Items.Add(Path.GetFileName(x));
- });
- }
- private void cb_LogLev_SelectedIndexChanged(object sender, EventArgs e)
- {
- if (this.cb_Date.SelectedItem == null)
- {
- UIMessageTip.ShowError("请先选择日期");
- return;
- }
- if (this.cb_LogLev.SelectedItem == null)
- {
- UIMessageTip.ShowError("请先选择日志级别");
- return;
- }
- string logPath = Path.Combine(Application.StartupPath, "Logs", cb_Date.SelectedItem.ToString(), cb_LogLev.SelectedItem.ToString());
- this.lb_Files.Items.Clear();
- string[] fileNames = Directory.GetFiles(logPath, "*.log");
- fileNames.OrderBy(x => x).ToList().ForEach(x =>
- {
- this.lb_Files.Items.Add(Path.GetFileName(x));
- });
- }
- private async void lb_Files_SelectedIndexChanged(object sender, EventArgs e)
- {
- if (this.cb_Date.SelectedItem == null)
- {
- UIMessageTip.ShowError("请先选择日期");
- return;
- }
- if (this.cb_LogLev.SelectedItem == null)
- {
- UIMessageTip.ShowError("请先选择日志级别");
- return;
- }
- if (this.lb_Files.SelectedItem == null)
- {
- UIMessageTip.ShowError("请先选择日志文件");
- return;
- }
- try
- {
- string logPath = Path.Combine(Application.StartupPath, "Logs", cb_Date.SelectedItem.ToString()
- , cb_LogLev.SelectedItem.ToString(), lb_Files.SelectedItem.ToString());
- string content = string.Empty;
- this.txt_ShowLog.Text = "";
- using (StreamReader sr = new StreamReader(logPath, Encoding.UTF8))
- {
- content = await sr.ReadToEndAsync();
- }
- this.txt_ShowLog.Text = content;
- }
- catch (Exception ex)
- {
- LogExtension.ShowMessage(ex.Message, Microsoft.Extensions.Logging.LogLevel.Error);
- }
- }
- private void btn_OpenDire_Click(object sender, EventArgs e)
- {
- string logPath = Path.Combine(Application.StartupPath, "Logs");
- ProcessStartInfo startInfo = new ProcessStartInfo()
- {
- FileName = logPath,
- UseShellExecute = true,
- };
- Process process = new Process
- {
- StartInfo = startInfo
- };
- process.Start();
- }
- private void btn_ShowToDgv_Click(object sender, EventArgs e)
- {
- if (this.txt_ShowLog.Text.Length == 0)
- {
- UIMessageTip.ShowError("请先选择日志文件");
- return;
- }
- string[] lines = this.txt_ShowLog.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
- DataTable dt = new DataTable();
- dt.Columns.Add("时间");
- dt.Columns.Add("日志等级");
- dt.Columns.Add("日志来源");
- dt.Columns.Add("日志内容");
- lines.ToArray().ForEach(line =>
- {
- string[] parts = line.Split(new string[] { "|" }, StringSplitOptions.None);
- if (parts.Length == 4)
- {
- DataRow row = dt.NewRow();
- row["时间"] = parts[0];
- row["日志等级"] = parts[1];
- row["日志来源"] = parts[2];
- row["日志内容"] = parts[3];
-
- dt.Rows.Add(row);
- }
- });
- this.dgv_ShowLog.DataSource = dt;
- this.dgv_ShowLog.Columns["日志内容"].Width = 500; // 设置日志内容列的宽度
- }
- private void btn_ShowToTXT_Click(object sender, EventArgs e)
- {
- if (this.txt_ShowLog.Text.Length == 0)
- {
- UIMessageTip.ShowError("请先选择日志文件");
- return;
- }
- SaveFileDialog saveFileDialog = new SaveFileDialog();
- saveFileDialog.Filter = "文本文件 |*.txt";
- saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
- if (saveFileDialog.ShowDialog() == DialogResult.OK)
- {
- try
- {
- using (StreamWriter sw = new StreamWriter(saveFileDialog.FileName, false, Encoding.UTF8))
- {
- sw.Write(this.txt_ShowLog.Text);
- }
- UIMessageTip.ShowOk("日志已保存到 " + saveFileDialog.FileName);
- LogExtension.ShowMessage("日志导出成功", Microsoft.Extensions.Logging.LogLevel.Information);
- ProcessStartInfo startInfo = new ProcessStartInfo
- {
- FileName = saveFileDialog.FileName,
- UseShellExecute = true
- };
- Process process = new Process
- {
- StartInfo = startInfo
- };
- process.Start();
- }
- catch (Exception ex)
- {
- UIMessageTip.ShowError("日志导出失败: " + ex.Message);
- LogExtension.ShowMessage(ex.Message, Microsoft.Extensions.Logging.LogLevel.Error);
- }
- }
- }
- private async void btn_ExportExcel_Click(object sender, EventArgs e)
- {
- if(this.txt_ShowLog.Text.Length==0)
- {
- UIMessageTip.ShowError("请先选择日志文件");
- return;
- }
- SaveFileDialog saveFileDialog = new SaveFileDialog();
- saveFileDialog.Filter = "Excel文件|*.xlsx";
- saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
- if(saveFileDialog.ShowDialog()==DialogResult.OK)
- {
- try
- {
- if(this.dgv_ShowLog.DataSource==null)
- {
- UIMessageTip.ShowError("请先将日志显示到表格中");
- return;
- }else
- {
- DataTable dt = this.dgv_ShowLog.DataSource as DataTable;
- await MiniExcel.SaveAsAsync(saveFileDialog.FileName,dt);
- }
- UIMessageTip.ShowOk("日志已保存到 " + saveFileDialog.FileName);
- LogExtension.ShowMessage("日志导出成功", Microsoft.Extensions.Logging.LogLevel.Information);
- ProcessStartInfo startInfo = new ProcessStartInfo
- {
- FileName = saveFileDialog.FileName,
- UseShellExecute = true
- };
- Process process = new Process
- {
- StartInfo = startInfo
- };
- process.Start();
- }
- catch (Exception ex)
- {
- UIMessageTip.ShowError("日志导出失败: " + ex.Message);
- LogExtension.ShowMessage(ex.Message, Microsoft.Extensions.Logging.LogLevel.Error);
- }
- }
- }
- }
- }
|