Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create rule S6670: Trace.Write and Trace.WriteLine should not be used #2538

Merged
merged 4 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Loading