How to configure nlog in .netcore?

Smith
Smith
None
2568 Points
74 Posts

I'm using .NetCore 2.0

In the nlog.config

<targets>
<!-- write logs to file -->
<target xsi:type="File" name="allfile" fileName=
"${basedir}\temp\nlog-all-${shortdate}.log"
layout="${longdate}| ${event-properties:item=
EventId.Id}|${logger}
|${uppercase:${level}}
|${message} ${exception}" />

But unable to create the log files. Anyone has any idea how to use ${basedir}.

Views: 10727
Total Answered: 1
Total Marked As Answer: 1
Posted On: 10-Apr-2018 02:41

Share:   fb twitter linkedin
Answers
Brian
Brian
Moderator
2232 Points
14 Posts
         

Update NLog 4.4 this is possible in one line (LayoutRenderer.Register)

And Add in program.cs LayoutRenderer.Register("basedir", (logEvent) => env.ContentRootPath);

public void Configure(IApplicationBuilder app, IHostingEnvironment env, 
ILoggerFactory loggerFactory)
{

//add NLog to ASP.NET Core
loggerFactory.AddNLog();

env.ConfigureNLog("nlog.config");

LayoutRenderer.Register("basedir", (logEvent) => env.ContentRootPath);

...
}
Posted On: 15-Apr-2018 04:00
Also, if still not working, please change the temp folder name to \temp1\ in nlog.config file
 - Rahul Maurya  27-Jun-2018 06:29
Great guys. It's worked for me.
 - Smith  27-Jun-2018 06:31
 Log In to Chat