rspec/rules/S1312/rule-dotnet.adoc
github-actions[bot] 68e9c93d57
Create rule S1312: Add C# and VB.NET (#2488)
* Add csharp to rule S1312

* First specification

* Fix ascii

* Improve description

* Address comments

* Add the list of supported frameworks

* Add links to supported frameworks

* Small fixes

* Add missing closing brace

* Add tag logging

* Remove VB.NET

* Fix code snippets

---------

Co-authored-by: cristian-ambrosini-sonarsource <cristian-ambrosini-sonarsource@users.noreply.github.com>
Co-authored-by: Cristian Ambrosini <cristian.ambrosini@sonarsource.com>
Co-authored-by: Gregory Paidis <gregory.paidis@sonarsource.com>
Co-authored-by: Gregory Paidis <115458417+gregory-paidis-sonarsource@users.noreply.github.com>
2024-03-12 13:59:26 +01:00

40 lines
1.9 KiB
Plaintext
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

== Why is this an issue?
Regardless of the logging framework in use (Microsoft.Extension.Logging, Serilog, Log4net, NLog, ...), logger fields should be:
* *{private}*: this restricts access to the logger from outside the enclosing type (class, struct, record...). Using any other access modifier would allow other types to use the logger to log messages in the type where it's defined.
* *{static}*: making the logger field `{static}` will ensure that the lifetime of the object doesn't depend on the lifetime of the instance of the enclosing type.
* *{readonly}*: marking the field as `{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 https://en.wikipedia.org/wiki/Service_locator_pattern[Service Locator Design pattern] is followed in place of https://en.wikipedia.org/wiki/Dependency_injection[Dependency Injection] for logging.
The rule supports the most popular logging frameworks:
* https://www.nuget.org/packages/Microsoft.Extensions.Logging[Microsoft.Extensions.Logging]
* https://www.nuget.org/packages/Serilog[Serilog]
* https://www.nuget.org/packages/Castle.Core[Castle.Core]
* https://www.nuget.org/packages/NLog[NLog]
* https://www.nuget.org/packages/log4net[log4net]
=== Noncompliant code example
include::{language}/noncompliant.adoc[]
=== Compliant solution
include::{language}/compliant.adoc[]
== Resources
=== Documentation
include::{language}/resources.adoc[]
* Wikipedia - https://en.wikipedia.org/wiki/Service_locator_pattern[Service locator pattern]
* Wikipedia - https://en.wikipedia.org/wiki/Dependency_injection[Dependency injection]
=== Articles & blog posts
* https://stackoverflow.com/questions/968132/c-sharp-private-static-and-readonly[C# `private`, `static`, and `readonly`]
include::rspecator-dotnet.adoc[]