Compare commits

...

14 Commits

Author SHA1 Message Date
Gregory Paidis
6612bf124d Revert "Remove VB.NET"
This reverts commit 644d73ed16a202002aa21b6f5849f95db4ff6ecc.
2024-03-12 12:07:32 +01:00
Gregory Paidis
b5cdf8c3e3 Fix code snippets 2024-03-12 11:25:52 +01:00
Gregory Paidis
704b46ff86
Merge branch 'master' into rule/S1312-add-csharp 2024-03-08 15:58:14 +01:00
Gregory Paidis
644d73ed16 Remove VB.NET 2024-03-08 15:46:31 +01:00
Gregory Paidis
3afe7c4b77 Add tag logging 2024-03-08 15:46:19 +01:00
Cristian Ambrosini
7a4b34cebc Add missing closing brace 2023-07-18 15:04:25 +02:00
Cristian Ambrosini
7724aa888d Small fixes 2023-07-18 13:55:11 +02:00
Cristian Ambrosini
262903cd6d Add links to supported frameworks 2023-07-18 13:25:43 +02:00
Cristian Ambrosini
eca917f94f Add the list of supported frameworks 2023-07-18 12:16:22 +02:00
Cristian Ambrosini
1c7d4f739c Address comments 2023-07-18 12:03:56 +02:00
Cristian Ambrosini
44cb8a6434 Improve description 2023-07-17 14:00:33 +02:00
Cristian Ambrosini
95e52a6176 Fix ascii 2023-07-14 13:22:06 +02:00
Cristian Ambrosini
3b318d3b25 First specification 2023-07-14 13:17:06 +02:00
cristian-ambrosini-sonarsource
aed4db2f9a Add csharp to rule S1312 2023-07-14 08:49:57 +00:00
19 changed files with 139 additions and 58 deletions

View File

@ -1,3 +0,0 @@
=== on 22 Nov 2013, 12:53:31 Freddy Mallet wrote:
Is implemented by \http://jira.codehaus.org/browse/SONARPLUGINS-3280 for Flex

View File

@ -0,0 +1,4 @@
[source,csharp,diff-id=1,diff-type=compliant]
----
private static readonly Logger logger;
----

View File

@ -0,0 +1,4 @@
{
"title": "Logger fields should be \"private static readonly\"",
"quickfix": "targeted"
}

View File

@ -0,0 +1,4 @@
[source,csharp,diff-id=1,diff-type=noncompliant]
----
public Logger logger;
----

View File

@ -0,0 +1,3 @@
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/access-modifiers[Access modifiers]
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/static-classes-and-static-class-members[`static` class members]
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/readonly[`readonly` keyword]

View File

@ -0,0 +1,6 @@
:language: csharp
:private: private
:static: static
:readonly: readonly
include::../rule-dotnet.adoc[]

View File

@ -22,20 +22,4 @@ public const logger:ILogger = LogUtil.getLogger(MyClass);
private static const LOG:ILogger = LogUtil.getLogger(MyClass);
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::../message.adoc[]
include::../parameters.adoc[]
'''
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]
include::../rspecator.adoc[]

View File

@ -27,20 +27,4 @@ private static final Logger LOGGER = LoggerFactory.getLogger(Foo.class);
Variables of type ``++org.apache.maven.plugin.logging.Log++`` are ignored.
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::../message.adoc[]
include::../parameters.adoc[]
'''
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]
include::../rspecator.adoc[]

View File

@ -1,8 +0,0 @@
=== Message
Make the logger "XXXX" private static [final|const] and rename it to comply with the format "{}".
Make the logger "XXXX" private static [final|const].
Rename the "XXX" logger to comply with the format "{}".

View File

@ -13,7 +13,8 @@
"constantCost": "5min"
},
"tags": [
"convention"
"convention",
"logging"
],
"extra": {
"replacementRules": [

View File

@ -1,12 +0,0 @@
=== Parameters
.format
****
_String_
----
LOG(?:GER)?
----
Regular expression used to check the logger names against.
****

View File

@ -0,0 +1,17 @@
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Make the logger "XXXX" {private} {static} {readonly}.
=== Parameters
'''
== Comments And Links
(visible only on this page)
endif::env-github,rspecator-view[]

View File

@ -0,0 +1,35 @@
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Make the logger "XXXX" private static [final|const] and rename it to comply with the format "{}".
Make the logger "XXXX" private static [final|const].
Rename the "XXX" logger to comply with the format "{}".
=== Parameters
.format
****
_String_
----
LOG(?:GER)?
----
Regular expression used to check the logger names against.
****
'''
== Comments And Links
(visible only on this page)
=== on 22 Nov 2013, 12:53:31 Freddy Mallet wrote:
Is implemented by \http://jira.codehaus.org/browse/SONARPLUGINS-3280 for Flex
endif::env-github,rspecator-view[]

View File

@ -0,0 +1,40 @@
== 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[]

View File

@ -0,0 +1,4 @@
[source,vbnet,diff-id=1,diff-type=compliant]
----
Private Shared ReadOnly logger As Logger
----

View File

@ -0,0 +1,5 @@
{
"title": "Logger fields should be \"Private Shared ReadOnly\"",
"quickfix": "targeted"
}

View File

@ -0,0 +1,4 @@
[source,vbnet,diff-id=1,diff-type=noncompliant]
----
Public logger As Logger
----

View File

@ -0,0 +1,3 @@
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/declared-elements/access-levels[Access levels]
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/modifiers/shared[`Shared` class members]
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/modifiers/readonly[`ReadOnly`]

View File

@ -0,0 +1,6 @@
:language: vbnet
:private: Private
:static: Shared
:readonly: ReadOnly
include::../rule-dotnet.adoc[]