Home > Software design >  Log files are not created when executing single file application
Log files are not created when executing single file application

Time:07-25

I'm trying to create a single file executable for my console application. The problem is that logging files are not created for some reason. If I run application in Visual studios, simple debug mode then everything works fine, I can see log file appear in the \bin\Debug\netcoreapp3.1 folder.

This is what publishing settings I use

enter image description here

I am using NLog for logging, and that's how my code looks regarding that

services.AddLogging(builder =>
{
    builder.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
    builder.AddNLog("nlog.config");
});
LogManager.Configuration = new NLogLoggingConfiguration(config.GetSection("NLog"));

and thats appsettings.json where I hold my nlog configurations

{
    "NLog": {
        "targets": {
            "console": {
                "type": "ColoredConsole",
                "layout": "${longdate}| ${uppercase:${level}}|  ${message}   ${exception:format=tostring}",
                "rowHighlightingRules": [
                    {
                        "condition": "level == LogLevel.Trace",
                        "foregroundColor": "DarkGray"
                    },
                    {
                        "condition": "level == LogLevel.Debug",
                        "foregroundColor": "DarkGray"
                    },
                    {
                        "condition": "level == LogLevel.Info",
                        "foregroundColor": "White"
                    },
                    {
                        "condition": "level == LogLevel.Warn",
                        "foregroundColor": "Yellow"
                    },
                    {
                        "condition": "level == LogLevel.Error",
                        "foregroundColor": "Red"
                    },
                    {
                        "condition": "level == LogLevel.Fatal",
                        "foregroundColor": "Red"
                    }
                ]
            },
            "allLogFile": {
                "type": "file",
                "fileName": "${basedir}/logs/allLog.txt",
                "archiveFileName": "${basedir}/logs/allLog.{#}.txt",
                "archiveNumbering": "Date",
                "archiveEvery": "Day",
                "archiveDateFormat": "yyyyMMdd",
                "layout": "${longdate}| ${uppercase:${level}}|  ${logger}|  ${message}   ${exception:format=tostring}"
            },
            "importantLogFile": {
                "type": "file",
                "fileName": "${basedir}/logs/importantLog.txt",
                "archiveFileName": "${basedir}/logs/importantLog.{#}.txt",
                "archiveNumbering": "Date",
                "archiveEvery": "Day",
                "archiveDateFormat": "yyyyMMdd",
                "layout": "${longdate}| ${uppercase:${level}}|  ${logger}|  ${message}   ${exception:format=tostring}"
            }
        },
        "rules": [
        {
        "levels": "Info, Warning, Error, Fatal",
        "logger": "*",
        "writeTo": "console"
        },
            {
                "logger": "*",
                "minLevel": "Trace",
                "writeTo": "allLogFile"
            },
            {
                "levels": "Debug, Error, Fatal",
                "logger": "*",
                "writeTo": "importantLogFile"
            }
        ]
    }
}

CodePudding user response:

Found the solution after hours and hours of struggle. so my allLog.txt file path was "fileName": "${basedir}/logs/allLog.txt", apparently if one file executable is created ${basedir} will become something into like C:\Users\my_username\AppData\Local\Temp\.net\my_project_name\VnUDuP7f4QxGtrsSuaNaBXA_wxhYftr=\ , so to fix that I need to use ${basedir:fixtempdir=true} instead of ${basedir} so full path would be ${basedir:fixtempdir=true}/logs/allLog.txt.

  • Related