From fcafb3e6f0f14daf5c1f07a6b93593e0c2dac18e Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Thu, 14 Mar 2024 17:44:12 +0100 Subject: [PATCH] Create rule S6670: Trace.Write and Trace.WriteLine should not be used (#2538) --- rules/S6670/csharp/metadata.json | 2 ++ rules/S6670/csharp/rule.adoc | 31 +++++++++++++++++++++++++++++++ rules/S6670/metadata.json | 20 ++++++++++++++++++++ rules/S6670/resources.adoc | 14 ++++++++++++++ rules/S6670/rspecator.adoc | 15 +++++++++++++++ rules/S6670/why-dotnet.adoc | 4 ++++ 6 files changed, 86 insertions(+) create mode 100644 rules/S6670/csharp/metadata.json create mode 100644 rules/S6670/csharp/rule.adoc create mode 100644 rules/S6670/metadata.json create mode 100644 rules/S6670/resources.adoc create mode 100644 rules/S6670/rspecator.adoc create mode 100644 rules/S6670/why-dotnet.adoc diff --git a/rules/S6670/csharp/metadata.json b/rules/S6670/csharp/metadata.json new file mode 100644 index 00000000000..7a73a41bfdf --- /dev/null +++ b/rules/S6670/csharp/metadata.json @@ -0,0 +1,2 @@ +{ +} \ No newline at end of file diff --git a/rules/S6670/csharp/rule.adoc b/rules/S6670/csharp/rule.adoc new file mode 100644 index 00000000000..6dd016ee209 --- /dev/null +++ b/rules/S6670/csharp/rule.adoc @@ -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[] \ No newline at end of file diff --git a/rules/S6670/metadata.json b/rules/S6670/metadata.json new file mode 100644 index 00000000000..7f53aecbcae --- /dev/null +++ b/rules/S6670/metadata.json @@ -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" +} diff --git a/rules/S6670/resources.adoc b/rules/S6670/resources.adoc new file mode 100644 index 00000000000..c929a45953e --- /dev/null +++ b/rules/S6670/resources.adoc @@ -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()] \ No newline at end of file diff --git a/rules/S6670/rspecator.adoc b/rules/S6670/rspecator.adoc new file mode 100644 index 00000000000..ae032effd50 --- /dev/null +++ b/rules/S6670/rspecator.adoc @@ -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[] \ No newline at end of file diff --git a/rules/S6670/why-dotnet.adoc b/rules/S6670/why-dotnet.adoc new file mode 100644 index 00000000000..5574eaaa2c1 --- /dev/null +++ b/rules/S6670/why-dotnet.adoc @@ -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. \ No newline at end of file