Asp.Net-Core-Inventory-Order-Management-System
Asp.Net-Core-Inventory-Order-Management-System copied to clipboard
Improve Program.cs startup: safer log folder creation, CORS policy, and middleware order
This pull request includes several improvements to the ASPNET/Program.cs startup file for better robustness, safety, and maintainability:
-
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.
-
Serilog Configuration
- Configured rolling file logging to
wwwroot/app_data/logs/app.logfor better log management. - Properly closes and flushes logs on application shutdown.
- Configured rolling file logging to
-
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.
-
Middleware Order Adjustment
- Moved
GlobalApiExceptionHandlerMiddlewareafter authentication and authorization middleware. - Ensures that auth exceptions are properly captured and logged.
- Moved
-
Safe Application Startup
- Wrapped
app.Run()in try-catch with Serilog fatal logging. - Prevents unhandled exceptions from crashing the application without logging.
- Wrapped
These changes improve application stability, provide better error tracking, and make the startup process more predictable, especially in production environments.