Answer Detail

How to configure nlog in .netcore?

Smith
Smith
Participant
348 Points
104 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}.

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

Share:   fb twitter linkedin
Answers
Brian
Brian
Participant
510 Points
70 Posts
Marked as answer      

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
banner

Blog

Active User (3)

 Log In to Chat