Skip to content

Commit

Permalink
refactor: appsettings & env
Browse files Browse the repository at this point in the history
- Game don't need secrets. It's a client app
  • Loading branch information
cherrynik committed Sep 17, 2023
1 parent 7f63c90 commit e549a9b
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 46 deletions.
29 changes: 29 additions & 0 deletions src/Apps/GameDesktop/ConfigurationFactory.cs
Original file line number Diff line number Diff line change
@@ -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();
}
5 changes: 5 additions & 0 deletions src/Apps/GameDesktop/GameCompositionRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
21 changes: 15 additions & 6 deletions src/Apps/GameDesktop/GameDesktop.csproj
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<RollForward>Major</RollForward>
<PublishReadyToRun>false</PublishReadyToRun>
<TieredCompilation>false</TieredCompilation>
<LangVersion>default</LangVersion>
<_TargetFrameworkDirectories>Jenny.Plugins.ReferenceAssemblyPathsPreProcessor</_TargetFrameworkDirectories>
<_FullFrameworkReferenceAssemblyPaths>Jenny.Plugins.ReferenceAssemblyPathsPreProcessor</_FullFrameworkReferenceAssemblyPaths>
<DisableHandlePackageFileConflicts>true</DisableHandlePackageFileConflicts>
</PropertyGroup>
<PropertyGroup>
<ApplicationManifest>app.manifest</ApplicationManifest>
Expand All @@ -18,6 +14,15 @@
<ItemGroup>
<None Remove="Icon.ico" />
<None Remove="Icon.bmp" />
<None Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="appsettings.Development.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="appsettings.Production.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Icon.ico" />
Expand All @@ -37,12 +42,16 @@
<PackageReference Include="Entitas.CodeGeneration.Plugins" Version="1.14.2" />
<PackageReference Include="Entitas.Roslyn.CodeGeneration.Plugins" Version="1.14.2" />
<PackageReference Include="LightInject" Version="6.6.4" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
<PackageReference Include="MonoGame.Aseprite" Version="5.1.0" />
<PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.1.303" />
<PackageReference Include="MonoGame.Content.Builder.Task" Version="3.8.1.303" />
<PackageReference Include="Sentry" Version="3.39.1" />
<PackageReference Include="Sentry.Serilog" Version="3.39.1" />
<PackageReference Include="Serilog.Settings.Configuration" Version="7.0.1" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
<PackageReference Include="Stateless" Version="5.13.0" />
Expand Down
33 changes: 4 additions & 29 deletions src/Apps/GameDesktop/LogFactory.cs
Original file line number Diff line number Diff line change
@@ -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();
}
9 changes: 0 additions & 9 deletions src/Apps/GameDesktop/LoggingCompositionRoot.cs

This file was deleted.

7 changes: 5 additions & 2 deletions src/Apps/GameDesktop/Program.cs
Original file line number Diff line number Diff line change
@@ -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<ServiceContainer>().Verbose("{EntryMessage}", entry.Message)
LogFactory = _ => entry => logger.ForContext<ServiceContainer>().Verbose($"{entry.Message}")
};
using ServiceContainer container = new(containerOptions);

try
{
container.Register(_ => configuration, new PerContainerLifetime());
container.Register(_ => logger, new PerContainerLifetime());

container.RegisterFrom<GameCompositionRoot>();
Expand Down
11 changes: 11 additions & 0 deletions src/Apps/GameDesktop/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"profiles": {
"GameDesktop": {
"commandName": "Project",
"environmentVariables": {
"DOTNET_ENVIRONMENT": "Development"
}
}
}
}
2 changes: 2 additions & 0 deletions src/Apps/GameDesktop/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
2 changes: 2 additions & 0 deletions src/Apps/GameDesktop/appsettings.Production.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
42 changes: 42 additions & 0 deletions src/Apps/GameDesktop/appsettings.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
]
}
}

0 comments on commit e549a9b

Please sign in to comment.