Controller-Wise | Day-Wise | Pretty JSON Logs | Production-Ready Architecture
This repository showcases a highly advanced, production-level logging architecture built with ASP.NET Core + Serilog, featuring:
✔ Automatic log separation per Controller
✔ Daily time-based folders (YYYY-MM-DD)
✔ Pretty JSON Log formatting (human readable)
✔ Dynamic Serilog Map Sink routing
✔ External Log Directory support
✔ Custom Exception Middleware
✔ Enterprise-level observability & debugging
🔹 1. API Endpoint Throws Exception
Whenever an exception occurs in Controller, Service, or Repository layer → it is intercepted by ExceptionMiddleware.
🔹 2. ExceptionMiddleware Extracts Metadata
It automatically reads: Controller Name (e.g., Employee) , Action Name , Path , Exception Message , Source (class where error occurred)
And constructs a structured log object:
🔹 3. Serilog Map Sink Routes the Log
LogFolder = "Employee" → Serilog routes log to:
E:\AppLogs\CRUD_Operations
└── EmployeeRepository(dynamic handling the folder based on exception)
└── 2025-12-04
└── log.json
🔹 4. PrettyJsonFormatter Writes Log Entries
Every exception is appended to the same log.json file for the day.
Employee Controller
GET /Employee/EmployeeDetails
POST /Employee/InsertDetails
Any error here will be stored in:
EmployeeRepository(dynamic handling the folder based on exception)/YYYY-MM-DD/log.json
✔ Controller-wise separation
Keeps logs organized based on feature/domain.
✔ Day-wise folder structure
Easy to track historical logs per day.
✔ Pretty JSON formatting
Readable, structured, and ready for log ingestion tools.
✔ Middleware-driven logging
Centralized — you never write try/catch manually.
✔ Production-ready
Recommended for enterprise applications and microservices.
To make your logging more secure & scalable:
🔐 Security
Never log sensitive data (passwords, tokens, Aadhaar, etc.)
Use environment-based log levels (Error for Prod)
📂 Storage
Store logs on external storage (SSD, Azure Blob, AWS S3)
Rotate logs monthly for performance
🔍 Monitoring
Send logs to ELK, Seq, Splunk, or Azure AppInsights
Add correlation IDs for distributed tracing
Step 1: Configure log root
appsettings.json:
"LogSettings": { "ExternalLogRoot": "E:\AppLogs\CRUD_Operations\" }
Step 2: Register Middleware app.UseMiddleware();
Step 3: Run application dotnet run
Logs will be auto-generated as you trigger API errors.
🎯 What You Will Learn From This Project
✔ How to build a real enterprise-level logging system
✔ How to route logs into dynamic folders using Serilog Map
✔ How to write Pretty JSON logs with custom formatters
✔ How to build Exception Handling Middleware
✔ How to maintain logs per controller, per day
✔ How to structure logs for observability tools (ELK, Seq, Kibana)
✔ How to design scalable logging architecture for microservices
