Al2110
Al2110

Reputation: 576

NLog not outputting text file

I have downloaded and installed the NuGet package NLog. I followed the tutorial, choosing to configure it through code, as follows:

public static void ConfigureLogger()
{
    var config = new NLog.Config.LoggingConfiguration();

    // target where to log to
    string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
    var logfile = new NLog.Targets.FileTarget("logfile") { FileName = path + @"\log.txt" };
    var logconsole = new NLog.Targets.ConsoleTarget("logconsole");

    // rules for mapping loggers to targets
    // minimum and maximum log levels for logging targets
    config.AddRule(NLog.LogLevel.Info, NLog.LogLevel.Fatal, logconsole);
    config.AddRule(NLog.LogLevel.Info, NLog.LogLevel.Fatal, logfile);

    // apply config
    NLog.LogManager.Configuration = config;
}

Within the application code, each class gets its own instance, as recommended in the tutorial:

private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();

I find that it is not writing the text file to the specified directory. I have read some troubleshooting advice, but I cannot find the file "NLog.config".

Upvotes: 2

Views: 568

Answers (1)

Athanasios Kataras
Athanasios Kataras

Reputation: 26372

Are you using .net core? On both .net and .net core you need what's referenced here for nlog:

https://github.com/NLog/NLog/wiki/Getting-started-with-ASP.NET-Core-3

Create nlog.config (lowercase all) file in the root of your project.

So you don't find out because you have not created it. There are more things to do.

In your code configuration, you need to execute the function somewhere. Are you calling it from your Startup.cs or any other entry point depending on the platform?

Upvotes: 1

Related Questions