📄 IrbyImaging/appsettings.json Documentation
📑 Table of Contents
- Overview & Purpose
- Configuration Structure
- 2.1 KeyVault Section
- 2.2 Logging Section
- Dependencies & Technologies
- Accessing Configuration in Code
- Sample Code Snippets & Usage Examples
- Third‑Party Integrations
- Error Handling & Log Levels
- UML Diagram of Configuration Model
- Conclusion
1. 🔍 Overview & Purpose
The appsettings.json file in the IrbyImaging project houses environment‑agnostic configuration settings. It provides:
- Centralized configuration for logging and secrets management.
- Strong separation of concerns, keeping code clean from hard‑coded URIs or log levels.
- Ease of deployment, by allowing overrides via environment variables or Azure App Configuration.
Note: ASP.NET Core reads
appsettings.jsonby default at startup. You can augment or override these settings withappsettings.{Environment}.jsonor environment variables.
2. ⚙️ Configuration Structure
{
"KeyVault": {
"VaultUri": "https://irbysystemsvault.vault.azure.net/"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
2.1 🔑 KeyVault Section
| Property | Type | Description |
|---|---|---|
| VaultUri | string | The URI of the Azure Key Vault where secrets are stored. |
- Use Case: Securely fetch connection strings, certificates, or other secrets at runtime instead of hard‑coding them.
2.2 📈 Logging Section
| Property | Sub‑Property | Type | Description |
|---|---|---|---|
| Logging | LogLevel → Default | string | The default log level for application code. |
| LogLevel → Microsoft | string | Overrides the log level for all Microsoft namespaces. | |
| LogLevel → Microsoft.Hosting.Lifetime | string | Controls logging around host start/stop events. |
- Levels:
Trace<Debug<Information<Warning<Error<Critical<None
3. 📦 Dependencies & Technologies
- .NET 6 / .NET 7 (or higher): Leverages the built‑in configuration system.
- Microsoft.Extensions.Configuration: Core configuration abstractions.
- Microsoft.Extensions.Logging: Flexible logging framework.
- Azure.Security.KeyVault.Secrets & Azure.Identity: For Key Vault integration.
4. 🛠️ Accessing Configuration in Code
-
Register Configuration in
Program.csorStartup.cs:var builder = WebApplication.CreateBuilder(args); // 1. Add JSON file (already done by default) builder.Configuration.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true); // 2. Bind KeyVault section var keyVaultUri = builder.Configuration["KeyVault:VaultUri"]; builder.Configuration.AddAzureKeyVault( new Uri(keyVaultUri), new DefaultAzureCredential() ); // 3. Configure Logging builder.Logging.ClearProviders(); builder.Logging.AddConfiguration(builder.Configuration.GetSection("Logging")); builder.Logging.AddConsole(); builder.Logging.AddDebug(); var app = builder.Build(); -
Inject IConfiguration where needed:
public class SomeService { private readonly IConfiguration _config; public SomeService(IConfiguration config) { _config = config; } public void DoWork() { var vaultUri = _config["KeyVault:VaultUri"]; // use vaultUri... } }
5. 📘 Sample Code Snippets & Usage Examples
Reading a Secret from Key Vault
using Azure.Identity;
using Azure.Security.KeyVault.Secrets;
// Assume builder.Configuration is already set up
var vaultUri = builder.Configuration["KeyVault:VaultUri"];
var client = new SecretClient(new Uri(vaultUri), new DefaultAzureCredential());
KeyVaultSecret secret = await client.GetSecretAsync("MyDatabaseConnString");
string connectionString = secret.Value;
Logging with Structured Messages
using Microsoft.Extensions.Logging;
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
public IActionResult Index()
{
_logger.LogInformation("User accessed the Home Index at {Time}", DateTime.UtcNow);
return View();
}
}
6. 🔌 Third‑Party Integrations
- Azure Key Vault
- Package:
Azure.Security.KeyVault.Secrets - Authentication:
DefaultAzureCredential(supports Managed Identity, Environment, CLI credentials)
- Package:
- Azure App Service / Azure Functions
- You can wire up Managed Identity in Azure and the same
VaultUrito retrieve secrets without code changes.
- You can wire up Managed Identity in Azure and the same
7. 🛡️ Error Handling & Log Levels
| Scenario | Log Level | Description |
|---|---|---|
| Unexpected exception thrown | Error/Critical | Captures stack trace & error details. |
| Host start/stop events | Information | Monitors application lifetime hooks. |
| Third‑party library warnings | Warning | Suppresses verbose logs from Microsoft libs. |
| Diagnostic/tracing information | Trace/Debug | (Not enabled by default; can be overridden.) |
Tip: You can override any of these at runtime:
dotnet run --Logging:LogLevel:Default=Debug
8. 📐 UML Diagram of Configuration Model
classDiagram
class AppSettings {
+KeyVault vault
+Logging logging
}
class KeyVault {
+string VaultUri
}
class Logging {
+LogLevelSettings LogLevel
}
class LogLevelSettings {
+string Default
+string Microsoft
+string Microsoft.Hosting.Lifetime
}
AppSettings --> KeyVault
AppSettings --> Logging
Logging --> LogLevelSettings
9. 🏁 Conclusion
This appsettings.json file provides a clean, scalable, and secure foundation for:
- Secret Management via Azure Key Vault.
- Flexible Logging using ASP.NET Core’s config‑driven approach.
By leveraging the built‑in configuration and logging systems, IrbyImaging ensures maintainability, security, and observability across all environments. 🚀
Happy Coding! 🎉