0

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

made_it_ma
  • 89
  • 9
  • Can you show the NLog.config that you are using that includes `NLog.Web` in `` ? Do you remember to deploy `NLog.Web.dll` with your Nlog.config ? – Rolf Kristensen Oct 03 '19 at 16:58
  • Alright sir. I updated a few things as well as included the Nlog.config and the exception message i get. I updated the reference picture, and also editted the wrapper class, to properly wrap NLog according to documentation I found online – made_it_ma Oct 03 '19 at 17:37
  • I did compile NLog and NLog.Web source code, into dlls and then reference them. I did not use NuGet package manager import them in. idk if this makes a difference. but it is the guideline i must follow – made_it_ma Oct 03 '19 at 17:47
  • Old dotnet project sucks in handling of transient dependencies. When deploying an application, then you have to manually include all sub-dependencies (ex. like NLog.dll + NLog.Web.dll). If you have actually included the relevant dependencies, then you have to use something like `Fuslogvw.exe` to see what is missing when running your application. – Rolf Kristensen Oct 03 '19 at 19:04
  • Btw. instead of compiling NLog.dll and NLog.Web.dll manually, then you can also just unzip the official versions from the nuget-packages. See "Download Package" at https://www.nuget.org/packages/NLog.Web and https://www.nuget.org/packages/NLog – Rolf Kristensen Oct 03 '19 at 19:06
  • alright i'm going to try that out. What do i do with this .nupkg though? how can i open it? bossman wants it compiled manually for 'security reasons' -__- – made_it_ma Oct 03 '19 at 19:17
  • `.nupkg` is just a fanzy way of say zip-file. If you add `.zip` to the filename of the downloaded file, then you can open it with your favorite zip-app. But normally you just add the nuget-package to the project using visual-studio. But everyone have their favorite style. – Rolf Kristensen Oct 03 '19 at 19:20
  • hmmm. so i just nuget installed NLog 4.6.7 and NLog.Web 4.8.5, to my wrapper class, just to see if it would work. and I still get the same errors. Any thoughts? – made_it_ma Oct 03 '19 at 19:33
  • does NLog.Web have to be referenced in the project that is calling my NLog wrapper class? – made_it_ma Oct 03 '19 at 19:33
  • Did you check with `Fuslogvw.exe` to see why it failed to load NLog.Web.dll ? Again NLog.dll and NLog.Web.dll has to be deployed with the application, else it will not work (has to be in the output-folder). – Rolf Kristensen Oct 03 '19 at 19:33
  • Btw. if you are doing AspNetCore (And not AspNet). Then you should not use NLog.Web but NLog.Web.AspNetCore: https://www.nuget.org/packages/NLog.Web.AspNetCore/ – Rolf Kristensen Oct 03 '19 at 19:40
  • Okay. so i have EventLog and WebApplication(project calling EventLog). EventLog is the only one that has references to NLog and NLog.Web...yet in the output folder of WebApplication, NLog.dll is there, NLog.Web.dll is not there. When i manually put NLog.dll in WebApplication output folder, it works....is there a way to get it to output NLog.Web.dll into the output folder of WebApplication? – made_it_ma Oct 03 '19 at 19:47
  • yeah, i'm using NLog.Web with aspnet, not aspnetcore – made_it_ma Oct 03 '19 at 19:47
  • hell yeah. i got to work. Thank you for your help. you mentioned checking the output directory of the calling application for NLog and NLog.Web which was key – made_it_ma Oct 03 '19 at 20:23
  • Glad you got it working. Always a good idea to check the simple stuff first. File won't load. Maybe file not there :) – Rolf Kristensen Oct 03 '19 at 20:46
  • LOL seriously. Now I know. I didn't realize that when a project references a class library, all the references (dlls) in the class library get output to the bin directory of the project. Thats 2 new things I learned this week! Thanks to you – made_it_ma Oct 03 '19 at 20:57

1 Answers1

2

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

made_it_ma
  • 89
  • 9