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.