rspec/rules/S6670/csharp/rule.adoc

39 lines
955 B
Plaintext
Raw Normal View History

include::../why-dotnet.adoc[]
== How to fix it
Use the 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], or https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.trace.traceinformation[Trace.TraceInformation] methods.
=== Noncompliant code example
[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
}
----
=== Compliant solution
[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[]