NLog does not create a log file

Srv19 picture Srv19 · May 11, 2012 · Viewed 65.5k times · Source

I am trying to add logging to an application running on mobile device with Windows Mobile 6.1. � .NET Compact framework 3.5. using NLog.

I have the appropriate version of the NLog distribution installed.

However no log files are being created.

Here is my 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">
  <targets>
    <target name="logfile" xsi:type="File" fileName=".\Neolant.ASRM.Terminal.log" layout="${longdate}|${level}|${message}|${exception}" autoFlush="true"/>
  </targets>
  <rules>
    <logger name="*" minlevel="Info" writeTo="logfile" />
  </rules>
</nlog>

And here is the test code I was using:

public static void Main()
{
    try
    {
        AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException;
        var logger = NLog.LogManager.GetLogger("UpperLevel");
        logger.Info("test test test.");
        try
        {
            throw new Exception("Unexpected!");
        }
        catch (Exception e)
        {
            var logger = NLog.LogManager.GetLogger("UpperLevel");
            logger.WarnException("An exception occured.", e);
        }
        throw new Exception("Suddenly!");           
    }
    finally
    {
        NLog.LogManager.Flush();
    }
}

private static void CurrentDomainOnUnhandledException(object sender, UnhandledExceptionEventArgs unhandledExceptionEventArgs)
{
    var logger = NLog.LogManager.GetLogger("UpperLevel");
    logger.FatalException("Application closed due to exception.", unhandledExceptionEventArgs.ExceptionObject as Exception);
    NLog.LogManager.Flush();
}

Answer

Malcolm Anderson picture Malcolm Anderson · Apr 14, 2017

I had this problem turned out that my log file was not being copied to my build directory. The NLog github page had the answer. (I've reformatted the paragraph a little for better readability.) https://github.com/NLog/NLog/wiki/Logging-troubleshooting

NLog cannot find the configuration file. This can happen when the NLog.config file is configured with Build Action = None or Copy to Output Directory = Do not copy in Visual Studio.

Set Build Action = Content and "Copy to Output Directory = Copy if newer to fix this)