0

I've been trying to build an app that will use NLog to log a file. The code is really basic, but not seeming to work. Anyone have any ideas? I've set the correct things to "copy always" as well, like in this question. NLog doen't work on IIS

Code below.

MAIN (Including using statements to show I am actually using them)

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Configuration;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NLog;

class Program
{

    static void Main(string[] args)
    {
        var logger = LogManager.GetCurrentClassLogger();
        logger.Debug("xxxx");
    }

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

<targets>
<target name="file" xsi:type="File"
    layout="${longdate} ${logger} ${message}"
    fileName="${basedir}/log/logfile.txt" />
</targets>

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

App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration> 
  <startup> 
      <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
<runtime>
  <assemblyBinding>
    <dependentAssembly>
      <assemblyIdentity name="NLog" publicKeyToken="5120e14c03d0593c"   culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-3.2.1.0" newVersion="3.2.1.0" />
  </dependentAssembly>
</assemblyBinding>
</runtime>

</configuration>
Community
  • 1
  • 1
Stanfrancisco
  • 352
  • 1
  • 7
  • 24
  • 1
    Does the log folder exist? Use the `createDirs="true"` to automatically create directories for file targets. If that doesn't work you probably don't have permission to the directory. – Mike Zboray Jun 02 '15 at 21:29
  • 1
    Have you tried the troubleshooting here:https://github.com/NLog/NLog/wiki/Logging-Troubleshooting – rlee Jun 02 '15 at 21:29
  • 1
    Does your exe have write access to ${basedir} ? – Mihai Caracostea Jun 02 '15 at 21:30
  • the log does exist, but I will add the createDirs too. @mikez Thanks for the link. Will check @rlee? – Stanfrancisco Jun 02 '15 at 21:30
  • Good question @mihaicaracostea Do you know how to check? I assumed it would by default. – Stanfrancisco Jun 02 '15 at 21:31
  • 1
    @AustinDonley Try to create an empty file from inside your code. Tip: if your application is running from a special folder (windows, program files, etc.) and is not running elevated, it will not have write permissions there by default. – Mihai Caracostea Jun 02 '15 at 21:33
  • It was simply a write access issue, if you're still around @mihaicaracostea, post an answer and I will mark it as correct. Thanks everybody for the information. I learned a lot about Nlog during this struggle. – Stanfrancisco Jun 03 '15 at 13:18
  • 1
    Great to hear you've solved it! Will post in a minute. – Mihai Caracostea Jun 03 '15 at 13:19

3 Answers3

1

Are you getting a valid logger back from the GetCurrentClassLogger call?

Have you tried turning on Internal Debugging as shown here: https://github.com/NLog/NLog/wiki/Internal-Logging

This should point you in the correct direction as to why your log files are not being created

bechbd
  • 6,206
  • 3
  • 28
  • 47
1

Check whether your process has write access to the ${basedir} location. Also, if your application is running from a special folder, make sure the process is running elevated.

Julian
  • 33,915
  • 22
  • 119
  • 174
Mihai Caracostea
  • 8,336
  • 4
  • 27
  • 46
0

If you are trying to debug from Visual studio, try to run VS in elevated account ( run as administrator ) and then start debugging. This way NLog will have enough permissions to create log file.

Also use createDirs=true for each target section in nlog config file to automatically create missing folders in the log file path that is provided in the target section.

Jabez
  • 795
  • 9
  • 18