阅读NLog.config从NLOG在REST API .NETNLOG、config、NLog、NET

2023-09-04 13:34:37 作者:りōū丶硪以命守护

我有一个问题的时候,我从我的ASP.NET MVC4项目称它为我的类库项目读取配置文件(NLog.config)。

I have a problem reading the configuration file (NLog.config) from my class library project when I call it from my ASP.NET MVC4 project.

我测试我使用的是单元测试项目code和正确读取配置文件,并就我的NLog.config指定的文件正确地记录,所以NLog.config正确定义。

I tested my code using an Unit Test Project and reads the configuration file properly and makes logging correctly on the file that I specified on the NLog.config, so the NLog.config is correctly defined.

我还设置始终复制,在复制到输出目录设置,就像教程说。

I also set "Copy always" at Copy to Output Directory settings, just like the tutorial says.

现在的问题是,当我叫类库项目,从我的ASP项目中,我意识到,这不是做任何日志,当我调试,我看到了,这是不拾取从NLOG配置。配置,因为这个原因,我的项目不知道何时何地登录。

The problem is that when I called the class library project, from my ASP project, I realized that wasn't making any kind of log, and when I debugged, I saw that It wasn't picking the configuration from NLog.config, and for that reason, my project didn't know when and where to log.

当我看着ASP项目文件夹/ bin中,有来自NLOG文件:NLog.dll,NLog.config和NLog.xml,因此从理论上讲,在ASP项目,仍可正常访问到NLOG文件

When I looked into "ASP project folder"/bin, there are the files from NLog: NLog.dll, NLog.config and NLog.xml, so in theory, the ASP project, can access without problem to the NLog files.

我读NLOG的教程,但我不能想出的解决方案。只是不可阻挡明白,为什么从我的测试项目工作正常,但是当我做出同样的电话,从我的ASP项目的不愉快。

I read the tutorial from NLog but I can't figured out the solution. Just can´t understand why from my test project works properly but when I make the same call from my ASP project just don't work.

这是NLOG我的配置文件。

This is my configuration file for NLog.

<?xml version="1.0" encoding="utf-8" ?>
<nlog throwExceptions="true" 
  xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<targets>
  <target name="logfile" xsi:type="File" fileName="C:\logfile.txt" layout="${callsite:fileName=true} - ${longdate} - ${level:uppercase=true} - ${logger} - ${message}"/>
</targets>

<rules>
  <logger name="*" minlevel="Info" writeTo="logfile" />
</rules>
</nlog>

和我的班级里,​​我创造了记录器是这样的:

And within my classes, I create the logger this way:

private static Logger logger = LogManager.GetCurrentClassLogger();

任何人可以帮助我解决这个问题?

Can anybody help me with this problem?

推荐答案

我想出了解决方案。

好像与复制到输出目录设置,是不够用asp.net-mvc4。

Seems like with the copy to output directory setting, isn't enough with asp.net-mvc4.

我不得不手动复制NLog.config到ASP项目本身的解决方案资源管理器里面。

I had to manually copy the NLog.config into the asp project itself inside the Solution explorer.

在这个例子中,我复制了NLog.config从LoggingLibrary到LogWebApp项目,效果很好。

In this example, I copied the NLog.config from LoggingLibrary into LogWebApp project and It works well.

您好!