Logging in .NET Core - Beginner’s Friendly Guide
🌟 Introduction
Hey there 👋, have you ever felt lost when your app suddenly throws an error and you don’t know why?
Well, that’s where Logging in .NET Core comes in.
Logging is like a diary 📖 your application keeps. It records events, warnings, and errors so that you, the developer, can trace what really happened. Without logging, debugging is like searching for a needle in a haystack.
The good news? Logging in .NET Core is simple, powerful, and fun to learn 🚀.
📚 What you are going to learn in this lesson
✔️ What Logging in .NET Core means and why it matters.
✔️ How to set up basic logging.
✔️ A simple code example with explanation + output.
✔️ A real-life scenario where logging saves the day.
🧐 What is Logging in .NET Core?
Simply put, Logging in .NET Core is a way for your app to tell you what it’s doing behind the scenes.
- If everything works fine, it writes informational logs.
- If something unusual happens, it writes warnings.
- If something breaks, it writes error logs ❌.
These logs help you debug, monitor performance, and maintain a healthy application. Think of it as your app whispering:
“Hey buddy, here’s what just happened!” 😅
🔧 Code Example
Let’s create a simple console app that shows how Logging in .NET Core works.
<p class="random-text">Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>using System;
using Microsoft.Extensions.Logging;
class Program
{
static void Main(string[] args)
{
// Create a logger factory
using var loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddConsole(); // Logs will appear in the console
});
// Create a logger
ILogger logger = loggerFactory.CreateLogger<Program>();
logger.LogInformation("App started 🚀");
logger.LogWarning("This is just a warning ⚠️");
logger.LogError("Oops! Something went wrong ❌");
}
}
🔍 Explanation
LoggerFactory.Create→ Creates a factory to build loggers.builder.AddConsole()→ Sends logs to the console (so you can see them while running).logger.LogInformation→ Logs a general info message.logger.LogWarning→ Logs a warning.logger.LogError→ Logs an error.
🖥️ Expected Output
When you run this program, you’ll see something like this in the console:
info: Program[0]
App started 🚀
warn: Program[0]
This is just a warning ⚠️
fail: Program[0]
Oops! Something went wrong ❌
Cool, right? Now you know exactly what your app is doing.
🎯 Real-life Scenario
Imagine you built a web app and suddenly users complain:
“The checkout button doesn’t work!”
Without logging, you’d have no idea what went wrong. But with Logging in .NET Core, you could check the logs and see:
- Did the app crash?
- Was the database unreachable?
- Was it just a missing configuration?
Logging gives you clues like a detective 🕵️, making bug-hunting faster and easier.
💡 Quick Tips
✨ Always log errors with enough detail to debug later.
✨ Don’t log sensitive info like passwords.
✨ Use different log levels (Info, Warning, Error) wisely.
👉 Next what?
Woohoo 🎉 You just learned the basics of Logging in .NET Core.
How do you feel? Pretty cool, right? 🚀
But don’t stop here.
In the next chapter, you will learn Understand MBC in .NET Core 🖥️.
It’s going to be fun, so stay tuned!
