异常日志

This commit is contained in:
nsnail 2022-12-21 10:01:56 +08:00
parent bd2f88a1d6
commit 67ef6b9fa0
5 changed files with 42 additions and 7 deletions

View File

@ -20,6 +20,7 @@ OPTIONS:
COMMANDS: COMMANDS:
git Git batch operation tool git Git batch operation tool
color Screen coordinate color selection tool color Screen coordinate color selection tool
tran Translation tools
guid GUID tool guid GUID tool
ip IP tools ip IP tools
json Json tool json Json tool
@ -31,7 +32,6 @@ COMMANDS:
tolf Convert newline characters to LF tolf Convert newline characters to LF
get <url> Multithreaded download tool get <url> Multithreaded download tool
``` ```
### Some functional examples ### Some functional examples

View File

@ -19,6 +19,7 @@ OPTIONS:
COMMANDS: COMMANDS:
git Git批量操作工具 git Git批量操作工具
color 屏幕坐标颜色选取工具 color 屏幕坐标颜色选取工具
tran 翻译工具
guid GUID工具 guid GUID工具
ip IP工具 ip IP工具
json Json工具 json Json工具

View File

@ -10,8 +10,9 @@ internal static class Program
{ {
public static int Main(string[] args) public static int Main(string[] args)
{ {
var app = new CommandApp(); CustomCulture(ref args);
var app = new CommandApp();
app.Configure(config => { app.Configure(config => {
config.SetApplicationName(AssemblyInfo.ASSEMBLY_PRODUCT); config.SetApplicationName(AssemblyInfo.ASSEMBLY_PRODUCT);
config.SetApplicationVersion(AssemblyInfo.ASSEMBLY_VERSION); config.SetApplicationVersion(AssemblyInfo.ASSEMBLY_VERSION);
@ -36,8 +37,21 @@ internal static class Program
config.ValidateExamples(); config.ValidateExamples();
}); });
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
return app.Run(args); return app.Run(args);
} }
private static void CustomCulture(ref string[] args)
{
var i = Array.IndexOf(args, "/e");
if (i < 0) {
return;
}
CultureInfo.CurrentCulture = CultureInfo.CurrentUICulture = CultureInfo.GetCultureInfo(args[i + 1]);
var argsList = args.ToList();
argsList.RemoveAt(i);
argsList.RemoveAt(i);
args = argsList.ToArray();
}
} }

View File

@ -1,6 +1,7 @@
#if NET7_0_WINDOWS #if NET7_0_WINDOWS
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Runtime.Versioning; using System.Runtime.Versioning;
using NSExt.Extensions;
namespace Dot.Tran; namespace Dot.Tran;
@ -14,7 +15,10 @@ internal sealed class Main : ToolBase<Option>
AnsiConsole.MarkupLine(Str.StartTranslate); AnsiConsole.MarkupLine(Str.StartTranslate);
AnsiConsole.MarkupLine(Str.HideTranslate); AnsiConsole.MarkupLine(Str.HideTranslate);
var th = new Thread(() => { var th = new Thread(() => {
using var frm = new FrmMain(); Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
AppDomain.CurrentDomain.UnhandledException += UnhandledException;
Application.ThreadException += UIThreadException;
using var frm = new WinMain();
Application.Run(); Application.Run();
}); });
th.SetApartmentState(ApartmentState.STA); th.SetApartmentState(ApartmentState.STA);
@ -22,6 +26,22 @@ internal sealed class Main : ToolBase<Option>
th.Join(); th.Join();
return Task.CompletedTask; return Task.CompletedTask;
} }
private static void Log(string msg)
{
var file = Path.Combine(Path.GetTempPath(), $"{DateTime.Now.yyyyMMdd()}.dotlog");
File.AppendAllText(file, $"{Environment.NewLine}{msg}");
}
private static void UIThreadException(object sender, ThreadExceptionEventArgs e)
{
Log(e.Json());
}
private static void UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
Log(e.Json());
}
} }
#endif #endif

View File

@ -12,7 +12,7 @@ using Size = System.Drawing.Size;
namespace Dot.Tran; namespace Dot.Tran;
[SupportedOSPlatform(nameof(OSPlatform.Windows))] [SupportedOSPlatform(nameof(OSPlatform.Windows))]
internal sealed partial class FrmMain : Form internal sealed partial class WinMain : Form
{ {
private const int _RETRY_WAIT_MIL_SEC = 1000; // 重试等待时间(秒) private const int _RETRY_WAIT_MIL_SEC = 1000; // 重试等待时间(秒)
private const string _TRANSLATE_API_URL = $"{_TRANSLATE_HOME_URL}/v2transapi"; private const string _TRANSLATE_API_URL = $"{_TRANSLATE_HOME_URL}/v2transapi";
@ -28,7 +28,7 @@ internal sealed partial class FrmMain : Form
private nint _nextClipViewer; // 下一个剪贴板监视链对象句柄 private nint _nextClipViewer; // 下一个剪贴板监视链对象句柄
private string _token = "ae72ebad4113270fd26ada5125301268"; private string _token = "ae72ebad4113270fd26ada5125301268";
public FrmMain() public WinMain()
{ {
InitForm(); InitForm();
InitHook(); InitHook();
@ -38,7 +38,7 @@ internal sealed partial class FrmMain : Form
_nextClipViewer = Win32.SetClipboardViewer(Handle); _nextClipViewer = Win32.SetClipboardViewer(Handle);
} }
~FrmMain() ~WinMain()
{ {
Dispose(false); Dispose(false);
} }