Nlog: Cant log to file on Linux

Created on 5 Dec 2020  路  5Comments  路  Source: NLog/NLog

I have asp.net core app. I deployed it to Linux (CentOS 8) but I cant make it work (it works fine on my dev machine on Windows).

NLog is 4.9.3

dotnet --info
.NET SDK (reflecting any global.json):
Version: 5.0.100
Commit: 5044b93829

Runtime Environment:
OS Name: centos
OS Version: 8
OS Platform: Linux
RID: centos.8-x64
Base Path: /usr/share/dotnet/sdk/5.0.100/

Host (useful for support):
Version: 5.0.0
Commit: cf258a14b7

.NET SDKs installed:
5.0.100 [/usr/share/dotnet/sdk]

.NET runtimes installed:
Microsoft.AspNetCore.App 5.0.0 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.NETCore.App 5.0.0 [/usr/share/dotnet/shared/Microsoft.NETCore.App]

NLog.Config (I tried different paths for filename eg /var/log/myapp and ~ - nothing works)

<?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"
  autoReload="true">
  <extensions>
    <add assembly="NLog.Web.AspNetCore"/>
  </extensions>
  <targets>
    <target name="logfile" xsi:type="File" fileName="./log-${shortdate}.log" />
  </targets>
  <rules>
    <logger name="*" minlevel="Trace" writeTo="logfile" />
  </rules>
</nlog>

My Program.cs:

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureLogging(logging =>
                {
                    logging.ClearProviders();
                    logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
                })
                .UseNLog()
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });

appsettings.json

  "Logging": {
    "LogLevel": {
      "Default": "Trace",
      "Microsoft": "Trace",
      "Microsoft.Hosting.Lifetime": "Trace"
    }
    },
file-target question

Most helpful comment

NLog will automatically load "NLog.config" and "nlog.config", but I guess it will fail when using "Nlog.Config"

All 5 comments

Hi! Thanks for opening your first issue here! Please make sure to follow the issue template - so we could help you better!

Please try and enable the NLog InternalLogger at runtime like this:

```c#
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureLogging(logging =>
{
// enable NLog internal logging for troubleshooting
NLog.Common.InternalLogger.LogToConsole = true;
NLog.Common.InternalLogger.LogFile = "/home/nlog-internal.txt";
NLog.Common.InternalLogger.LogLevel = LogLevel.Debug;

                logging.ClearProviders();
                logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
            })
            .UseNLog()
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });

```

And check the output for clues: https://github.com/NLog/NLog/wiki/Internal-Logging

Please add the requested info, so we could help you better! (This issue will be closed in 7 days)

I changed config name to all small caps nlog.config and put full path for the log file location and it started to work. Thank you

NLog will automatically load "NLog.config" and "nlog.config", but I guess it will fail when using "Nlog.Config"

Was this page helpful?
0 / 5 - 0 ratings

Related issues

geedsen picture geedsen  路  3Comments

imanushin picture imanushin  路  3Comments

haythamabutair picture haythamabutair  路  3Comments

ErcinDedeoglu picture ErcinDedeoglu  路  3Comments

Rapiiidooo picture Rapiiidooo  路  3Comments