|
| 1 | +// Copyright 2017 Serilog Contributors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +using System.Collections.Generic; |
| 16 | +using Microsoft.AspNetCore.Mvc; |
| 17 | +using Microsoft.Extensions.Configuration; |
| 18 | +using Microsoft.Extensions.Logging; |
| 19 | + |
| 20 | +namespace SimpleWebSampleV2.Controllers |
| 21 | +{ |
| 22 | + // This controller lists configuration settings and their value. |
| 23 | + // Please, do try to update the appsettings.json while the application is running. 😉 |
| 24 | + |
| 25 | + [Route("api/[controller]")] |
| 26 | + public class ConfigurationController : Controller |
| 27 | + { |
| 28 | + public ConfigurationController(IConfiguration configuration, ILogger<ConfigurationController> logger) |
| 29 | + { |
| 30 | + Configuration = configuration; |
| 31 | + Logger = logger; |
| 32 | + } |
| 33 | + |
| 34 | + public IConfiguration Configuration { get; } |
| 35 | + public ILogger Logger { get; } |
| 36 | + |
| 37 | + [HttpGet] |
| 38 | + public IEnumerable<KeyValuePair<string, string>> Get() |
| 39 | + { |
| 40 | + Logger.LogInformation("Listing all configuration settings…"); |
| 41 | + |
| 42 | + return Configuration.AsEnumerable(); |
| 43 | + } |
| 44 | + |
| 45 | + [HttpGet("{key}")] |
| 46 | + public string Get(string key) |
| 47 | + { |
| 48 | + Logger.LogInformation("The configuration key {ConfigurationKey} was requested.", key); |
| 49 | + string value = Configuration[key]; |
| 50 | + Logger.LogInformation("The configuration key {ConfigurationKey} has value {ConfigurationValue}.", key, value); |
| 51 | + |
| 52 | + return value; |
| 53 | + } |
| 54 | + } |
| 55 | +} |
0 commit comments