This console application was created to test the SGuard.ConfigValidation library and demonstrate example usage scenarios. The application provides a command-line interface (CLI) for validating configuration files.
SGuard.ConfigValidation.Console is a console application used to validate SGuard configuration files. The application performs configuration validation operations using all features of the SGuard.ConfigValidation library.
- Configuration file validation
- Environment-based validation (Development, Staging, Production)
- Output generation in JSON and text formats
- Detailed logging support
- Configuration management with environment variables
# Validate all environments
dotnet run -- validate
# Validate a specific environment
dotnet run -- validate --env Development
# Validate all environments (explicitly)
dotnet run -- validate --all
# Get output in JSON format
dotnet run -- validate --output json
# Write output to file
dotnet run -- validate --output json --output-file results.json
# Run in verbose mode
dotnet run -- validate --verbose--config, -c: Path to configuration file (default:sguard.json)--env, -e: Environment ID to validate (if not specified, all environments are validated)--all, -a: Validate all environments--output, -o: Output format:json,text, orconsole(default:console)--output-file, -f: Output file path (if specified, results will be written to this file)--verbose, -v: Enable verbose logging mode
SGuard.ConfigValidation.Console automatically detects the environment according to .NET standards.
- DOTNET_ENVIRONMENT (Priority - .NET 6+ standard)
- ASPNETCORE_ENVIRONMENT (For backward compatibility)
- Production (Default, if none is set)
export DOTNET_ENVIRONMENT=Development
# or
export ASPNETCORE_ENVIRONMENT=Developmentset DOTNET_ENVIRONMENT=Development
# or
set ASPNETCORE_ENVIRONMENT=Development$env:DOTNET_ENVIRONMENT="Development"
# or
$env:ASPNETCORE_ENVIRONMENT="Development"Files are automatically loaded based on the environment:
appsettings.json- Always loaded (base configuration)appsettings.Development.json- Loaded in Development environmentappsettings.Staging.json- Loaded in Staging environmentappsettings.Production.json- Loaded in Production environment
Note: Environment-specific files are optional. If a file does not exist, only the base appsettings.json is used.
Environment control can be performed by injecting IHostEnvironment in services:
public class MyService
{
private readonly IHostEnvironment _environment;
public MyService(IHostEnvironment environment)
{
_environment = environment;
}
public void DoSomething()
{
if (_environment.IsDevelopment())
{
// Development-specific logic
}
if (_environment.IsProduction())
{
// Production-specific logic
}
// Custom environment check
if (_environment.IsEnvironment("Staging"))
{
// Staging-specific logic
}
}
}The IHostEnvironment interface provides the following helper methods:
IsDevelopment()- Development environment checkIsStaging()- Staging environment checkIsProduction()- Production environment checkIsEnvironment(string name)- Custom environment check
export DOTNET_ENVIRONMENT=Development
./ConsoleApp1appsettings.jsonis loadedappsettings.Development.jsonis loaded (if exists)- Debug level logging is active
export DOTNET_ENVIRONMENT=Staging
./ConsoleApp1appsettings.jsonis loadedappsettings.Staging.jsonis loaded (if exists)- Information level logging is active
export DOTNET_ENVIRONMENT=Production
# or set nothing (defaults to Production)
./ConsoleApp1appsettings.jsonis loadedappsettings.Production.jsonis loaded (if exists)- Warning level logging is active
ENV DOTNET_ENVIRONMENT=ProductionapiVersion: apps/v1
kind: Deployment
spec:
template:
spec:
containers:
- name: sguard-checker
env:
- name: DOTNET_ENVIRONMENT
value: "Production"private static void ConfigureServices(IServiceCollection services)
{
// Environment is automatically detected
var environmentName = GetEnvironmentName(); // "Development", "Staging", "Production"
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{environmentName}.json", optional: true)
.Build();
// IHostEnvironment is registered
var hostEnvironment = new HostEnvironment
{
EnvironmentName = environmentName,
// ...
};
services.AddSingleton<IHostEnvironment>(hostEnvironment);
}- Use DOTNET_ENVIRONMENT - .NET 6+ standard
- Default to Production - For security
- Make environment-specific files optional - Base config should always be loaded
- Inject IHostEnvironment - For environment control
- Keep sensitive data in environment variables - Don't write to appsettings.json
SGuard loggers are managed from the appsettings.json file. .NET's logging system provides namespace-based log level control.
Log levels are checked in the following order (from most specific to most general):
- Specific Namespace (e.g.,
SGuard.ConfigValidation.Services.ConfigLoader) - Namespace Segment (e.g.,
SGuard.ConfigValidation.Services) - Root Namespace (e.g.,
SGuard) - Default (default for all namespaces)
{
"Logging": {
"LogLevel": {
"Default": "Information",
"SGuard": "Information", // For all SGuard namespaces
"SGuard.ConfigValidation.Services": "Debug", // Only for Services
"SGuard.ConfigValidation.Validators": "Warning", // Only for Validators
"ConsoleApp1": "Information" // For CLI
}
}
}- Trace (0): Most detailed logs, typically used in development
- Debug (1): Debug information for development and troubleshooting
- Information (2): General informational messages (default)
- Warning (3): Warning messages, non-error situations that require attention
- Error (4): Error messages when an operation fails
- Critical (5): Critical errors, situations that risk application crash
- None (6): Logging disabled
{
"Logging": {
"LogLevel": {
"SGuard": "Debug"
}
}
}{
"Logging": {
"LogLevel": {
"SGuard": "Information",
"SGuard.ConfigValidation.Services": "Debug"
}
}
}{
"Logging": {
"LogLevel": {
"SGuard": "Information",
"SGuard.ConfigValidation.Validators": "Warning"
}
}
}{
"Logging": {
"LogLevel": {
"Default": "Warning",
"SGuard": "Warning",
"ConsoleApp1": "Error"
}
}
}You can also override log levels using environment variables:
# Linux/Mac
export Logging__LogLevel__SGuard=Debug
# Windows
set Logging__LogLevel__SGuard=Debugpublic class ConfigLoader
{
private readonly ILogger<ConfigLoader> _logger;
public ConfigLoader(ILogger<ConfigLoader> logger)
{
_logger = logger;
}
public void LoadConfig(string path)
{
// Log level check is performed automatically
_logger.LogTrace("Trace: Most detailed information");
_logger.LogDebug("Debug: Debug information");
_logger.LogInformation("Information: General information");
_logger.LogWarning("Warning: Warning");
_logger.LogError("Error: Error");
_logger.LogCritical("Critical: Critical error");
}
}- Namespace is automatically determined when logger is created:
ILogger<ConfigLoader>→SGuard.ConfigValidation.Services.ConfigLoader - Log level is checked from appsettings.json before writing the log message
- If the message's log level is lower than the minimum level in configuration, the log is not written
- Example:
LogLevel.Debugmessage is not written if configuration hasInformation
Log level checking is very fast and has minimal performance impact. However, be careful when using string interpolation:
// ❌ Bad: String is always created
_logger.LogDebug($"Processing {largeObject}");
// ✅ Good: String is only created if Debug is active
_logger.LogDebug("Processing {LargeObject}", largeObject);{
"Logging": {
"LogLevel": {
"Default": "Debug",
"SGuard": "Debug"
}
}
}{
"Logging": {
"LogLevel": {
"Default": "Information",
"SGuard": "Information"
}
}
}{
"Logging": {
"LogLevel": {
"Default": "Warning",
"SGuard": "Warning"
}
}
}This console application was created to test the SGuard.ConfigValidation library and demonstrate example usage scenarios. The application simulates real-world scenarios using all features of the library and can be used as a reference during development.