让 {aspnet-request} 正常工作而无需从调用 NLog 的项目中引用 NLog.Web
getting {aspnet-request} to work properly without referencing NLog.Web from project that is calling NLog
我有一个 asp.net 项目调用我的 NLog 包装器 class (.netframework 4.7)。为了让 {aspnet-request} 工作,我必须添加一个 NLog.Web 对 asp.net 项目的引用,以及对我的 NLog 包装器 class 的引用。有没有一种方法可以使 {aspnet} 调用工作而无需从 asp.net 项目引用 NLog.Web,而仅引用我的 NLog 包装器 class?我尝试添加扩展以在我的 NLog.config 中添加 NLog.Web 程序集,但无济于事。我还尝试在我的包装器 class 中仅引用 NLog.Web。但是从 asp.net 项目 class 中抛出一个错误,指出找不到 NLog.Web。提前致谢。
当前参考文献:my references
NLog 包装器 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 调用 NLog 包装器的项目 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" });
}
}
}
这是我的 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 包装器 class 中遇到的错误:Error Message I get
WebApplication 项目正在引用 EventLog class 库,但其中一个 dll 没有被复制到项目的输出中。这是因为即使 class 库引用了这个 dll,除了引用之外没有实际调用或使用 dll,需要显式调用这个 dll 才能让它复制到项目中。
所以我在 class 库中的 NLog.Web.dll 中添加了一个对随机 属性 的虚拟调用,现在引用 class 库的项目获得了 NLog.Web.dll 在其输出文件夹 (bin) 中。
Dependent DLL is not getting copied to the build output folder in Visual Studio
我有一个 asp.net 项目调用我的 NLog 包装器 class (.netframework 4.7)。为了让 {aspnet-request} 工作,我必须添加一个 NLog.Web 对 asp.net 项目的引用,以及对我的 NLog 包装器 class 的引用。有没有一种方法可以使 {aspnet} 调用工作而无需从 asp.net 项目引用 NLog.Web,而仅引用我的 NLog 包装器 class?我尝试添加扩展以在我的 NLog.config 中添加 NLog.Web 程序集,但无济于事。我还尝试在我的包装器 class 中仅引用 NLog.Web。但是从 asp.net 项目 class 中抛出一个错误,指出找不到 NLog.Web。提前致谢。
当前参考文献:my references
NLog 包装器 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 调用 NLog 包装器的项目 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" });
}
}
}
这是我的 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 包装器 class 中遇到的错误:Error Message I get
WebApplication 项目正在引用 EventLog class 库,但其中一个 dll 没有被复制到项目的输出中。这是因为即使 class 库引用了这个 dll,除了引用之外没有实际调用或使用 dll,需要显式调用这个 dll 才能让它复制到项目中。
所以我在 class 库中的 NLog.Web.dll 中添加了一个对随机 属性 的虚拟调用,现在引用 class 库的项目获得了 NLog.Web.dll 在其输出文件夹 (bin) 中。
Dependent DLL is not getting copied to the build output folder in Visual Studio