File Logger in C#: A Complete Guide

In this post, we’ll explore simple yet practical examples of building a file logger in C#. You’ll learn how to write log entries to a file, organize messages with timestamps and log levels, and structure your code in a reusable way and all without relying on external libraries. By the end, you’ll have a solid foundation for creating your own logging utilities or enhancing them with more advanced features.

File Logger in C#: A Complete Guide

Getting Started

Logging is one of the most essential components of any application, regardless of its size or complexity. Whether you're troubleshooting unexpected behavior, monitoring system performance, or keeping an audit trail of user actions, a reliable logging system can save countless hours of debugging and analysis. In C#, implementing a basic file logger is both straightforward and highly effective, making it a perfect starting point for developers who want to understand how logging works under the hood.

What a File Logger

A file logger is a component in software (or sometimes hardware) that records information into a file so it can be reviewed later. It’s commonly used for debugging, monitoring, auditing, or tracking application behavior.

What a File Logger Does

  1. Writes logs to a file (e.g., app.log, error.log)
  2. Stores messages such as:
    • Errors
    • Warnings
    • System events
    • Debug messages
    • User actions
  3. Keeps historical records so developers or administrators can analyze issues after they happen.

Where File Loggers Are Used

  • Web servers
  • Applications
  • Operating systems (system logs)
  • Embedded systems or IoT devices

Basic File Logger Examples in C#

Creating a basic file logger in C# is a simple yet powerful way to improve the reliability and maintainability of your applications. By capturing important events, errors, and diagnostic information, logs make it easier to understand what your software is doing and why. The examples in this post demonstrate how easily you can build a custom logging solution using only core .NET features, no external dependencies required.

Simple Logger Class
using System;
using System.IO;
public static class FileLogger
{
  private static readonly string logFilePath = "app.log";
  public static void Log(string message)
  {
    string logEntry = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss} - {message}";
    File.AppendAllText(logFilePath, logEntry + Environment.NewLine);
  }
}
Using the Logger
class Program
{
  static void Main(string[] args)
  {
    FileLogger.Log("Application started.");
    FileLogger.Log("Performing some operation...");
    FileLogger.Log("Operation completed successfully.");
  }
}
Example with Log Levels
using System;
using System.IO;
public static class FileLogger
{
  private static readonly string logFilePath = "app.log";
  public static void Log(string level, string message)
  {
    string logEntry = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss} [{level}] {message}";
    File.AppendAllText(logFilePath, logEntry + Environment.NewLine);
  }
  public static void Info(string msg)  => Log("INFO", msg);
  public static void Error(string msg) => Log("ERROR", msg);
  public static void Debug(string msg) => Log("DEBUG", msg);
}

Popular File Logger Libraries for C#

Here are some well-known file logger libraries for C#/.NET, ranging from lightweight to full-featured logging frameworks:

  1. NLog: One of the most widely used logging libraries in .NET.
  2. Serilog: A modern, structured logging library.
  3. log4net: Apache’s long-standing logging framework for .NET.
  4. Microsoft.Extensions.Logging: The built-in .NET logging framework.
  5. Loupe (Gibraltar Software): A more advanced logging and monitoring tool.
  6. Essential Diagnostics: Enhances System.Diagnostics with better configuration options.
  7. Elmah (with file logging extensions): Primarily for web app error logging.

Summary

With this foundation, you can extend your logger to include features like asynchronous writing, rolling log files, log levels, or integration with larger logging frameworks. Whether you're building a small utility or a large-scale system, a solid logging strategy is an investment that pays off in clearer insights, faster debugging, and smoother application performance.

Thanks

Kailash Chandra Behera

I am an IT professional with over 13 years of experience in the full software development life cycle for Windows, services, and web-based applications using Microsoft .NET technologies.

Previous Post Next Post

نموذج الاتصال