Skip to content

Commit

Permalink
Create rule S6670: Trace.Write and Trace.WriteLine should not be used (
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] authored Mar 14, 2024
1 parent 460fc9c commit fcafb3e
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 0 deletions.
2 changes: 2 additions & 0 deletions rules/S6670/csharp/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
31 changes: 31 additions & 0 deletions rules/S6670/csharp/rule.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
include::../why-dotnet.adoc[]

[source,csharp,diff-id=1,diff-type=noncompliant]
----
try
{
var message = RetrieveMessage();
Trace.Write($"Message received: {message}"); // Noncompliant
}
catch (Exception ex)
{
Trace.WriteLine(ex); // Noncompliant
}
----

[source,csharp,diff-id=1,diff-type=compliant]
----
try
{
var message = RetrieveMessage();
Trace.TraceInformation($"Message received: {message}");
}
catch (Exception ex)
{
Trace.TraceError(ex);
}
----

include::../resources.adoc[]

include::../rspecator.adoc[]
20 changes: 20 additions & 0 deletions rules/S6670/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"title": "\"Trace.Write\" and \"Trace.WriteLine\" should not be used",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "5min"
},
"tags": [
"logging"
],
"extra": {
},
"defaultSeverity": "Minor",
"ruleSpecification": "RSPEC-6670",
"sqKey": "S6670",
"scope": "Main",
"defaultQualityProfiles": ["Sonar way"],
"quickfix": "targeted"
}
14 changes: 14 additions & 0 deletions rules/S6670/resources.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
== Resources

=== Documentation

* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.trace.traceerror[Trace.TraceError Method]
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.trace.traceinformation[Trace.TraceInformation Method]
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.trace.tracewarning[Trace.TraceWarning Method]
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.trace.write[Trace.Write Method]
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.trace.writeline[Trace.WriteLine Method]
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.tracelistener.traceevent[TraceListener.TraceEvent Method]

=== Articles & blog posts

* Stackoverflow - https://stackoverflow.com/q/26350620[Difference between Trace.Write() and Trace.TraceInformation()]
15 changes: 15 additions & 0 deletions rules/S6670/rspecator.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
ifdef::env-github,rspecator-view[]

'''
== Implementation Specification
(visible only on this page)

=== Message

Avoid using [Trace.Write/Trace.WriteLine], use instead methods that specify the trace event type.

'''
== Comments And Links
(visible only on this page)

endif::env-github,rspecator-view[]
4 changes: 4 additions & 0 deletions rules/S6670/why-dotnet.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
== Why is this an issue?

https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.trace.write[Trace.Write] and https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.trace.writeline[Trace.WriteLine] methods are writing to the underlying output stream directly, bypassing the trace formatting and filtering performed by https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.tracelistener.traceevent[TraceListener.TraceEvent] implementations.
It is preferred to use https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.trace.traceerror[Trace.TraceError], https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.trace.tracewarning[Trace.TraceWarning] and https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.trace.traceinformation[Trace.TraceInformation] methods instead because they call the https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.tracelistener.traceevent[TraceEvent method] which filters the trace output according to the https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.traceeventtype[TraceEventType] (Error, Warning or Information) and enhance the output with additional information.

0 comments on commit fcafb3e

Please sign in to comment.