Tutorials ASP.NET Core Complete Tutorial (ShopNest)

Logging in ASP.NET Core — ILogger, Serilog, NLog

Learn Logging in ASP.NET Core — ILogger, Serilog, NLog in our free ASP.NET Core Complete Tutorial (ShopNest) series. Step-by-step explanations, examples, and interview tips on Toolliyo Academy.

On this page
Logging in ASP.NET Core — ILogger, Serilog, NLog — ShopNest
Article 27 of 75 · Module 3: Dependency Injection & Middleware · ShopNest Production E-Commerce Monitoring
Target keyword: logging asp.net core serilog · Read time: ~29 min · .NET: 8 / 9 · Project: ShopNest Production E-Commerce Monitoring

Introduction

When ShopNest checkout fails at 2 AM, logs tell you why. ASP.NET Core ships with ILogger<T>; production apps add Serilog for structured logs to files, Seq, or Application Insights.

After this article you will

  • Use log levels appropriately
  • Write structured logs with message templates
  • Configure Serilog sinks and enrichers
  • Add correlation IDs per request
  • Mask sensitive data in log output

Prerequisites

Concept deep-dive

LevelUse when
Trace/DebugLocal debugging only
InformationNormal flow — order placed, payment started
WarningRecoverable — retry, validation fail
ErrorException, payment gateway down
CriticalApp unusable — DB unreachable
// Structured — DO NOT string interpolate for production analytics
_logger.LogInformation("Order {OrderId} paid {Amount} by {UserId}",
    order.Id, order.Total, userId);

// Serilog Program.cs
Log.Logger = new LoggerConfiguration()
    .ReadFrom.Configuration(builder.Configuration)
    .Enrich.FromLogContext()
    .Enrich.WithProperty("App", "ShopNest")
    .WriteTo.Console()
    .WriteTo.File("logs/shopnest-.txt", rollingInterval: RollingInterval.Day)
    .CreateLogger();
builder.Host.UseSerilog();

Correlation ID: middleware sets TraceIdentifier or custom header — flows through all logs for one request.

Masking: never log full card numbers, passwords, or JWT tokens.

Hands-on — ShopNest Production E-Commerce Monitoring

  1. Add Serilog NuGet + appsettings Serilog section.
  2. CorrelationIdMiddleware pushes ID into LogContext.
  3. PaymentService logs Information on success, Error on gateway exception.
  4. Filter appsettings: Microsoft.EntityFrameworkCore at Warning in Production.

Common errors & best practices

  • LogInformation for every loop iteration — noise and cost.
  • String interpolation in logs — loses structured properties.
  • Sync file writes under load — Serilog async sink helps.

Interview questions

Q: Structured logging benefit?
A: Query logs by OrderId, UserId in Seq/App Insights — not grep plain text.

Q: Serilog vs built-in?
A: Built-in abstracts providers; Serilog is rich sink/enricher ecosystem.

Q: Correlation ID purpose?
A: Tie all log lines from one HTTP request together in distributed traces.

Summary

  • ILogger with structured templates is the baseline
  • Serilog sinks feed files, Seq, Application Insights
  • Correlation IDs essential for production debugging
  • Mask PCI/PII — never log secrets

Previous: Filters
Next: Error Handling and Exception Management

FAQ

NLog instead of Serilog?

Both work — Serilog more common in .NET Core tutorials and microservices.

Log EF SQL in production?

Usually Warning only — Information SQL logs are verbose and costly.

Questions on this lesson 0

Sign in to ask a question or upvote helpful answers.

No questions yet — be the first to ask!

ASP.NET Core Complete Tutorial (ShopNest)
Course syllabus
Module 1: Foundations
Module 2: Entity Framework Core
Module 3: Dependency Injection & Middleware
Module 4: Authentication & Security
Module 5: Web API
Module 6: Advanced Architecture
Module 7: Testing
Module 8: Deployment & DevOps
Module 9: Real-World Projects
Module 10: Advanced Topics
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details