You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
1.8 KiB

using CoreAgent.API;
using CoreAgent.Domain.Contexts;
using CoreAgent.Domain.Models.System;
using CoreAgent.Infrastructure.Logging;
using Serilog;
using System;
using System.Text.Json;
var builder = WebApplication.CreateBuilder(args);
try
{
// 第一步:添加所有配置
builder.AddConfigurations();
// 第二步:创建Startup实例
var startup = builder.CreateStartup();
// 第三步:配置服务
startup.ConfigureServices(builder.Services);
var app = builder.Build();
// 配置中间件
startup.Configure(app);
// 配置Swagger
app.ConfigureSwagger();
Log.Information("Application startup completed");
// 注册应用程序关闭事件
var lifetime = app.Lifetime;
lifetime.ApplicationStopping.Register(() =>
{
try
{
CellularNetworkContext.Instance.Dispose();
Log.Information("蜂窝网络上下文已释放");
}
catch (Exception ex)
{
Log.Error(ex, "释放蜂窝网络上下文时发生错误");
}
});
lifetime.ApplicationStopped.Register(() =>
{
Log.Information("Application has stopped.");
// 确保所有日志都被写入
Log.CloseAndFlush();
});
app.Run();
}
catch (Exception ex)
{
Log.Fatal(ex, "Application failed to start: {Message}", ex.Message);
// 记录详细的启动失败信息
var errorDetails = new
{
Message = ex.Message,
StackTrace = ex.StackTrace,
Source = ex.Source,
InnerException = ex.InnerException?.Message
};
Log.Fatal("Startup failure details: {Details}", JsonSerializer.Serialize(errorDetails));
// 确保所有日志都被写入
Log.CloseAndFlush();
// 使用非零退出码表示错误
Environment.Exit(1);
}