简体   繁体   中英

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. 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. I even uninstalled NLog through NuGet and then installed it again as well as closed VS and opened up the project again.

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.

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. 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

<?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):

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. 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. I copied out of different project a different NLog configuration. 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 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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