Logging in .NET Core with Serilog

Quality Thought: The Best Full Stack .NET Training Institute in Hyderabad with Live Internship Program

In today's rapidly evolving tech industry, becoming proficient in Full Stack development is more essential than ever. With a myriad of technologies to learn, it's crucial to have expert guidance and hands-on experience. That’s where Quality Thought stands out as the premier choice for aspiring developers. As one of the best Full Stack .NET training institutes in Hyderabad, Quality Thought offers an industry-focused curriculum and a unique Live Internship program designed to provide students with real-world experience.

Why Choose Quality Thoughts for Full Stack .NET Training?

Comprehensive Curriculum: Covers front-end, back-end, database, cloud integration, and deployment.

Industry Experts as Trainers: Learn from professionals working on real-time .NET projects.

Live Internship Program: Work on real-time client projects and gain hands-on experience.

Career Support: Resume preparation, mock interviews, and placement assistance.

Flexible Batches: Weekend and weekday options available for working professionals and career-switchers.

Logging in .NET Core with Serilog

Logging is essential in any modern application to track behavior, identify issues, and audit events. In .NET Core, while the built-in logging framework is powerful and flexible, many developers turn to Serilog for its structured logging capabilities and rich ecosystem.

Why Serilog?

Unlike traditional loggers that produce plain text logs, Serilog is a structured logging library. This means logs are recorded as key-value pairs, making them easier to query and analyze, especially when using tools like Seq, Kibana, or Application Insights.

Setting Up Serilog in .NET Core

To get started, you can install the Serilog packages:

bash

dotnet add package Serilog.AspNetCore

dotnet add package Serilog.Sinks.Console

Then, modify Program.cs (for .NET 6+):

csharp

using Serilog;

Log.Logger = new LoggerConfiguration()

    .WriteTo.Console()

    .Enrich.FromLogContext()

    .CreateLogger();

var builder = WebApplication.CreateBuilder(args);

builder.Host.UseSerilog();

var app = builder.Build();

// your app configuration

app.Run();

Logging in Action

You can inject ILogger<T> into your services or controllers:

csharp

public class HomeController : Controller

{

    private readonly ILogger<HomeController> _logger;

    public HomeController(ILogger<HomeController> logger)

    {

        _logger = logger;

    }

    public IActionResult Index()

    {

        _logger.LogInformation("Visited the Index page at {Time}", DateTime.Now);

        return View();

    }

}

Conclusion

Serilog brings powerful, flexible, and structured logging to .NET Core applications. With various sinks and enrichment features, it enables better observability, debugging, and log analytics, making it a go-to choice for modern .NET applications. 

Read More

JWT Authentication in .NET 7

Authentication & Authorization in ASP.NET Core

Repository Pattern in .NET Core

Entity Framework Core vs ADO.NET

Visit Our "Quality Thought" Training Institute in Hyderabad.

Comments

Popular posts from this blog

JWT Authentication in .NET 7

Building REST APIs with ASP.NET Core

Introduction to .NET Core for Full Stack Developers