'C# : Why won't NLog generate a log file even with no errors

Here's my 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 xsi:type="File" name="logfile" fileName="${basedir}\log.txt" layout="${longdate}  ${message}"/>
  </targets>

  <rules>
    <logger name ="logger" minlevel="Info" writeTo="logfile"/>
  </rules>
</nlog>

I had tried to mention this file with this code

private static readonly Logger Logger = LogManager.GetLogger("logfile");

But the log file doesn't get generated or written even if I create one, I had also tried doing it programatically

var config = new NLog.Config.LoggingConfiguration();
var logfile = new NLog.Targets.FileTarget("logfile") { FileName = "log.txt" };
var layout = new NLog.Layouts.SimpleLayout();
config.AddRule(LogLevel.Info, LogLevel.Fatal, logfile);
NLog.LogManager.Configuration = config;

That doesn't seem to work so I'm back to my first try. At this point I looked through the troubleshoot page on the wiki, I have set the Build Action to Content and Copy to Output to Copy if newer but the problem doesn't change. I checked my config syntax and it doesn't seem to be wrong, so I'm guessing it's a problem with my cs code.

some background/other information

  • this code is run in a class which is run through an async method in another class
  • this is to log the outputs of a log that just keeps going untill i shut it off
  • this is used in a wpf app that i had made a button that runs this method through an async task

not sure if this is needed but here is how I log something

Logger.Info("stuff!!");

Any help?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source