让{aspnet-request}正常工作,而无需从正在调用NLog的项目中引用NLog.Web

问题描述 投票:0回答:1

我有一个asp.net项目,该项目调用我的NLog包装器类(.netframework 4.7)。为了使{aspnet-request}正常工作,我必须添加对asp.net项目的NLog.Web引用,以及对我的NLog包装器类的引用。有没有一种方法可以使{aspnet}调用正常工作而无需从asp.net项目引用NLog.Web,而仅引用我的NLog包装器类?我尝试添加扩展以将NLog.Web程序集添加到NLog.config中无济于事。我还尝试在包装类中引用NLog.Web。但是从asp.net项目类抛出一个错误,指出它找不到NLog.Web。提前致谢。

当前参考:my references

NLog包装器类

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项目,调用NLog包装类:

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" });
        }
    }
}

这是我的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>

这是我在NLog包装器类中遇到的错误:Error Message I get

c# asp.net nlog
1个回答
0
投票

WebApplication项目正在引用EventLog类库,但是其中一个dll没有被复制到项目的输出中。这是由于以下事实:即使类库引用了该dll,除了引用之外也没有实际的dll调用或用法,需要对该dll进行显式调用才能将其复制到该项目。

因此,我向类库内NLog.Web.dll中的随机属性添加了一个虚拟调用,现在引用类库的项目在其输出文件夹(bin)中获得了NLog.Web.dll。

Dependent DLL is not getting copied to the build output folder in Visual Studio

© www.soinside.com 2019 - 2024. All rights reserved.