Skip to content

Commit

Permalink
feat(app): add EnvironmentName app options to define current deployme…
Browse files Browse the repository at this point in the history
…nt environment
  • Loading branch information
SonicGD committed Dec 2, 2022
1 parent f38b510 commit 80a9a68
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 18 deletions.
1 change: 1 addition & 0 deletions src/Sitko.Core.App/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ protected Application(string[] args)
public static string OptionsKey => nameof(Application);

public Guid Id { get; } = Guid.NewGuid();
public string Environment => GetApplicationOptions().Environment;

public string Name => GetApplicationOptions().Name;
public string Version => GetApplicationOptions().Version;
Expand Down
20 changes: 10 additions & 10 deletions src/Sitko.Core.App/ApplicationOptions.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
namespace Sitko.Core.App
namespace Sitko.Core.App;

public class ApplicationOptions
{
public class ApplicationOptions
{
public const string BaseConsoleLogFormat =
"[{Timestamp:HH:mm:ss} {Level:u3} {SourceContext}]{NewLine}\t{Message:lj}{NewLine}{Exception}";
public const string BaseConsoleLogFormat =
"[{Timestamp:HH:mm:ss} {Level:u3} {SourceContext}]{NewLine}\t{Message:lj}{NewLine}{Exception}";

public string Name { get; set; } = "";
public string Version { get; set; } = "";
public string Name { get; set; } = "";
public string Version { get; set; } = "";
public string Environment { get; set; } = "";

public string ConsoleLogFormat { get; set; } = BaseConsoleLogFormat;
public bool? EnableConsoleLogging { get; set; }
}
public string ConsoleLogFormat { get; set; } = BaseConsoleLogFormat;
public bool? EnableConsoleLogging { get; set; }
}
5 changes: 2 additions & 3 deletions src/Sitko.Core.App/HostedApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ protected IHostBuilder ConfigureHostBuilder(Action<IHostBuilder>? configure = nu

var startEnvironment = new HostingEnvironment
{
ApplicationName = GetType().Assembly.FullName,
EnvironmentName = EnvHelper.GetEnvironmentName()
ApplicationName = GetType().Assembly.FullName, EnvironmentName = EnvHelper.GetEnvironmentName()
};

var configBuilder = new ConfigurationBuilder()
Expand Down Expand Up @@ -245,7 +244,7 @@ public HostedApplicationContext(Application application, IConfiguration configur
configuration) =>
this.environment = environment;

public override string EnvironmentName => environment.EnvironmentName;
public override string AspNetEnvironmentName => environment.EnvironmentName;
public override bool IsDevelopment() => environment.IsDevelopment();

public override bool IsProduction() => environment.IsProduction();
Expand Down
12 changes: 10 additions & 2 deletions src/Sitko.Core.App/IApplicationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public interface IApplicationContext
ApplicationOptions Options { get; }
IConfiguration Configuration { get; }
ILogger Logger { get; }
string EnvironmentName { get; }
string Environment { get; }
string AspNetEnvironmentName { get; }
bool IsDevelopment();
bool IsProduction();
}
Expand All @@ -42,10 +43,11 @@ protected BaseApplicationContext(Application application, IConfiguration configu
public Guid Id { get; } = Guid.NewGuid();
public string Name => Options.Name;
public string Version => Options.Version;
public string Environment => Options.Environment;

public IConfiguration Configuration { get; }
public ILogger Logger { get; }
public abstract string EnvironmentName { get; }
public abstract string AspNetEnvironmentName { get; }
public abstract bool IsDevelopment();

public abstract bool IsProduction();
Expand All @@ -69,6 +71,11 @@ private ApplicationOptions GetApplicationOptions()
applicationOptions.Version = application.GetType().Assembly.GetName().Version?.ToString() ?? "dev";
}

if (string.IsNullOrEmpty(applicationOptions.Environment))
{
applicationOptions.Version = AspNetEnvironmentName;
}

ConfigureApplicationOptions(applicationOptions);
return applicationOptions;
}
Expand All @@ -77,3 +84,4 @@ protected virtual void ConfigureApplicationOptions(ApplicationOptions options)
{
}
}

3 changes: 2 additions & 1 deletion src/Sitko.Core.Blazor.Wasm/WasmApplicationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ public class WasmApplicationContext : BaseApplicationContext
: base(application, configuration) =>
this.environment = environment;

public override string EnvironmentName => environment.Environment;
public override string AspNetEnvironmentName => environment.Environment;
public override bool IsDevelopment() => environment.IsDevelopment();

public override bool IsProduction() => environment.IsProduction();
}

2 changes: 1 addition & 1 deletion src/Sitko.Core.ElasticStack/ElasticStackModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public class ElasticStackModule : BaseApplicationModule<ElasticStackModuleOption
if (options.LoggingEnabled)
{
var rolloverAlias = string.IsNullOrEmpty(options.LoggingLiferRolloverAlias)
? $"dotnet-logs-{context.Name.ToLower(CultureInfo.InvariantCulture).Replace(".", "-")}-{context.EnvironmentName.ToLower(CultureInfo.InvariantCulture).Replace(".", "-")}"
? $"dotnet-logs-{context.Name.ToLower(CultureInfo.InvariantCulture).Replace(".", "-")}-{context.Environment.ToLower(CultureInfo.InvariantCulture).Replace(".", "-")}"
: options.LoggingLiferRolloverAlias;
var sinkOptions = new ElasticsearchSinkOptions(options.ElasticSearchUrls)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Sitko.Core.Xunit/BaseTestScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public async Task ConfigureAsync(string name, ITestOutputHelper testOutputHelper
scopeApplication.ConfigureAppConfiguration((applicationContext, builder) =>
{
builder.AddJsonFile("appsettings.json", true);
builder.AddJsonFile($"appsettings.{applicationContext.EnvironmentName}.json", true);
builder.AddJsonFile($"appsettings.{applicationContext.AspNetEnvironmentName}.json", true);
});

scopeApplication.ConfigureServices((context, services) =>
Expand Down

0 comments on commit 80a9a68

Please sign in to comment.