简体   繁体   English

NLog 未在 MVC 应用程序中创建日志?

[英]NLog not creating log in MVC application?

I've used NLog on a few console apps without any issue but this is the first time I've used it on an MVC web application.我在几个控制台应用程序上使用 NLog 没有任何问题,但这是我第一次在 MVC Web 应用程序上使用它。 When I try to log to a file nothing happens.当我尝试登录到文件时,什么也没有发生。 There are no errors nor are any log files created.没有错误,也没有创建任何日志文件。 When I step through the app locally I don't get any errors either.当我在本地浏览应用程序时,我也没有收到任何错误。

First thing I did was confirm that my NLog.config file's property was set to “Copy always” in the “Copy to Output” property.我做的第一件事是确认我的 NLog.config 文件的属性在“Copy to Output”属性中设置为“Copy always”。 I even uninstalled NLog through NuGet and then installed it again as well as closed VS and opened up the project again.我什至通过 NuGet 卸载了 NLog,然后再次安装它以及关闭 VS 并再次打开项目。

I've done some searching and the only real suggestions I found were to create the folder location first and then check the permissions on the app pool.我进行了一些搜索,发现的唯一真正建议是先创建文件夹位置,然后检查应用程序池的权限。 I created the folder location but that didn't seem to work either.我创建了文件夹位置,但这似乎也不起作用。 I'm running this through Debug mode in Visual Studio 2015 which automatically fires up a local web server so I don't know how to find out what service it's using to write to the file location.我正在通过 Visual Studio 2015 中的调试模式运行它,该模式会自动启动本地 Web 服务器,因此我不知道如何找出它用于写入文件位置的服务。

In the example below I can put a break point on my ActionResult inside my controller and see a value being passed in the “gate” parameter.在下面的示例中,我可以在我的控制器内的 ActionResult 上放置一个断点,并查看在“gate”参数中传递的值。 I step though to my test of the logger and don't get any errors.我对记录器进行了测试,但没有出现任何错误。 I look in my log location and no log file is created.我查看了我的日志位置,但没有创建日志文件。

Anyone have any suggestions on what I can try?有人对我可以尝试的方法有什么建议吗?

NLog.config file 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" 
      throwExceptions="true">

  <variable name="LogDirBase" value="D:/logs/RampInfo"/>
  <variable name="LogYear" value="${date:format=yyy}"/>
  <variable name="LogMonth" value="${date:format=MM}"/>
  <variable name="LogDay" value="${date:format=dd}"/>
  <variable name="LogFileShortDate" value="${date:format=yyyy-MM-dd}"/>

  <targets>

    <target name="DefaultTarget"
      xsi:type="File"
      fileName="${LogDirBase}/${LogFileShortDate}.log"
      encoding="utf-8"
      layout="${longdate} | ${callsite} | ${message}"
      maxArchiveFiles="14"
    />

  </targets>

  <rules>
    <logger name="defaultLogger" minlevel="Debug" writeTo="DefaultTarget" />
  </rules>
</nlog>

My Controller我的控制器

public class RampController : Controller
    {
        private static Logger logger = LogManager.GetCurrentClassLogger();


        // GET: GateInfo
        public ActionResult Gate(string gate)
        {

            logger.Debug("Start action result. Gate: " + gate);
    }

Logger names typically use something like this记录器名称通常使用这样的东西

<logger name="Name.Space.RampController" minlevel="Debug" writeTo="DefaultTarget" /> 

or, if you just want a single log file for your application:或者,如果您只想为您的应用程序创建一个日志文件:

<logger name="*" minlevel="Debug" writeTo="DefaultTarget" /> 

Internally, LogManager.GetCurrentClassLogger looks at the current Stack to determine the calling type: (silverlight conditionals removed):在内部, LogManager.GetCurrentClassLogger查看当前 Stack 以确定调用类型:(移除了 Silverlight 条件):

public new T GetCurrentClassLogger()
{
    StackFrame frame = new StackFrame(1, false);
    return this.GetLogger(frame.GetMethod().DeclaringType.FullName);
}

I appreciate everyone's suggestions.我感谢大家的建议。 I spent hours trying to figure out why the NLog wasn't working.我花了几个小时试图找出 NLog 不工作的原因。 I gave up for the day and came back to it this morning with a fresh mind.我放弃了这一天,今天早上带着新的想法回来了。 I basically started over with a whole new NLog.config and that worked.我基本上是从一个全新的 NLog.config 开始的,而且效果很好。 I copied out of different project a different NLog configuration.我从不同的项目中复制了不同的 NLog 配置。 I don't really see the difference between the two but either way it's now working as expected.我真的没有看到两者之间的区别,但无论哪种方式,它现在都按预期工作。

Here is the new NLog.config I went with.这是我使用的新 NLog.config。

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      autoReload="true" 
      throwExceptions="true">

  <variable name="LogDirectory" value="D:\integration\logs\RampInfo"/>

  <targets>
    <target name="LogTarget" 
            xsi:type="File" 
            fileName="${LogDirectory}/${shortdate}.log" 
            encoding="utf-8"
            maxArchiveFiles="14" 
            layout="${longdate} ${callsite} ${message}" />
  </targets>

  <rules>
    <logger name="*" minlevel="Debug" writeTo="LogTarget" />
  </rules>
</nlog>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM