Regardless of the logging framework in use (Microsoft.Extension.Logging, Serilog, Log4net, NLog, …), logger fields should be:
static will ensure that the lifetime of the object doesn’t depend on the lifetime
of the instance of the enclosing type. readonly will prevent modifications to the reference of the logger. This ensures
that the reference to the logger remains consistent and doesn’t get accidentally reassigned during the lifetime of the enclosing type. 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:
Make the logging field {private static readonly}.
public Logger logger;
private static readonly Logger logger;
static class members readonly
keyword