S1312 — Logger fields should be "private static readonly"

Language
C#
Type
Code smell
Severity
Minor
Tags
convention, logging

Why is this an issue?

Regardless of the logging framework in use (Microsoft.Extension.Logging, Serilog, Log4net, NLog, …​​), logger fields should be:

This rule should be activated when Service Locator Design pattern is followed in place of Dependency Injection for logging.

The rule supports the most popular logging frameworks:

How to fix it

Make the logging field {private static readonly}.

Noncompliant code example


public Logger logger;

Compliant solution


private static readonly Logger logger;

Resources

Documentation

Articles & blog posts

↑ Back to top