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.
67 lines
1.6 KiB
67 lines
1.6 KiB
1 week ago
|
using CoreAgent.API;
|
||
|
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(() =>
|
||
|
{
|
||
|
Log.Information("Application is stopping...");
|
||
|
// 在这里可以添加清理代码,比如关闭数据库连接等
|
||
|
});
|
||
|
|
||
|
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);
|
||
|
}
|