rspec/rules/S6672/rule.adoc
Costin Zaharia 70bdb31f51
Logging rules: add "how to fix" section (#3969)
* S6674: add how to fix section

* S6677: add how to fix section

* S2629: add how to fix section

* S6670: add how to fix section

* S1312: add how to fix section

* S6672: add how to fix section

* S6670: add compliant/noncompliant headers

* S6669: add how to fix section

* S6668: add how to fix section

* S2629: remove method definition

* S6669: update formatting

* S6670: add links

* Fix formatting
2024-06-05 10:39:56 +02:00

31 lines
2.0 KiB
Plaintext

== Why is this an issue?
In most logging frameworks, it's good practice to set the logger name to match its enclosing type, as enforced by S3416.
Logging frameworks can define or use https://learn.microsoft.com/en-us/dotnet/standard/generics/interfaces[Generic interfaces] for the logger, such as https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.logging.ilogger-1[`ILogger<TCategoryName>`].
The use of a logger of a generic type parameter `A` (e.g. `ILogger<A>`) in a type different than `A`, say `B`, goes against the convention.
Because the instance of type `A` would log with a logger named after `B`, log items would appear as if they were logged by `B` instead, resulting in confusion and logging misconfiguration:
* overriding defaults for the logger named after `A` would not take effect for entries logged in the type `A`
* fine-graned logging configuration would not be possible, since there would be no way to distinguish entries logged in the type `A` from entries logged in the type `B`
Further details and examples are provided in S3416.
This rule specifically targets the generic logging interface https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.logging.ilogger-1[`ILogger<TCategoryName>` Interface] defined by https://learn.microsoft.com/en-us/dotnet/core/extensions/logging[Microsoft Extensions Logging].
== How to fix it
include::{language}/how-code-example.adoc[]
== Resources
=== Documentation
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/core/diagnostics/logging-tracing[.NET logging and tracing]
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/standard/generics/interfaces[Generic interface]
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.logging.ilogger-1[`ILogger<TCategoryName>` Interface]
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/core/extensions/logging?tabs=command-line#log-category[Logging in C# and .NET - Log category]
include::rspecator.adoc[]