log4net
logger naming
logging best practices
C# logging
software development

Correct way of using log4net logger naming

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

The safest way to name log4net loggers is to base them on the class or namespace that emits the log. That keeps logger names consistent, works naturally with log4net's hierarchical configuration model, and avoids the copy-paste mistakes that happen with arbitrary string names.

Use the Class Type as the Logger Identity

A common pattern is:

csharp
1using log4net;
2
3public class OrderService
4{
5    private static readonly ILog Log =
6        LogManager.GetLogger(typeof(OrderService));
7
8    public void Process()
9    {
10        Log.Info("Processing order");
11    }
12}

This gives the logger the fully qualified class name, which fits log4net's hierarchy naturally.

Why Type-Based Names Work Well

log4net treats logger names hierarchically. If your class is MyApp.Services.OrderService, it can inherit configuration from:

  • 'MyApp'
  • 'MyApp.Services'
  • 'MyApp.Services.OrderService'

That makes filtering and level overrides easy. You can turn on DEBUG for one namespace without affecting the whole application.

Avoid Random String Names

This is weaker:

csharp
private static readonly ILog Log =
    LogManager.GetLogger("GeneralLogger");

It loses useful structure. A generic string name does not tell you which code produced the message, and it does not align with namespace-based configuration.

Hard-coded string names also create maintenance problems:

  • Renaming a class does not rename the logger
  • Copy-paste can leave the wrong logger name in a file
  • Configuration becomes less intuitive

Namespace-Level Naming Can Also Be Useful

Sometimes you want a logger for a broader subsystem rather than a specific class:

csharp
private static readonly ILog Log =
    LogManager.GetLogger("MyApp.Payments");

That is valid when the logger is intentionally tied to a feature area, module, or integration boundary. The key is that the name should still reflect a meaningful part of the application's structure.

Example Configuration Benefit

With hierarchical names, configuration is straightforward:

xml
1<log4net>
2  <logger name="MyApp.Services">
3    <level value="DEBUG" />
4  </logger>
5
6  <root>
7    <level value="INFO" />
8  </root>
9</log4net>

This lets service-layer code log more verbosely while the rest of the app stays at INFO.

That same hierarchy helps when reading logs in production. A logger name such as MyApp.Services.OrderService immediately tells you both the subsystem and the class, which is much more actionable than a flat custom label.

Keep Naming Consistent Across the Codebase

The most important rule is consistency. A mixed codebase with some type-based names, some subsystem strings, and some generic labels becomes hard to reason about.

A practical guideline is:

  • Default to GetLogger(typeof(CurrentClass))
  • Use explicit namespace or subsystem strings only when that scope is intentional

That gives you clarity without overcomplicating the logging strategy.

Teams often wrap this pattern in a base class or helper, but the principle stays the same: the logger name should follow the code structure closely enough that a log line immediately tells you where it came from.

Common Pitfalls

  • Using generic names like "Logger" or "GeneralLogger".
  • Copy-pasting a hard-coded logger string from another class.
  • Mixing naming schemes without deciding what each scheme means.
  • Expecting logger names to stay meaningful after refactors if they are not derived from the type.

Summary

  • In log4net, logger names should usually follow the emitting class or namespace.
  • 'LogManager.GetLogger(typeof(CurrentClass)) is the standard safe default.'
  • Hierarchical names make log filtering and configuration easier.
  • Explicit subsystem names are fine when they are deliberate and meaningful.
  • Arbitrary string names usually create more maintenance cost than value.

Good logger naming is mostly about preserving structure, not inventing labels. That is why class and namespace names usually win.


Course illustration
Course illustration

All Rights Reserved.