using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using SlnMesnac.Config; using SlnMesnac.Quartz; using TouchSocket.Sockets; using SlnMesnac.Extensions; using Microsoft.AspNetCore.Hosting; using Serilog; using Autofac.Extensions.DependencyInjection; using SlnMesnac.Serilog; namespace ConsoleApp { internal class Program { private static SerilogHelper? serilogHelper; static void Main(string[] args) { try { Thread.CurrentThread.Name = "Main"; Console.ForegroundColor = ConsoleColor.Yellow; AppDomain.CurrentDomain.UnhandledException += GlobalExceptionHandler; //全局异常捕获 var host = CreateHostBuilder(args).Build(); var hostTask = host.RunAsync(); var appConfig = host.Services.GetService(); //var logPath = $"{appConfig.logPath}/Logs/{DateTime.UtcNow:yyyy-MM-dd}/"; //Log.Information($"系统初始化完成,日志存放路径:{appConfig.logPath}"); serilogHelper = host.Services.GetRequiredService(); MainCentralControl mainCentralControl = new MainCentralControl(host); mainCentralControl.Start(); serilogHelper.Info("iMES系统启动成功"); while (true) { Console.ReadLine(); } } catch (Exception ex) { Console.WriteLine(ex.Message); if (serilogHelper != null) { serilogHelper.Error("系统启动失败,异常:" + ex.Message); } } } private static void GlobalExceptionHandler(object sender, UnhandledExceptionEventArgs e) { //当前线程名称 Thread.CurrentThread.Name = "Error"; if (e.ExceptionObject is Exception exception) { Console.WriteLine("全局异常捕获:"); Console.WriteLine(exception.Message); Console.WriteLine(exception.StackTrace); if (serilogHelper != null) { serilogHelper.Error("全局异常捕获:" + exception.Message + "\n" + exception.StackTrace); } } else { Console.WriteLine("未知异常捕获:"); Console.WriteLine(e.ExceptionObject); if (serilogHelper != null) { serilogHelper.Error("未知异常捕获:" + e.ExceptionObject); } } } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .UseSerilog() .UseServiceProviderFactory(new AutofacServiceProviderFactory()) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup(); }); } }