1

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.

zchpit
  • 3,071
  • 4
  • 28
  • 43

2 Answers2

3

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.

Nicholas Blumhardt
  • 30,271
  • 4
  • 90
  • 101
1

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.

Tseng
  • 61,549
  • 15
  • 193
  • 205