From e549a9b0d7df6a284022bc178db93debba6802d4 Mon Sep 17 00:00:00 2001 From: Nik Cherednik Date: Sun, 17 Sep 2023 13:41:18 +0300 Subject: [PATCH] refactor: appsettings & env - Game don't need secrets. It's a client app --- src/Apps/GameDesktop/ConfigurationFactory.cs | 29 +++++++++++++ src/Apps/GameDesktop/GameCompositionRoot.cs | 5 +++ src/Apps/GameDesktop/GameDesktop.csproj | 21 +++++++--- src/Apps/GameDesktop/LogFactory.cs | 33 ++------------- .../GameDesktop/LoggingCompositionRoot.cs | 9 ---- src/Apps/GameDesktop/Program.cs | 7 +++- .../Properties/launchSettings.json | 11 +++++ .../GameDesktop/appsettings.Development.json | 2 + .../GameDesktop/appsettings.Production.json | 2 + src/Apps/GameDesktop/appsettings.json | 42 +++++++++++++++++++ 10 files changed, 115 insertions(+), 46 deletions(-) create mode 100644 src/Apps/GameDesktop/ConfigurationFactory.cs delete mode 100644 src/Apps/GameDesktop/LoggingCompositionRoot.cs create mode 100644 src/Apps/GameDesktop/Properties/launchSettings.json create mode 100644 src/Apps/GameDesktop/appsettings.Development.json create mode 100644 src/Apps/GameDesktop/appsettings.Production.json create mode 100644 src/Apps/GameDesktop/appsettings.json diff --git a/src/Apps/GameDesktop/ConfigurationFactory.cs b/src/Apps/GameDesktop/ConfigurationFactory.cs new file mode 100644 index 0000000..ee90462 --- /dev/null +++ b/src/Apps/GameDesktop/ConfigurationFactory.cs @@ -0,0 +1,29 @@ +using System; +using System.IO; +using Microsoft.Extensions.Configuration; + +namespace GameDesktop; + +internal static class ConfigurationFactory +{ + private const string AppSettingsName = "appsettings"; + private const string EnvironmentVariableName = "DOTNET_ENVIRONMENT"; + + private static string BasePath => Directory.GetParent(AppContext.BaseDirectory)!.FullName; + + private static string CurrentEnvironment => + Environment.GetEnvironmentVariable(EnvironmentVariableName) ?? "Production"; + + private static readonly string EnvironmentAppSettingsName = + $"{AppSettingsName}.{CurrentEnvironment}"; + + private static string BuildJsonFileName(string fileName) => $"{fileName}.json"; + + public static IConfigurationRoot Create() => + new ConfigurationBuilder() + .SetBasePath(BasePath) + .AddJsonFile(BuildJsonFileName(AppSettingsName), optional: false, reloadOnChange: true) + .AddJsonFile(BuildJsonFileName(EnvironmentAppSettingsName), optional: true) + .AddEnvironmentVariables() + .Build(); +} diff --git a/src/Apps/GameDesktop/GameCompositionRoot.cs b/src/Apps/GameDesktop/GameCompositionRoot.cs index a2e06d9..f05323b 100644 --- a/src/Apps/GameDesktop/GameCompositionRoot.cs +++ b/src/Apps/GameDesktop/GameCompositionRoot.cs @@ -10,6 +10,11 @@ public class GameCompositionRoot : ICompositionRoot private const bool IsMouseVisible = true; public void Compose(IServiceRegistry serviceRegistry) + { + RegisterGameServices(serviceRegistry); + } + + private void RegisterGameServices(IServiceRegistry serviceRegistry) { serviceRegistry.Register(_ => Contexts.sharedInstance); diff --git a/src/Apps/GameDesktop/GameDesktop.csproj b/src/Apps/GameDesktop/GameDesktop.csproj index 51dbf54..c3146e0 100644 --- a/src/Apps/GameDesktop/GameDesktop.csproj +++ b/src/Apps/GameDesktop/GameDesktop.csproj @@ -1,15 +1,11 @@  - WinExe + Exe net7.0 Major false - false default - <_TargetFrameworkDirectories>Jenny.Plugins.ReferenceAssemblyPathsPreProcessor - <_FullFrameworkReferenceAssemblyPaths>Jenny.Plugins.ReferenceAssemblyPathsPreProcessor - true app.manifest @@ -18,6 +14,15 @@ + + Always + + + Always + + + Always + @@ -37,12 +42,16 @@ - + + + + + diff --git a/src/Apps/GameDesktop/LogFactory.cs b/src/Apps/GameDesktop/LogFactory.cs index 69e37ac..082d317 100644 --- a/src/Apps/GameDesktop/LogFactory.cs +++ b/src/Apps/GameDesktop/LogFactory.cs @@ -1,38 +1,13 @@ -using Serilog; +using Microsoft.Extensions.Configuration; +using Serilog; using Serilog.Core; -using Serilog.Events; namespace GameDesktop; internal static class LogFactory { - private const LogEventLevel MinimumLevel = LogEventLevel.Verbose; - private const string FilePath = "logs/log-.txt"; - - private const string OutputTemplate = - "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] [{SourceContext}] {Message:lj} {NewLine}{Exception}"; - - private const bool DoSentryDebug = false; - private const LogEventLevel SentryMinimumBreadcrumbLevel = LogEventLevel.Debug; - - // TODO: Put it in env - private const string SentryDsn = - "https://ff3f6fec4457d740ab0a98c123e77086@o4505883399487488.ingest.sentry.io/4505883401388032"; - - public static Logger Create() => + public static Logger Create(IConfiguration configuration) => new LoggerConfiguration() - .MinimumLevel.Is(MinimumLevel) - .Enrich.FromLogContext() - .WriteTo.File(FilePath, - outputTemplate: OutputTemplate, - rollingInterval: RollingInterval.Day) - .WriteTo.Console(outputTemplate: OutputTemplate) - .WriteTo.Sentry(options => - { - // Debug and higher are stored as breadcrumbs (default is Information) - options.MinimumBreadcrumbLevel = SentryMinimumBreadcrumbLevel; - options.Debug = DoSentryDebug; - options.Dsn = SentryDsn; - }) + .ReadFrom.Configuration(configuration) .CreateLogger(); } diff --git a/src/Apps/GameDesktop/LoggingCompositionRoot.cs b/src/Apps/GameDesktop/LoggingCompositionRoot.cs deleted file mode 100644 index 277e656..0000000 --- a/src/Apps/GameDesktop/LoggingCompositionRoot.cs +++ /dev/null @@ -1,9 +0,0 @@ -using LightInject; - -namespace GameDesktop; - -public class LoggingCompositionRoot : ICompositionRoot -{ - public void Compose(IServiceRegistry serviceRegistry) => - serviceRegistry.Register(_ => LogFactory.Create(), new PerContainerLifetime()); -} diff --git a/src/Apps/GameDesktop/Program.cs b/src/Apps/GameDesktop/Program.cs index 554974b..4741c91 100644 --- a/src/Apps/GameDesktop/Program.cs +++ b/src/Apps/GameDesktop/Program.cs @@ -1,19 +1,22 @@ using System; using GameDesktop; using LightInject; +using Microsoft.Extensions.Configuration; using Serilog.Core; -using Logger logger = LogFactory.Create(); +IConfigurationRoot configuration = ConfigurationFactory.Create(); +using Logger logger = LogFactory.Create(configuration); ContainerOptions containerOptions = new ContainerOptions { - LogFactory = _ => entry => logger.ForContext().Verbose("{EntryMessage}", entry.Message) + LogFactory = _ => entry => logger.ForContext().Verbose($"{entry.Message}") }; using ServiceContainer container = new(containerOptions); try { + container.Register(_ => configuration, new PerContainerLifetime()); container.Register(_ => logger, new PerContainerLifetime()); container.RegisterFrom(); diff --git a/src/Apps/GameDesktop/Properties/launchSettings.json b/src/Apps/GameDesktop/Properties/launchSettings.json new file mode 100644 index 0000000..631b2d0 --- /dev/null +++ b/src/Apps/GameDesktop/Properties/launchSettings.json @@ -0,0 +1,11 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "profiles": { + "GameDesktop": { + "commandName": "Project", + "environmentVariables": { + "DOTNET_ENVIRONMENT": "Development" + } + } + } +} diff --git a/src/Apps/GameDesktop/appsettings.Development.json b/src/Apps/GameDesktop/appsettings.Development.json new file mode 100644 index 0000000..8593c62 --- /dev/null +++ b/src/Apps/GameDesktop/appsettings.Development.json @@ -0,0 +1,2 @@ +{ +} \ No newline at end of file diff --git a/src/Apps/GameDesktop/appsettings.Production.json b/src/Apps/GameDesktop/appsettings.Production.json new file mode 100644 index 0000000..8593c62 --- /dev/null +++ b/src/Apps/GameDesktop/appsettings.Production.json @@ -0,0 +1,2 @@ +{ +} \ No newline at end of file diff --git a/src/Apps/GameDesktop/appsettings.json b/src/Apps/GameDesktop/appsettings.json new file mode 100644 index 0000000..fb553e3 --- /dev/null +++ b/src/Apps/GameDesktop/appsettings.json @@ -0,0 +1,42 @@ +{ + "Serilog": { + "Using": [ + "Serilog.Sinks.Console", + "Serilog.Sinks.File" + ], + "MinimumLevel": "Verbose", + "Enrich": [ + "FromLogContext" + ], + "WriteTo": [ + { + "Name": "Console", + "Args": { + "outputTemplate": "[{Level:u3}] [{SourceContext}] {Message:lj} {NewLine}{Exception}", + "theme": "Serilog.Sinks.SystemConsole.Themes.SystemConsoleTheme::Colored, Serilog.Sinks.Console" + } + }, + { + "Name": "File", + "Args": { + "path": "%APPDATA%/GameDesktop/Logs/log-.txt", + "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] [{SourceContext}] {Message:lj} {NewLine}{Exception}", + "rollingInterval": "Day" + } + }, + { + "Name": "Sentry", + "Args": { + "EnableTracing": true, + "MaxRequestBodySize": "Always", + "SendDefaultPii": true, + "IncludeActivityData": true, + "AttachStackTrace": true, + "Debug": true, + "Dsn": "https://ff3f6fec4457d740ab0a98c123e77086@o4505883399487488.ingest.sentry.io/4505883401388032", + "DiagnosticLevel": "Error" + } + } + ] + } +} \ No newline at end of file