Logging in .NET Core using Serilog/NLog
IHUB Talent – The Best Full Stack .NET Training Institute in Hyderabad with Live Internship Program
If you're planning to build a rewarding career in software development, then IHUB is the perfect place to begin. Recognized as the best Full Stack .NET training institute in Hyderabad, IHUB offers a complete job-oriented course with a live intensive internship program, guided by real-time industry experts.
Whether you're a graduate, postgraduate, someone with an educational gap, or planning a job domain change, IHUB’s Full Stack .NET course is designed to make you job-ready in just a few months. The program is structured for freshers and working professionals alike, offering hands-on experience and placement support.
🔹 Why Choose IHUB for Full Stack .NET Training?
Expert Faculty: Learn from certified trainers with real-world industry experience.
Live Intensive Internship: Work on actual projects under the guidance of professionals.
Placement Assistance: Resume preparation, mock interviews, and job referrals.
Suitable for All Backgrounds: Freshers, students with gaps, or those changing job domains.
Flexible Learning: Online and offline training options available.
Project-Based Training: Gain hands-on experience by developing end-to-end real-time projects.
Logging in .NET Core Using Serilog/NLog
In modern .NET Core applications, effective logging is essential for monitoring, diagnosing, and maintaining healthy software systems. Two popular and powerful logging libraries in the .NET ecosystem are Serilog and NLog. Both offer rich features, flexibility, and performance, making them top choices for developers aiming to build maintainable and observable applications.
Why Use Serilog or NLog?
While .NET Core comes with built-in logging via Microsoft.Extensions.Logging, it lacks advanced features such as structured logging, log file management, or third-party integrations. Serilog and NLog fill this gap with powerful features like:
Asynchronous logging
File/database/console/sink support
Log filtering and formatting
Structured (JSON) logging for analytics
Using Serilog in .NET Core
Install Packages
bash
dotnet add package Serilog.AspNetCore
dotnet add package Serilog.Sinks.File
Configure in Program.cs
csharp
public class Program
{
public static void Main(string[] args)
{
Log.Logger = new LoggerConfiguration()
.WriteTo.Console()
.WriteTo.File("logs/log.txt", rollingInterval: RollingInterval.Day)
.Enrich.FromLogContext()
.CreateLogger();
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseSerilog() // Integrate Serilog
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
Usage
csharp
private readonly ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
_logger.LogInformation("HomeController initialized.");
}
Using NLog in .NET Core
Install Packages
bash
dotnet add package NLog.Web.AspNetCore
Add nlog.config
Create an XML configuration file for sinks and rules:
xml
<nlog>
<targets>
<target name="file" xsi:type="File" fileName="logs/nlog.txt" />
</targets>
<rules>
<logger name="*" minlevel="Info" writeTo="file" />
</rules>
</nlog>
Configure in Program.cs
csharp
public class Program
{
public static void Main(string[] args)
{
var logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
try
{
CreateHostBuilder(args).Build().Run();
}
catch (Exception ex)
{
logger.Error(ex, "Stopped program due to exception");
throw;
}
finally
{
NLog.LogManager.Shutdown();
}
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
})
.UseNLog(); // Integrate NLog
}
Final Thoughts
Both Serilog and NLog are mature and production-ready. Serilog is preferred for structured logging and extensibility, while NLog is great for performance-focused file and database logging. Choosing between them depends on project needs, but either offers a huge improvement over default logging mechanisms.
Read More
LINQ Queries in C# – Basics and Examples
CRUD Operations with Entity Framework Core
Middleware in ASP.NET Core Explained
Dependency Injection in ASP.NET Core
Visit Our I-HUB Talent Training Institute Hyderabad
Comments
Post a Comment