From f2a3b697d2dcf834fb9aa29c40fe820a3c42c809 Mon Sep 17 00:00:00 2001 From: Administrator Date: Mon, 1 Jun 2026 17:53:48 +0800 Subject: [PATCH] =?UTF-8?q?=E6=97=A5=E5=BF=97=E8=BE=93=E5=87=BA=E9=80=BB?= =?UTF-8?q?=E8=BE=91=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MainWindow.xaml.cs | 3 +- UserClass/AsyncHttpServer.cs | 1 + UserClass/AsyncTcpClient.cs | 5 +- UserClass/AsyncTcpServer.cs | 1 + UserClass/DataBase.cs | 10 +-- UserClass/LogGing.cs | 118 +++++++++++++++++++++++++++++++++++ UserClass/NetFwManger.cs | 3 +- UserClass/TextWriter.cs | 23 ++++--- View/ExtendPage.xaml | 12 ++++ View/ExtendPage.xaml.cs | 26 ++++++++ 10 files changed, 186 insertions(+), 16 deletions(-) create mode 100644 UserClass/LogGing.cs create mode 100644 View/ExtendPage.xaml create mode 100644 View/ExtendPage.xaml.cs diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs index 124607a..dc8aef0 100644 --- a/MainWindow.xaml.cs +++ b/MainWindow.xaml.cs @@ -24,7 +24,7 @@ namespace SunlightAggregationManager } private void Window_Loaded(object sender, RoutedEventArgs e) { - GlobalLogManager.RegisterTarget(LOG); + GlobalLogManager.RegisterTarget(LOG,LogGing.LogGingDATA); DataContext = new MainWindowViewModel(); } @@ -86,6 +86,7 @@ namespace SunlightAggregationManager { state.Visibility = Visibility.Collapsed; Picture.Visibility = Visibility.Visible; + Picture.Content = new View.ExtendPage(); } private void ListViewItem_MouseLeftButtonUp_user(object sender, MouseButtonEventArgs e) { diff --git a/UserClass/AsyncHttpServer.cs b/UserClass/AsyncHttpServer.cs index b335c8b..cb80ea7 100644 --- a/UserClass/AsyncHttpServer.cs +++ b/UserClass/AsyncHttpServer.cs @@ -101,6 +101,7 @@ namespace SunlightAggregationManager.UserClass } catch (Exception ex) {//错误 + LogGing.ERRDATA(ex); Console.WriteLine("Http:" + ex.ToString()); //client.SendAsync("Target instruction parsing error manager will close connection"); } diff --git a/UserClass/AsyncTcpClient.cs b/UserClass/AsyncTcpClient.cs index 7f08a64..5652b5b 100644 --- a/UserClass/AsyncTcpClient.cs +++ b/UserClass/AsyncTcpClient.cs @@ -38,7 +38,7 @@ namespace SunlightAggregationManager.UserClass tcpClient.Connected = (client, e) => { //设定显示状态 - System.Data.DataRow targetRow =ViewModel.MainWindowViewModel._service.Select("ID = 1").FirstOrDefault()!; + System.Data.DataRow targetRow =ViewModel.MainWindowViewModel._service.Select("Service = 'Sunlight_Server_Link'").FirstOrDefault()!; if (targetRow != null) { targetRow.BeginEdit(); @@ -60,7 +60,7 @@ namespace SunlightAggregationManager.UserClass };//成功连接到服务器 tcpClient.Closing = (client, e) => { return EasyTask.CompletedTask; };//即将从服务器断开连接。此处仅主动断开才有效。 tcpClient.Closed = (client, e) => { - System.Data.DataRow targetRow = ViewModel.MainWindowViewModel._service.Select("ID = 1").FirstOrDefault()!; + System.Data.DataRow targetRow = ViewModel.MainWindowViewModel._service.Select("Service = 'Sunlight_Server_Link'").FirstOrDefault()!; if (targetRow != null) { targetRow.BeginEdit(); @@ -138,6 +138,7 @@ namespace SunlightAggregationManager.UserClass } catch (Exception ex) { + LogGing.ERRDATA(ex); Console.WriteLine("Tcp:" + ex.ToString()); } } diff --git a/UserClass/AsyncTcpServer.cs b/UserClass/AsyncTcpServer.cs index 356e6dc..d244548 100644 --- a/UserClass/AsyncTcpServer.cs +++ b/UserClass/AsyncTcpServer.cs @@ -139,6 +139,7 @@ namespace SunlightAggregationManager.UserClass } catch (Exception ex) {//错误 + LogGing.ERRDATA(ex); Console.WriteLine("Tcp:" + ex.ToString()); client.SendAsync("Target instruction parsing error manager will close connection"); foreach (var item in AsyncTcpServer.service.Clients) diff --git a/UserClass/DataBase.cs b/UserClass/DataBase.cs index e7feacd..2b20f6e 100644 --- a/UserClass/DataBase.cs +++ b/UserClass/DataBase.cs @@ -18,7 +18,7 @@ namespace SunlightAggregationManager.UserClass private string DB_IP = null!; private string DB_PASSWORD = null!; - public void Config(string ip, string name, string type, string id, string password) + public async void Config(string ip, string name, string type, string id, string password) { DB_IP = ip; DB_ID = id; @@ -26,7 +26,7 @@ namespace SunlightAggregationManager.UserClass DB_TYPE = type; DB_PASSWORD = password; - _= ConfigAsync(); + await ConfigAsync(); } public async Task ConfigAsync() @@ -63,6 +63,7 @@ namespace SunlightAggregationManager.UserClass } catch (Exception ex) { + LogGing.ERRDATA(ex); Console.WriteLine("DataBase Link timeout\n"+ex.ToString()); // 失败处理 return; // 直接返回,不执行后续代码 @@ -76,7 +77,7 @@ namespace SunlightAggregationManager.UserClass // 创建一个空的 DataTable 用于存放查询结果 DataTable dataTable = new DataTable(); - // 沿用你原有的数据库连接字符串构建逻辑 + // 数据库连接字符串构建逻辑 var builder = new SqlConnectionStringBuilder { DataSource = DB_IP, @@ -109,8 +110,9 @@ namespace SunlightAggregationManager.UserClass } catch (Exception ex) { + LogGing.ERRDATA(ex); Console.WriteLine("DataBase Query failed\n" + ex.ToString()); - // 这里可以选择将异常抛出给调用方,或者返回一个空的 DataTable + // 将异常抛出给调用方,或者返回一个空的 DataTable throw; } diff --git a/UserClass/LogGing.cs b/UserClass/LogGing.cs new file mode 100644 index 0000000..46369fd --- /dev/null +++ b/UserClass/LogGing.cs @@ -0,0 +1,118 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.IO; + +namespace SunlightAggregationManager.UserClass +{ + public class LogGing + { + public static void LogGingDATA(string dat) + { + string logpath = System.Environment.CurrentDirectory + "\\Log";//日志文件目录 + string logPath = "" + System.Environment.CurrentDirectory + "\\Log\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";//日志文件 + string Log_time = "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]:"; + + if (Directory.Exists(logpath))//检查日志路径 + { + if (!File.Exists(logPath))//检查日志文件并写入启动日志 + { + FileStream fs = new FileStream(logPath, FileMode.CreateNew, FileAccess.Write);//创建写入文件 + StreamWriter wr = new StreamWriter(fs);//创建文件 + wr.WriteLine(Log_time + dat); + wr.Close(); + } + else + { + try + { + FileStream fs = new FileStream(logPath, FileMode.Append, FileAccess.Write); + StreamWriter wr = new StreamWriter(fs);//创建文件 + wr.WriteLine(Log_time + dat); + wr.Close(); + } + catch { } + } + } + else + { + DirectoryInfo directoryInfo = new DirectoryInfo(logpath); + directoryInfo.Create();//创建日志路径 + } + } + public static void ERRDATA(System.Exception dat) + { + string Log_time = DateTime.Now.ToString("yyyy-MM-dd"); + string logpath = System.Environment.CurrentDirectory + "\\ERR";//日志文件目录 + // string logPathtxt = "" + System.Environment.CurrentDirectory + "\\Log\\"+ Log_time + "Log.txt";//日志文件 + // System.IO.DirectoryInfo log = new System.IO.DirectoryInfo();//生成日志文件目录 + string log_path = logpath + "\\ERR" + Log_time + ".txt"; + string Log_timehms = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); + if (Directory.Exists(logpath))//检查日志路径 + { + if (!File.Exists(log_path))//检查文件并写入 + { + // FileStream fs = new FileStream(log_path, FileMode.CreateNew, FileAccess.Write);//创建文件 + // StreamWriter wr = new StreamWriter(fs);//创建文件 + // wr.Close(); + FileStream fil = new FileStream(log_path, FileMode.CreateNew, FileAccess.Write);//创建写入文件 + StreamWriter wfil = new StreamWriter(fil);//创建文件 + wfil.WriteLine("[" + Log_timehms + "];[Error] ||" + Environment.NewLine.ToString()); + wfil.WriteLine("[" + Log_timehms + "];[Error source] ||" + dat.Source.ToString() + Environment.NewLine.ToString()); + wfil.WriteLine("[" + Log_timehms + "];[Error message] ||" + dat.Message.ToString() + Environment.NewLine.ToString()); + wfil.WriteLine("[" + Log_timehms + "];[Error area] ||" + dat.StackTrace.ToString()); + wfil.Close(); + } + else + { + FileStream fs = new FileStream(log_path, FileMode.Append, FileAccess.Write);//创建写入文件 + StreamWriter wr = new StreamWriter(fs);//创建文件 + wr.WriteLine("[" + Log_timehms + "];[Error] ||" + Environment.NewLine.ToString()); + wr.WriteLine("[" + Log_timehms + "];[Error source] ||" + dat.ToString() + Environment.NewLine.ToString()); + wr.WriteLine("[" + Log_timehms + "];[Error message] ||" + dat.Message.ToString() + Environment.NewLine.ToString()); + wr.WriteLine("[" + Log_timehms + "];[Error area] ||" + dat.ToString()); + wr.Close(); + } + } + else + { + DirectoryInfo directoryInfo = new DirectoryInfo(logpath); + directoryInfo.Create(); + } + } + public static void ExchangeDATA(string dat) + { + string Log_time = DateTime.Now.ToString("yyyy-MM-dd"); + string logpath = System.Environment.CurrentDirectory + "\\Exchange";//日志文件目录 + // string logPathtxt = "" + System.Environment.CurrentDirectory + "\\Log\\"+ Log_time + "Log.txt";//日志文件 + // System.IO.DirectoryInfo log = new System.IO.DirectoryInfo();//生成日志文件目录 + string log_path = logpath + "\\Exchange" + Log_time + ".txt"; + string Log_timehms = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); + if (Directory.Exists(logpath))//检查日志路径 + { + if (!File.Exists(log_path))//检查文件并写入 + { + // FileStream fs = new FileStream(log_path, FileMode.CreateNew, FileAccess.Write);//创建文件 + // StreamWriter wr = new StreamWriter(fs);//创建文件 + // wr.Close(); + FileStream fil = new FileStream(log_path, FileMode.CreateNew, FileAccess.Write);//创建写入文件 + StreamWriter wfil = new StreamWriter(fil);//创建文件 + wfil.WriteLine("[" + Log_timehms + "];[Exchange] ||" + dat); + wfil.Close(); + } + else + { + FileStream fs = new FileStream(log_path, FileMode.Append, FileAccess.Write);//创建写入文件 + StreamWriter wr = new StreamWriter(fs);//创建文件 + wr.WriteLine("[" + Log_timehms + "];[Exchange] ||" + dat); + wr.Close(); + } + } + else + { + DirectoryInfo directoryInfo = new DirectoryInfo(logpath); + directoryInfo.Create(); + } + } + } +} diff --git a/UserClass/NetFwManger.cs b/UserClass/NetFwManger.cs index 344ced9..0fed532 100644 --- a/UserClass/NetFwManger.cs +++ b/UserClass/NetFwManger.cs @@ -46,6 +46,7 @@ namespace SunlightAggregationManager.UserClass //将规则添加到防火墙策略中 policy?.Rules.Add(rule); + Console.WriteLine("Add firewall port:"+port); } public static void DelPort(int port, string protocol) @@ -65,7 +66,7 @@ namespace SunlightAggregationManager.UserClass if (rule.Name == GetName(port, protocol)) { rules.Remove(rule.Name); - Console.WriteLine(@"Firewall rule deleted successfully."); + //Console.WriteLine(@"Firewall rule deleted successfully."); break; } } diff --git a/UserClass/TextWriter.cs b/UserClass/TextWriter.cs index 13c8148..a9eed1a 100644 --- a/UserClass/TextWriter.cs +++ b/UserClass/TextWriter.cs @@ -10,25 +10,32 @@ namespace SunlightAggregationManager.UserClass public class ControlWriter : TextWriter { private readonly TextBox _textbox; + private readonly Action _logGingDataAction; - public ControlWriter(TextBox textbox) + public ControlWriter(TextBox textbox,Action logGingDataAction) { _textbox = textbox; + _logGingDataAction = logGingDataAction; } - public override void Write(char value) => AppendText(DateTime.Now + ": " + value.ToString()); - public override void Write(string? value) => AppendText(DateTime.Now + ": " + value); - public override void WriteLine(string? value) => AppendText(DateTime.Now + ": "+value + Environment.NewLine); + public override void Write(char value) => AppendText(DateTime.Now + ": " + value.ToString(), true); + public override void Write(string? value) => AppendText(DateTime.Now + ": " + value, true); + public override void WriteLine(string? value) => AppendText(DateTime.Now + ": "+value + Environment.NewLine, true); - private void AppendText(string text) + private void AppendText(string text, bool invokeExternalLog) { if (!_textbox.Dispatcher.CheckAccess()) { - _textbox.Dispatcher.Invoke(DispatcherPriority.Background, new Action(() => AppendText(text))); + _textbox.Dispatcher.Invoke(DispatcherPriority.Background, new Action(() => AppendText(text, true))); return; } _textbox.AppendText(text); _textbox.ScrollToEnd(); + + if (invokeExternalLog) + { + _logGingDataAction?.Invoke(text); + } } public override Encoding Encoding => Encoding.UTF8; @@ -41,10 +48,10 @@ namespace SunlightAggregationManager.UserClass // 【核心功能】注册目标 TextBox // 子页面加载时调用这个方法 - public static void RegisterTarget(TextBox logBox) + public static void RegisterTarget(TextBox logBox ,Action logGingDataAction) { // 创建新的 Writer - _currentWriter = new ControlWriter(logBox); + _currentWriter = new ControlWriter(logBox, logGingDataAction); // 【关键】将 Console 的输出重定向到这个 Writer // 这样 Console.WriteLine("...") 就会自动写入到 logBox 中 diff --git a/View/ExtendPage.xaml b/View/ExtendPage.xaml new file mode 100644 index 0000000..c7431e1 --- /dev/null +++ b/View/ExtendPage.xaml @@ -0,0 +1,12 @@ + + + + + diff --git a/View/ExtendPage.xaml.cs b/View/ExtendPage.xaml.cs new file mode 100644 index 0000000..279d4eb --- /dev/null +++ b/View/ExtendPage.xaml.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Windows.Navigation; +using System.Windows.Shapes; + +namespace SunlightAggregationManager.View +{ + /// + /// ExtendPage.xaml 的交互逻辑 + /// + public partial class ExtendPage : UserControl + { + public ExtendPage() + { + InitializeComponent(); + } + } +}