0

With AspNet Core, I would like to have a different config value when I develop a web site on my local computer and when the web site is published on the development server.

For exemple, on my computer, the log files use the path "..\..\logs\app.log" and on the development server it's "w:\logs\app.log". Since that by default, AspNet Core is using the appsettings.Development.json file wherever I'm on my development computer or on the development server, I cannot set the path differently.

So how can I distinguish when the code run on my local computer and when it run on my development server and have a different settings in my appsettings.json files and still use env.IsDevelopment() that will return true on both environment? The reason I need that is because Microsoft use the IsDevelopment() function in there own logic and I don't want to break that.

Alexandre Jobin
  • 2,811
  • 4
  • 33
  • 43
  • Possible duplicate of [How to update appsettings.json based on publish profile using .NET Core?](https://stackoverflow.com/questions/53330404/how-to-update-appsettings-json-based-on-publish-profile-using-net-core) – Simply Ged Dec 04 '18 at 08:11

2 Answers2

0

From your description, I understood that you want to keep single Config file. If that is the case, you can alter your configuration setting on server side using environment variables.

Logging__logPath=C:\dir\file.log

this link may help you https://medium.com/thirddev/overriding-configuration-using-environmental-variables-in-asp-net-core-d38079475654

If your server is hosted on IIS then you can change your environment variables under Configuration Editor. Here is the step by step instructions

  1. Go to your application in IIS and choose Configuration Editor.
  2. Select Configuration Editor
  3. Choose system.webServer/aspNetCore (RC2 and RTM) or system.webServer/httpPlatform (RC1) in Section combobox
  4. Choose Applicationhost.config ... in From combobox.
  5. Click on enviromentVariables element and open edit window.
  6. Set your environment variables.
  7. Close the window and click Apply. Done REF: https://stackoverflow.com/a/36836533/1118978

Visal demonstration: https://www.andrecarlucci.com/en/setting-environment-variables-for-asp-net-core-when-publishing-on-iis/

If you prefer to have multiple config files, then :

   public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
            .AddEnvironmentVariables();

        Configuration = builder.Build();
    }

and environment variable is needed to be set ASPNETCORE_ENVIRONMENT=DevServer and your config file will be appsettings.DevServer.json

Derviş Kayımbaşıoğlu
  • 28,492
  • 4
  • 50
  • 72
  • You are right that i can change the EnvironmentVariable on the dev-server but if I give it the name "DevServer", the function `env.IsDevelopment()` will return false and everything that is set in code when the environment is "Development" will never run. – Alexandre Jobin Dec 04 '18 at 18:14
  • You can use env.IsEnvironment istead of env.IsDevelopment. but if your main goal is to change the log location, I suggest you to use environment variables to update log location. – Derviş Kayımbaşıoğlu Dec 04 '18 at 21:11
0

.NET Core solves this problem by using "Environments".

It's kind of evolved over the versions so it's a little different depending on which version of .NET Core you are actually using. But in short, your code will typically load appsettings.json first, then overwrite any settings from a file called :

appsettings.{env.EnvironmentName}.json

Notice how the Environment is suffixed to your appSettings. To set the environment for your machine, if you are on Windows you can run a powershell command like :

$Env:ASPNETCORE_ENVIRONMENT = "Development"

Or have a quick google around "How to set environment variables". As long as the key is "ASPNETCORE_ENVIRONMENT", then whatever you set it to, you can then load that file as your settings.

More info : https://dotnetcoretutorials.com/2017/05/03/environments-asp-net-core/

MindingData
  • 11,924
  • 6
  • 49
  • 68
  • I understand how the EnvironmentName work but how can I distinguish between my dev-computer and my dev-server where the config might differ. If I create a different environment name for my dev-server, everything that use `env.IsDevelopment()` will not work. Same thing if I create a new environment name for my dev-computer. Since that Microsoft use the `env.IsDevelopment()` in is own logic, my app will miss something. – Alexandre Jobin Dec 04 '18 at 18:17