Asp.Net-Core-Inventory-Order-Management-System icon indicating copy to clipboard operation
Asp.Net-Core-Inventory-Order-Management-System copied to clipboard

Improve Program.cs startup: safer log folder creation, CORS policy, and middleware order

Open ZohaiAli opened this issue 5 months ago • 0 comments

This pull request includes several improvements to the ASPNET/Program.cs startup file for better robustness, safety, and maintainability:

  1. Safe Logs Folder Creation

    • Wrapped log folder creation in a try-catch block to prevent crashes in environments with restricted permissions.
    • Ensures the Serilog logs folder is always safely initialized.
  2. Serilog Configuration

    • Configured rolling file logging to wwwroot/app_data/logs/app.log for better log management.
    • Properly closes and flushes logs on application shutdown.
  3. CORS Policy

    • Added an explicit "AllowAll" CORS policy.
    • Prevents runtime errors due to missing default CORS policy and ensures API can be called from frontend apps.
  4. Middleware Order Adjustment

    • Moved GlobalApiExceptionHandlerMiddleware after authentication and authorization middleware.
    • Ensures that auth exceptions are properly captured and logged.
  5. Safe Application Startup

    • Wrapped app.Run() in try-catch with Serilog fatal logging.
    • Prevents unhandled exceptions from crashing the application without logging.

These changes improve application stability, provide better error tracking, and make the startup process more predictable, especially in production environments.

ZohaiAli avatar Oct 14 '25 09:10 ZohaiAli