Reputation: 99
I have an asp.net project that calls my NLog wrapper class (.netframework 4.7). To get {aspnet-request} to work, I had to add an NLog.Web reference to the asp.net project, along with reference to my NLog wrapper class. Is there a way I can get {aspnet} calls to work without referencing NLog.Web from the asp.net project, and just referencing my NLog wrapper class? I've tried adding extensions to add NLog.Web assembly in my NLog.config to no avail. I've also tried to just reference NLog.Web in my wrapper class. But an error gets thrown from the asp.net project class that says that it can't find NLog.Web. Thanks in advance.
Current references: my references
NLog Wrapper Class
using System;
using NLog;
public class MyLogger {
private Logger _logger;
public MyLogger(string name)
{
_logger = LogManager.GetLogger(name);
}
public void Info(string msg, params object[] args)
{
LogEventInfo logEvent = new LogEventInfo(LogLevel.Info, _logger.Name, null, msg, args);
_logger.Log(typeof(MyLogger), logEvent);
}
}
Asp.Net Project that calls NLog wrapper class:
WebApplication.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
EventLog.MyLogger l = new EventLog.MyLogger("uuu");
l.Info("Test {param1} and {param2}", new { OrderId = 2, Status = "Processing" }, new { OrderId = 1, Status = "Processing" });
}
}
}
Here is my NLog.config
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true"
throwConfigExceptions="true"
internalLogLevel="info"
internalLogFile="${basedir}\logs\internal\nlog.log">
<extensions>
<add assembly="NLog.Web"/>
</extensions>
<!-- NLog wikipedia: https://github.com/nlog/nlog/wiki/Tutorial -->
<!-- the targets to write to -->
<targets async="true">
<!-- write logs to file -->
<target xsi:type="File" name="allfile" fileName="${basedir}\logs\events\${date:format=yyyyMMdd}-${appsetting:item=Version}-${appsetting:item=Environment}.log"
layout="${longdate}
|${uppercase:${level}}
|IP:${aspnet-request-ip}
|Identity:${windows-identity}
|Controller:${aspnet-mvc-controller}
|Action:${aspnet-mvc-action}
|Callsite:${callsite}
|Message:${message} />
</targets>
<!-- rules to map from logger name to target -->
<rules>
<!--All logs-->
<logger name="*" minlevel="Trace" writeTo="allfile" />
</rules>
</nlog>
This is the error I get in my NLog wrapper class: Error Message I get
Upvotes: 0
Views: 313
Reputation: 99
WebApplication project was referencing EventLog class library, but one of the dlls was not being copied over to the output of the project. This was due to the fact that even though the class library was referencing this dll, there was no actual call or usage of the dll besides the reference, there needs to be an explicit call to this dll in order for it to get copied over to the project.
So I added a dummy call to a random property in NLog.Web.dll inside the class library and now the project that references the class library gets the NLog.Web.dll in its output folder(bin).
Dependent DLL is not getting copied to the build output folder in Visual Studio
Upvotes: 2