A:增减错误处理

D:调整公共项目
This commit is contained in:
jimmy committed 2025-05-27 23:05:20 +08:00
1 parent 67889796ed
commit 2831deffe3
12 files changed
+120 -109

No files matched your search

+6 -6
View File
@@ -12,10 +12,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WinDivertNet", "WinDivertNe
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Common", "Common\Common.csproj", "{F16BB136-23AD-43F6-8D95-4C0D88FD8FBA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UI.Shared.Wpf", "UI.Shared.Wpf\UI.Shared.Wpf.csproj", "{8512B4F6-65F3-48EE-845F-09A1692BB901}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Common.Test", "Common.Test\Common.Test.csproj", "{B3F481C9-B351-449C-AE2B-CDEC29BD5DCC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Common.Wpf", "Common.Wpf\Common.Wpf.csproj", "{293477FA-3C1C-429F-9FBA-AD16A3E402BC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -46,13 +46,13 @@ Global
{F16BB136-23AD-43F6-8D95-4C0D88FD8FBA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F16BB136-23AD-43F6-8D95-4C0D88FD8FBA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F16BB136-23AD-43F6-8D95-4C0D88FD8FBA}.Release|Any CPU.Build.0 = Release|Any CPU
{8512B4F6-65F3-48EE-845F-09A1692BB901}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8512B4F6-65F3-48EE-845F-09A1692BB901}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8512B4F6-65F3-48EE-845F-09A1692BB901}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8512B4F6-65F3-48EE-845F-09A1692BB901}.Release|Any CPU.Build.0 = Release|Any CPU
{B3F481C9-B351-449C-AE2B-CDEC29BD5DCC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B3F481C9-B351-449C-AE2B-CDEC29BD5DCC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B3F481C9-B351-449C-AE2B-CDEC29BD5DCC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B3F481C9-B351-449C-AE2B-CDEC29BD5DCC}.Release|Any CPU.Build.0 = Release|Any CPU
{293477FA-3C1C-429F-9FBA-AD16A3E402BC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{293477FA-3C1C-429F-9FBA-AD16A3E402BC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{293477FA-3C1C-429F-9FBA-AD16A3E402BC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{293477FA-3C1C-429F-9FBA-AD16A3E402BC}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
+1
View File
@@ -1,4 +1,5 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AApplication_002Ecs_002Fl_003AC_0021_003FUsers_003Fcharlatans_003FAppData_003FRoaming_003FJetBrains_003FRider2025_002E1_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F6412d4331611499aab4eb63809a2a83bf60910_003Fd6_003F8ef46f2c_003FApplication_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AFileVersionInfo_002Ecs_002Fl_003AC_0021_003FUsers_003Fcharlatans_003FAppData_003FRoaming_003FJetBrains_003FRider2025_002E1_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F7fad91e527c1434bb313ddbc78144605b8a0_003F3f_003F911bc64d_003FFileVersionInfo_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AFile_002Ecs_002Fl_003AC_0021_003FUsers_003Fcharlatans_003FAppData_003FRoaming_003FJetBrains_003FRider2025_002E1_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F211e6f3b24fa438a92f1815153647ce2c8f908_003F71_003F40854420_003FFile_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AIWebSocketConnection_002Ecs_002Fl_003AC_0021_003FUsers_003Fcharlatans_003FAppData_003FRoaming_003FJetBrains_003FRider2025_002E1_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003Fa7d96aff8e344c1d9aa8042f229d493cae00_003Fc1_003Fb154f2eb_003FIWebSocketConnection_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
+32 -58
View File
@@ -4,6 +4,7 @@ using System.Net.Http;
using System.Windows;
using System.Windows.Threading;
using Application.Utils;
using Common.Logger;
using Microsoft.Extensions.Logging;
namespace Shell;
@@ -15,25 +16,43 @@ public partial class App : System.Windows.Application
{
protected override void OnStartup(StartupEventArgs e)
{
// 注册全局异常处理
this.DispatcherUnhandledException += App_DispatcherUnhandledException;
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
// 异常处理
ExceptionCatch();
base.OnStartup(e);
// ✅ 启动你的服务(如 WebSocket、HTTP、端口监听等)
StartMyServer();
}
/// <summary>
/// 异常捕捉
/// </summary>
private void ExceptionCatch()
{
DispatcherUnhandledException += App_DispatcherUnhandledException;
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
// 注册全局异常处理
TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
// 启动主窗口
MainWindow mainWindow = new MainWindow();
mainWindow.Show();
}
/// <summary>
/// 处理异常
/// </summary>
/// <param name="exception"></param>
private void HandleException(Exception? exception)
{
Log.Error("未捕获异常", exception);
// 错误记录,错误处理
if (exception != null)
{
// todo:2023年10月29日 恭喜你发现了BUG页面
MessageBox.Show($"恭喜你发现了BUG,请联系管理员解决:{exception.Message}");
}
}
private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
LogException(e.Exception, "UI线程异常");
Log.Error(e.Exception, "UI线程异常");
HandleException(e.Exception);
// 根据异常类型决定是否继续运行
if (CanRecover(e.Exception))
{
@@ -50,7 +69,7 @@ public partial class App : System.Windows.Application
{
foreach (var ex in e.Exception.InnerExceptions)
{
LogException(ex, "Task未观察异常");
Log.Error(e.Exception, "UI线程异常");
}
e.SetObserved(); // 标记异常已被观察,防止应用终止
@@ -59,13 +78,13 @@ public partial class App : System.Windows.Application
private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
var exception = e.ExceptionObject as Exception;
LogException(exception, "非UI线程异常");
Log.Error(exception, "非UI线程异常");
if (e.IsTerminating)
{
// 应用即将终止,进行紧急清理
// EmergencyCleanup();
// LogCritical("应用程序即将因未处理异常而终止");
Log.Fatal("应用程序即将因未处理异常而终止");
}
}
private bool CanRecover(Exception exception)
@@ -83,49 +102,6 @@ public partial class App : System.Windows.Application
};
}
private void LogException(Exception exception, string context)
{
// var logLevel = GetLogLevel(exception);
// var logMessage = $"[{context}] {exception.GetType().Name}: {exception.Message}\n{exception.StackTrace}";
//
// // 使用日志框架记录
// Logger<>.Log(logLevel, logMessage);
//
// // 严重异常时发送邮件或其他通知
// if (logLevel == LogLevel.Critical)
// {
// NotifyDevelopers(exception, context);
// }
}
// 对于async/await操作,确保正确处理异常
public async Task<T> SafeExecuteAsync<T>(Func<Task<T>> operation, T defaultValue = default)
{
try
{
return await operation();
}
catch (Exception ex)
{
LogException(ex, "异步操作异常");
return defaultValue;
}
}
// 对于Fire-and-Forget的Task,确保异常被捕获
public void SafeFireAndForget(Func<Task> operation)
{
Task.Run(async () =>
{
try
{
await operation();
}
catch (Exception ex)
{
LogException(ex, "后台任务异常");
}
});
}
private void StartMyServer()
{
int port = SysHelper.GetAvailablePort();
@@ -138,10 +114,8 @@ public partial class App : System.Windows.Application
});
}
private void StartWebSocketServer(int port)
{
// 示例服务逻辑,如上文 WebSocket 实现
}
}
+13
View File
@@ -0,0 +1,13 @@
# 默认忽略的文件
/shelf/
/workspace.xml
# Rider 忽略的文件
/modules.xml
/.idea.UI.Shared.iml
/contentModel.xml
/projectSettingsUpdater.xml
# 基于编辑器的 HTTP 客户端请求
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="UserContentModel">
<attachedFolders />
<explicitIncludes />
<explicitExcludes />
</component>
</project>
+6
View File
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>
+1 -3
View File
@@ -1,8 +1,6 @@
<Application x:Class="UI.Shared.Wpf.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:UI.Shared.Wpf"
StartupUri="MainWindow.xaml">
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Application.Resources>
</Application.Resources>
+3 -7
View File
@@ -1,12 +1,8 @@
using System.Configuration;
using System.Data;
using System.Windows;
namespace UI.Shared.Wpf;
namespace UI.Shared.Wpf;
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
public partial class App : System.Windows.Application
{
}
}
+38
View File
@@ -0,0 +1,38 @@
using System.Windows;
namespace UI.Shared.Wpf;
public static class GlobalExceptionHandler
{
public static void RegisterGlobalExceptionHandlers(System.Windows.Application app)
{
// UI线程异常处理
app.DispatcherUnhandledException += (sender, e) =>
{
HandleException(e.Exception, "UI线程异常");
e.Handled = true;
};
// 非UI线程异常处理
AppDomain.CurrentDomain.UnhandledException += (sender, e) =>
{
if (e.ExceptionObject is Exception ex)
HandleException(ex, "非UI线程异常");
};
// 任务未观察到的异常处理
TaskScheduler.UnobservedTaskException += (sender, e) =>
{
HandleException(e.Exception, "任务未观察异常");
e.SetObserved();
};
}
private static void HandleException(Exception ex, string context)
{
// TODO: 替换为你自己的日志系统或上报服务
string message = $"【{context}】{ex.Message}\n{ex.StackTrace}";
MessageBox.Show(message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
// 例如 LogHelper.LogError(ex);
}
}
-12
View File
@@ -1,12 +0,0 @@
<Window x:Class="UI.Shared.Wpf.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:UI.Shared.Wpf"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
</Grid>
</Window>
-23
View File
@@ -1,23 +0,0 @@
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 UI.Shared.Wpf;
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
+12
View File
@@ -8,4 +8,16 @@
<UseWPF>true</UseWPF>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Common\Common.csproj" />
</ItemGroup>
<ItemGroup>
<ApplicationDefinition Include="App.xaml">
<Generator>MSBuild:Compile</Generator>
<XamlRuntime>Wpf</XamlRuntime>
<SubType>Designer</SubType>
</ApplicationDefinition>
</ItemGroup>
</Project>