Dispenser/DispenserDesktop/Program.cs

80 lines
2.4 KiB
C#

using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using Avalonia;
using Avalonia.Logging;
using Avalonia.ReactiveUI;
using DispenserUI;
using Serilog;
namespace DispenserDesktop;
internal class Program
{
// Initialization code. Don't use any Avalonia, third-party APIs or any
// SynchronizationContext-reliant code before AppMain is called: things aren't initialized
// yet and stuff might break.
[STAThread]
public static void Main(string[] args)
{
try
{
AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionCatcher;
TaskScheduler.UnobservedTaskException += UnhandledTaskExceptionCatcher;
var mutex = new Mutex(true, Process.GetCurrentProcess().ProcessName, out var createdNew);
// 通过互斥锁来实现进制应用多开
if (!createdNew)
{
Environment.Exit(1);
}
else
{
// 获取到锁
BuildAvaloniaApp().StartWithClassicDesktopLifetime(args);
}
// 释放信号量
mutex.ReleaseMutex();
}
catch (Exception e)
{
Console.WriteLine($"程序捕获崩溃异常: {e.StackTrace}");
Log.Fatal(e, $"程序捕获崩溃异常: {e.Message}");
}
finally
{
Log.CloseAndFlush();
}
}
private static void UnhandledExceptionCatcher(object? sender, UnhandledExceptionEventArgs e)
{
Log.Fatal(e.ExceptionObject as Exception, $"捕获到未处理异常: {((Exception)e.ExceptionObject).Message}");
}
/// <summary>
/// 捕获全局异步线程异常
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void UnhandledTaskExceptionCatcher(object? sender, UnobservedTaskExceptionEventArgs e)
{
Log.Fatal(e.Exception, $"捕获到异步线程异常: {e.Exception.Message}");
e.SetObserved();
}
// Avalonia configuration, don't remove; also used by visual designer.
public static AppBuilder BuildAvaloniaApp()
{
return AppBuilder.Configure<App>()
.UsePlatformDetect()
.With(new SkiaOptions { MaxGpuResourceSizeBytes = 8096000 })
// 开启debug日志级别
.LogToTrace(LogEventLevel.Debug)
.WithInterFont()
.UseReactiveUI();
}
}