I started development on an ASP .NET MVC project, running on .NET 4.8.1.
I wanted to use NLog for logging and decided to add it through DI.
Not being used to doing this in ASP .NET, I googled and found a "how to" using Unity.
I started by adding the necessary references:
Added a file UnitConfig.cs in App_Start:
using System.Web.Mvc;
using Unity;
using Unity.Mvc5;
using Unity.NLog;
namespace SomeSite
{
public static class UnityConfig
{
public static void RegisterComponents()
{
var container = new UnityContainer();
container.AddNewExtension<NLogExtension>();
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
}
}
}
Added a reference in Global.asax.cs:
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
namespace SomeSite
{
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
UnityConfig.RegisterComponents();
}
}
}
And then I added a constructor & a log line in each of my 2 Controllers, like this:
using System.Web.Mvc;
using NLog;
namespace SomeSite.Controllers
{
public class SomeController : Controller
{
private readonly ILogger _logger;
#region Constructors
public SomeController (ILogger logger)
{
_logger = logger;
}
#endregion
// GET: Some
public ActionResult Index()
{
_logger.Trace("SomeController - Index");
return View();
}
}
}
For good measure, I added a simple "Hello World" view, and @ first sight everything works, I get a log file, and each of the controllers logs it:
2024-01-28 18:51:51.3988 TRACE SomeSite.Controllers.SomeController SomeController - Index
2024-01-28 18:51:59.8301 TRACE SomeSite.Controllers.SomeController SomeController1 - Index
But as you can see, each of the log lines indicates it originates from the same class/controller SomeSite.Controllers.SomeController, while in reality the 2nd line should have been written as originating from SomeSite.Controllers.SomeController1.
So my question then becomes: what causes this and how can I fix it?
Perhaps a configuration issue? This is my nlog.xml:
<nlog
xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>
<targets>
<target
name="coloredConsole"
xsi:type="ColoredConsole"
useDefaultRowHighlightingRules="false"
layout="${longdate}|${pad:padding=5:inner=${level:uppercase=true}}|${message}"
>
<highlight-row
condition="level == LogLevel.Debug"
foregroundColor="DarkGray"
/>
<highlight-row
condition="level == LogLevel.Info"
foregroundColor="Gray"
/>
<highlight-row
condition="level == LogLevel.Warn"
foregroundColor="Yellow"
/>
<highlight-row
condition="level == LogLevel.Error"
foregroundColor="Red"
/>
<highlight-row
condition="level == LogLevel.Fatal"
foregroundColor="Red"
backgroundColor="White"
/>
</target>
<target
name="traceFile"
xsi:type="File"
layout="${longdate} ${pad:padding=5:inner=${level:uppercase=true}} ${logger} ${message}"
fileName="d:/Debug/MyClient/SomeSite/Logging/Trace_${shortdate}.log"
keepFileOpen="false"
encoding="iso-8859-2"
/>
<target
name="errorFile"
xsi:type="File"
layout="${longdate} ${pad:padding=5:inner=${level:uppercase=true}} ${logger} ${message}"
fileName="d:/Debug/MyClient/SomeSite/Logging/Error_${shortdate}.log"
keepFileOpen="false"
encoding="iso-8859-2"
/>
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="coloredConsole" />
<logger name="*" minlevel="Trace" writeTo="traceFile" />
<logger name="*" minlevel="Warn" maxlevel="Fatal" writeTo="errorFile" />
</rules>
</nlog>

