Browse Source

日志模块

Linsk 3 months ago
parent
commit
51fecdaf3a
2 changed files with 119 additions and 0 deletions
  1. 5 0
      Scada/Page/PageLogManage.Designer.cs
  2. 114 0
      Scada/Page/PageLogManage.cs

+ 5 - 0
Scada/Page/PageLogManage.Designer.cs

@@ -106,6 +106,7 @@
             btn_ShowToDgv.TabIndex = 4;
             btn_ShowToDgv.Text = "显示到表格";
             btn_ShowToDgv.TipsFont = new Font("宋体", 9F, FontStyle.Regular, GraphicsUnit.Point, 134);
+            btn_ShowToDgv.Click += btn_ShowToDgv_Click;
             // 
             // btn_OpenDire
             // 
@@ -118,6 +119,7 @@
             btn_OpenDire.TabIndex = 3;
             btn_OpenDire.Text = "打开日志目录";
             btn_OpenDire.TipsFont = new Font("宋体", 9F, FontStyle.Regular, GraphicsUnit.Point, 134);
+            btn_OpenDire.Click += btn_OpenDire_Click;
             // 
             // lb_Files
             // 
@@ -133,6 +135,7 @@
             lb_Files.Size = new Size(261, 163);
             lb_Files.TabIndex = 2;
             lb_Files.Text = "uiListBox1";
+            lb_Files.SelectedIndexChanged += lb_Files_SelectedIndexChanged;
             // 
             // cb_LogLev
             // 
@@ -151,6 +154,7 @@
             cb_LogLev.TabIndex = 1;
             cb_LogLev.TextAlignment = ContentAlignment.MiddleLeft;
             cb_LogLev.Watermark = "选择类型";
+            cb_LogLev.SelectedIndexChanged += cb_LogLev_SelectedIndexChanged;
             // 
             // cb_Date
             // 
@@ -169,6 +173,7 @@
             cb_Date.TabIndex = 0;
             cb_Date.TextAlignment = ContentAlignment.MiddleLeft;
             cb_Date.Watermark = "选择日期";
+            cb_Date.SelectedIndexChanged += cb_Date_SelectedIndexChanged;
             // 
             // uiTitlePanel2
             // 

+ 114 - 0
Scada/Page/PageLogManage.cs

@@ -2,6 +2,7 @@
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
+using System.Diagnostics;
 using System.Drawing;
 using System.Linq;
 using System.Text;
@@ -17,8 +18,26 @@ namespace Scada.Page
         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)
         {
 
@@ -28,5 +47,100 @@ namespace Scada.Page
         {
 
         }
+
+        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)
+        {
+
+        }
     }
 }