Skip to content

A custom Serilog JSON formatter for ASP.NET Core that transforms MessageTemplate logs into clean structured JSON with exception details, request data, thread info, and organized daily log storage.

Notifications You must be signed in to change notification settings

reenapraveenap/ASP.NET-Core-Advanced-JSON-Logging-Serilog-Custom-Formatter-

Repository files navigation

Advanced Serilog Logging System in ASP.NET Core

Controller-Wise | Day-Wise | Pretty JSON Logs | Production-Ready Architecture

Overview

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

Architecture Diagram

File Structure Overview image

Technologies Used

image

Logging Flow

🔹 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:

image

🔹 3. Serilog Map Sink Routes the Log

LogFolder = "Employee" → Serilog routes log to:

image

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.

image

Key Endpoints (For Testing)

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

Why This Logging Architecture is Powerful

✔ 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.

Production Recommendations

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

How to Run Locally

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

About

A custom Serilog JSON formatter for ASP.NET Core that transforms MessageTemplate logs into clean structured JSON with exception details, request data, thread info, and organized daily log storage.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published