rspec/rules/S6670/csharp/rule.adoc
Costin Zaharia 70bdb31f51
Logging rules: add "how to fix" section (#3969)
* S6674: add how to fix section

* S6677: add how to fix section

* S2629: add how to fix section

* S6670: add how to fix section

* S1312: add how to fix section

* S6672: add how to fix section

* S6670: add compliant/noncompliant headers

* S6669: add how to fix section

* S6668: add how to fix section

* S2629: remove method definition

* S6669: update formatting

* S6670: add links

* Fix formatting
2024-06-05 10:39:56 +02:00

39 lines
955 B
Plaintext

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[]