A minimal ASP.NET Core Web API project demonstrating how to implement custom middleware for logging HTTP requests and handling runtime exceptions.
- β Custom middleware for request logging
- β Global exception handling in middleware
- β Logs saved to file using Serilog (optional)
- β Clean and modular codebase
- β Supports Swagger UI for testing
- Backend: ASP.NET Core Web API
- Language: C#
- Middleware: Custom-built
- Logging: Console and File (via Serilog)
- Docs: Swagger (OpenAPI)
RequestLoggingMiddlewareApp/ βββ Controllers/ β βββ WeatherController.cs βββ Middlewares/ β βββ RequestLoggingMiddleware.cs β βββ RequestLoggingMiddlewareExtensions.cs βββ Logs/ β βββ requests.txt βββ Program.cs βββ appsettings.json βββ README.md
git clone https://github.com/yourusername/RequestLoggingMiddlewareApp.git
cd RequestLoggingMiddlewareApp
2. Restore packages
bash
dotnet restore
3. Run the app
bash
Copy
Edit
dotnet run
4. Open Swagger
Go to https://localhost:5001/swagger in your browser.
π§ͺ Sample Endpoint
http
GET /weather/today
π Sample Output:
json
Copy
Edit
{
"error": "An error occurred: Cannot divide by zero.",
"details": "Attempted to divide by zero."
}
π Logs Preview
txt
[2025-07-29T10:00:01Z] Request: GET /weather/today
[2025-07-29T10:00:01Z] Error: System.DivideByZeroException: Attempted to divide by zero.
π Customize
You can extend the middleware to:
Log response status codes
Write to database or cloud
Enforce API keys or rate limiting
π License
This project is licensed under the MIT License.
βοΈ Author
Md Hasanuzzaman Rony
LinkedIn β’ GitHub
β Star this repo
If you found this useful, give it a β on GitHub β it helps others find it too!
---
π§ **Next Step for You:**
- Replace all `yourusername` or profile links with your actual GitHub username.
- Add a `LICENSE` file (MIT or others) if publishing publicly.
- If you want, I can help create this as a `.zip` or upload-ready GitHub repo for you.
Let me know if you want a version tailored for .NET 6 or 7, or with NLog instead of Serilog.