Base Directory path of different environments in nlog

Viewed 637

I have .net core web api project with Production,Stage and Development environments. i used {baseDir} in filename of nlog target but when i run project it store log file in bin\netcoreapp3.1\netcoreapp3.1 folder. I have a folder Logs and i want to store logs in this folder. same in all environments i want that. Anyone know how to do that please help me !!

for more detail :

nlog.config

    <variable name="DefaultLayout" value="${longdate} ${processid} ${uppercase:${level}} ${logger:shortName=true} ${environment-user} ${local-ip} ${message} "/>
        
    <targets async="true">
            <target xsi:type="File" name="file"  layout="${DefaultLayout}" fileName="${baseDir}\log-${shortdate}.log" />
    </targets>

Thank you !!

1 Answers

The {baseDir} is the directory from which the code is executing, so what you're getting is correct.

If you want to use the content root path of your ASP.NET Core app you need to use ${aspnet-appbasepath} - ref.

You can specify it as fileName="${aspnet-appbasepath}\Logs\log-${shortdate}.log".

From the docs:

Introduced with NLog.Web.AspNetCore v4.5.0 and NLog.Web v4.5.2

Make sure you're targeting the respective version, otherwise you wouldn't be able to use ${aspnet-appbasepath}

Related