pedro-oliveira-sonarsource 2888e2ab67 Noncompliant annotation
2023-03-02 19:03:03 +01:00

51 lines
1.1 KiB
Plaintext

=== How to fix it in ASP.NET
include::../../common/fix/code-rationale.adoc[]
==== Noncompliant code example
[source,csharp,diff-id=1,diff-type=noncompliant]
----
using System.Web;
using System.Web.Mvc;
public class ExampleController : Controller
{
private static readonly log4net.ILog _logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
[HttpGet]
public void Log(string data)
{
if (data != null)
{
_logger.Info("Log: " + data); // Noncompliant
}
}
}
----
==== Compliant solution
[source,csharp,diff-id=1,diff-type=compliant]
----
using System.Web;
using System.Web.Mvc;
public class ExampleController : Controller
{
private static readonly log4net.ILog _logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
[HttpGet]
public void Log(string data)
{
if (data != null)
{
data = data.Replace('\n', '_').Replace('\r', '_');
_logger.Info("Log: " + data);
}
}
}
----
include::../../common/fix/how-does-this-work.adoc[]