简体   繁体   English

在asp.net核心的appsettings.json中

[英]basedir in appsettings.json in asp.net core

I started my new project in ASP.NET Core and I have a question. 我在ASP.NET Core中开始了我的新项目,我有一个问题。

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 我有2个记录器:a)nLog,在nlog.config文件中有配置b)serilog,在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 此时,我有两个存储日志的位置: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. 我的问题是,如何在appsettings.json文件中获取basedir目录。

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. 然后在配置中使用"%BASEDIR%/logs/serilog-{Date}.txt"

Short : You can't in appsettings.json. 简短 :你不能在appsettings.json。

Longer : You can in code via the static PlatformServices class. 更长 :您可以通过静态PlatformServices类在代码中。

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. 然后使用replace或者你认为合适的任何东西将${basedir}替换为ApplicationBasePath返回的值,然后将其传递给loggers配置。

Sidenote: You shouldn't use PlatformServices outside of the Startup / Programm class, since they are static and bad to test etc. 旁注:您不应该在Startup / Programm类之外使用PlatformServices ,因为它们是静态的,不适合测试等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM