Sitemap
.Net Programming

Explore the .Net with the latest in C# from basic to advanced, including .Net versions 9, 8, 6, 5, Core 3.1, .Net Framework, ASP.NET Core, MVC, design patterns, OOPS, and SOLID principles. Get top tutorials, best practices, and hands-on code examples on GitHub.

Member-only story

Mastering C# Logging: Essential Tips from Basics to Advanced

Explore expert tips on C# logging with practical examples, from selecting the right framework to implementing advanced features like structured logging and log rotation. Perfect for developers looking to enhance application reliability and maintainability.

3 min readApr 24, 2024

--

1. Choose the Right Logging Framework

Many logging frameworks like NLog, Serilog, and log4net are available for .Net Applications. I recommend using Serilog as it is most commonly used because of its structured logging capabilities.

// Using Serilog
Log.Logger = new LoggerConfiguration()
.WriteTo.Console()
.WriteTo.File("logs/myapp.txt", rollingInterval: RollingInterval.Day)
.CreateLogger();

Log.Information("This is a Serilog example.");

2. Use Built-in .NET Core Logging

Even Microsoft provides a built-in logging framework using Microsoft.Extensions.Logging namespace as shown below.

// Using Microsoft.Extensions.Logging
ILogger<YourClass> _logger = serviceProvider.GetRequiredService<ILogger<YourClass>>();
_logger.LogInformation("Logging…

--

--

.Net Programming
.Net Programming

Published in .Net Programming

Explore the .Net with the latest in C# from basic to advanced, including .Net versions 9, 8, 6, 5, Core 3.1, .Net Framework, ASP.NET Core, MVC, design patterns, OOPS, and SOLID principles. Get top tutorials, best practices, and hands-on code examples on GitHub.

Sukhpinder Singh | C# .Net
Sukhpinder Singh | C# .Net

Written by Sukhpinder Singh | C# .Net

C# .Net developer 👨‍💻 who's 100% convinced my bugs are funnier than yours. 🐛💥 #BugLife Pubs: https://medium.com/c-sharp-programming

No responses yet