-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create rule S6670: Trace.Write and Trace.WriteLine should not be used (…
- Loading branch information
1 parent
460fc9c
commit fcafb3e
Showing
6 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |