zchpit
zchpit

Reputation: 3121

basedir in appsettings.json in asp.net core

I started my new project in ASP.NET Core and I have a question.

I have 2 loggers: a) nLog, that have it's config in nlog.config file b) serilog, that have it's config in appsettings.json

At this moment, I have 2 locations to store logs: fileName="${basedir}/logs/EPR/nlog-all-${shortdate}.log - nLog "SerilogFile": "logs/serilog-{Date}.txt" - serilog

My question is, how to get basedir catalog in appsettings.json file.

Upvotes: 1

Views: 2395

Answers (2)

Nicholas Blumhardt
Nicholas Blumhardt

Reputation: 31877

One option is to set an environment variable with the base path at startup:

Environment.SetEnvironmentVariable("BASEDIR", AppDomain.CurrentDomain.BaseDirectory);

Then use "%BASEDIR%/logs/serilog-{Date}.txt" in the config.

Upvotes: 3

Tseng
Tseng

Reputation: 64297

Short: You can't in appsettings.json.

Longer: You can in code via the static PlatformServices class.

PlatformServices.Default.Application.ApplicationBasePath;

and then use replace or whatever you seem fit to replace the ${basedir} with the value returned by ApplicationBasePath, then pass it to your loggers configuration.

Sidenote: You shouldn't use PlatformServices outside of the Startup/Programm class, since they are static and bad to test etc.

Upvotes: 1

Related Questions