Logging Exceptions in C#: an overview
A log is a file of time-stamped lines recording what an app did and what went wrong, so you can find out later. Apps log to plain text files because they're fast, simple, and keep working when everything else fails.
Journeyman · C# · Updated
After this lesson you can
- Explain why apps log errors to text files, and what a useful log entry holds
Key takeaways
- A log records what an app did and what went wrong as it happens, so you can find out later, when nobody was watching.
- A useful log entry says when (the date and time), what (for an exception, its type and message) and where (which file, record or user).
- Keep an error log for exceptions and an application log for ordinary events, such as starting up and finishing a job.
- Apps log to plain text files because adding a line is fast, needs no setup, and keeps working when the database is down.
When your app runs on your own computer, an exception shows up in front of you. When it runs on a server, or on a hundred users' computers, nobody is watching. A user says "it broke on Tuesday", and you need to know what happened on Tuesday.
That's what a log is for: a record of what the app did and what went wrong, written as it happens, so you can read it later.
A log entry
Each event becomes one line, a log entry:
2026-03-14 09:15:02 Started
2026-03-14 09:15:07 Loaded 128 students from student_grades.csv
2026-03-14 09:16:41 FileNotFoundException: Could not find file 'C:\GradeBook\honor_roll_template.csv'.
A useful entry answers three questions:
- When? The date and time, so you can match it to "it broke on Tuesday at 9."
- What? A message that makes sense on its own, months later. For an exception, its type and message.
- Where? Which file, record or user was involved:
student_grades.csv, not just "a file".
Most apps keep at least two logs: an error log for exceptions, read when something goes wrong, and an application log for ordinary events, such as starting up and finishing a job.
Why plain text files?
You could store log entries in a database, but most apps log to text files:
| Reason | Why it matters |
|---|---|
| Fast | Adding a line to the end of a file is quick and simple, so logging costs the app very little. |
| Simple | No database, no connection string, nothing to set up. |
| Keeps working | If the database goes down, that's exactly when you need the log, so the log mustn't depend on the database. |
| Easy to read | Open it in Notepad or VS Code, search it, send it to a colleague. |
| Easy to tidy | A new file each day keeps files small, and old days can be deleted or archived. |
What you'll build
Real .NET apps use a logging library: ASP.NET Core has one built in (ILogger), and Serilog and NLog are popular choices. They all work on the same idea, and the best way to understand it is to build a small one yourself. In the next lessons you'll write a LogService that:
- writes to a folder you choose, such as
logs\errorsorlogs\application, - starts a new file each day, named for the date, such as
20260314.log, - appends one time-stamped line per entry.
Check yourself
1 question to try on the lesson page.
So what
A log is your app's diary. Write down when, what and where, one line at a time, and the next time someone says "it broke on Tuesday", you'll be able to find out why.