2022-08-23 10:07:08 +02:00
|
|
|
=== How to fix it in ASP.NET
|
|
|
|
|
|
|
|
include::../../common/fix/code-rationale.adoc[]
|
|
|
|
|
2022-10-18 16:03:10 +02:00
|
|
|
==== Noncompliant code example
|
2022-09-15 10:28:08 +02:00
|
|
|
|
|
|
|
[source,csharp,diff-id=1,diff-type=noncompliant]
|
2022-08-23 10:07:08 +02:00
|
|
|
----
|
|
|
|
using System.Web;
|
|
|
|
using System.Web.Mvc;
|
|
|
|
|
|
|
|
public class ExampleController : Controller
|
|
|
|
{
|
2022-08-23 10:18:42 +02:00
|
|
|
private static readonly log4net.ILog _logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
|
|
|
|
|
2022-08-23 10:07:08 +02:00
|
|
|
[HttpGet]
|
|
|
|
public void Log(string data)
|
|
|
|
{
|
|
|
|
if (data != null)
|
|
|
|
{
|
2022-08-25 10:13:12 +02:00
|
|
|
_logger.Info("Log: " + data); // Noncompliant
|
2022-08-23 10:07:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
----
|
2022-09-15 10:28:08 +02:00
|
|
|
|
|
|
|
==== Compliant solution
|
|
|
|
|
|
|
|
[source,csharp,diff-id=1,diff-type=compliant]
|
2022-08-23 10:07:08 +02:00
|
|
|
----
|
|
|
|
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)
|
|
|
|
{
|
2022-08-23 10:12:21 +02:00
|
|
|
data = data.Replace('\n', '_').Replace('\r', '_');
|
2022-08-23 10:07:08 +02:00
|
|
|
_logger.Info("Log: " + data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
2022-09-15 10:28:08 +02:00
|
|
|
include::../../common/fix/how-does-this-work.adoc[]
|