日志输出逻辑修改

This commit is contained in:
Administrator committed 2026-06-01 17:53:48 +08:00
1 parent 10611241d1
commit f2a3b697d2
10 files changed
+186 -16

No files matched your search

+2 -1
View File
@@ -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)
{
+1
View File
@@ -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");
}
+3 -2
View File
@@ -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());
}
}
+1
View File
@@ -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)
+6 -4
View File
@@ -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;
}
+118
View File
@@ -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();
}
}
}
}
+2 -1
View File
@@ -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;
}
}
+15 -8
View File
@@ -10,25 +10,32 @@ namespace SunlightAggregationManager.UserClass
public class ControlWriter : TextWriter
{
private readonly TextBox _textbox;
private readonly Action<string> _logGingDataAction;
public ControlWriter(TextBox textbox)
public ControlWriter(TextBox textbox,Action<string> 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<string> logGingDataAction)
{
// 创建新的 Writer
_currentWriter = new ControlWriter(logBox);
_currentWriter = new ControlWriter(logBox, logGingDataAction);
// 【关键】将 Console 的输出重定向到这个 Writer
// 这样 Console.WriteLine("...") 就会自动写入到 logBox 中
+12
View File
@@ -0,0 +1,12 @@
<UserControl x:Class="SunlightAggregationManager.View.ExtendPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:SunlightAggregationManager.View"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
</Grid>
</UserControl>
+26
View File
@@ -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
{
/// <summary>
/// ExtendPage.xaml 的交互逻辑
/// </summary>
public partial class ExtendPage : UserControl
{
public ExtendPage()
{
InitializeComponent();
}
}
}