Updated the `try-catch` block in `Program.cs` to rethrow exceptions after logging them, ensuring proper error propagation. Added a `public partial class Program` declaration to enable splitting the `Program` class across multiple files. Adjusted closing braces to align with the new structure.
102 lines
3.3 KiB
C#
102 lines
3.3 KiB
C#
using Microsoft.AspNetCore.Rewrite;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using NLog;
|
|
using NLog.Web;
|
|
using ReC.API.Middleware;
|
|
using ReC.Application;
|
|
using ReC.Infrastructure;
|
|
using System.Reflection;
|
|
using LogLevel = Microsoft.Extensions.Logging.LogLevel;
|
|
|
|
var logger = LogManager.Setup().LoadConfigurationFromAppSettings().GetCurrentClassLogger();
|
|
logger.Info("Logging initialized!");
|
|
|
|
try
|
|
{
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
builder.Logging.SetMinimumLevel(LogLevel.Trace);
|
|
|
|
if (!builder.Environment.IsDevelopment())
|
|
{
|
|
builder.Logging.ClearProviders();
|
|
builder.Host.UseNLog();
|
|
}
|
|
|
|
var config = builder.Configuration;
|
|
|
|
Directory
|
|
.GetFiles(builder.Environment.ContentRootPath, "appsettings.*.json", SearchOption.TopDirectoryOnly)
|
|
.Where(file => Path.GetFileName(file) != $"appsettings.Development.json")
|
|
.Where(file => Path.GetFileName(file) != $"appsettings.migration.json")
|
|
.ToList()
|
|
.ForEach(file => config.AddJsonFile(file, true, true));
|
|
|
|
// Add services to the container.
|
|
builder.Services.AddRecServices(options =>
|
|
{
|
|
options.LuckyPennySoftwareLicenseKey = builder.Configuration["LuckyPennySoftwareLicenseKey"];
|
|
options.ConfigureRecActions(config.GetSection("RecAction"));
|
|
options.ConfigureSqlException(config.GetSection("SqlException"));
|
|
});
|
|
|
|
builder.Services.AddRecInfrastructure(options =>
|
|
{
|
|
options.ConfigureDbContext((provider, opt) =>
|
|
{
|
|
var cnnStr = builder.Configuration.GetConnectionString("Default")
|
|
?? throw new InvalidOperationException("Connection string is not found.");
|
|
|
|
var logger = provider.GetRequiredService<ILogger<RecDbContext>>();
|
|
var enableSensitiveDataLogging = config.GetValue("EfCore:EnableSensitiveDataLogging", true);
|
|
var enableDetailedErrors = config.GetValue("EfCore:EnableDetailedErrors", false);
|
|
|
|
opt.UseSqlServer(cnnStr)
|
|
.LogTo(log => logger.LogInformation("{log}", log), LogLevel.Trace)
|
|
.EnableSensitiveDataLogging(enableSensitiveDataLogging)
|
|
.EnableDetailedErrors(enableDetailedErrors);
|
|
});
|
|
});
|
|
|
|
builder.Services.AddControllers(options =>
|
|
{
|
|
options.Filters.Add<AuthScopedFilter>();
|
|
});
|
|
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
builder.Services.AddSwaggerGen(c =>
|
|
{
|
|
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
|
|
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
|
|
c.IncludeXmlComments(xmlPath);
|
|
});
|
|
|
|
var app = builder.Build();
|
|
|
|
app.UseMiddleware<ExceptionHandlingMiddleware>();
|
|
|
|
// Configure the HTTP request pipeline.
|
|
if (app.Environment.IsDevelopment() || config.GetValue<bool>("UseSwagger"))
|
|
{
|
|
app.UseSwagger();
|
|
app.UseSwaggerUI();
|
|
|
|
var rewriteOptions = new RewriteOptions().AddRedirect("^$", "swagger");
|
|
app.UseRewriter(rewriteOptions);
|
|
}
|
|
|
|
app.UseHttpsRedirection();
|
|
|
|
app.UseAuthorization();
|
|
|
|
app.MapControllers();
|
|
|
|
app.Run();
|
|
}
|
|
catch(Exception ex)
|
|
{
|
|
logger.Error(ex, "Stopped program because of exception");
|
|
throw;
|
|
}
|
|
|
|
public partial class Program; |