Reputation: 352
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>
Upvotes: 0
Views: 8382
Reputation: 905
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.
Upvotes: 0
Reputation: 8466
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.
Upvotes: 1
Reputation: 6341
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
Upvotes: 1