From 94ea7502a3aca0f87c6b3bca7bab35d35f58cf74 Mon Sep 17 00:00:00 2001 From: Daniel Wathen Date: Tue, 17 Jan 2023 15:25:20 -0700 Subject: [PATCH] Created appsettings class to pull from appsettings.json files --- ReportingServices.API/Models/AppSettings.cs | 23 +++++ .../Models/Binder/AppSettings.cs | 77 +++++++++++++++++ ReportingServices.API/Program.cs | 8 +- .../appsettings.Development.json | 50 ++++++++--- .../appsettings.Release.json | 12 --- ReportingServices.API/appsettings.json | 20 +++-- .../ScrapeDatabaseRepository.cs | 2 +- .../Controllers/ProductionReportController.cs | 11 ++- ReportingServices.UI/Models/AppSettings.cs | 25 ++++++ .../Models/Binder/AppSettings.cs | 85 +++++++++++++++++++ ReportingServices.UI/Program.cs | 4 + .../appsettings.Development.json | 54 +++++++++--- ReportingServices.UI/appsettings.Release.json | 16 ---- ReportingServices.UI/appsettings.json | 28 +++--- 14 files changed, 341 insertions(+), 74 deletions(-) create mode 100644 ReportingServices.API/Models/AppSettings.cs create mode 100644 ReportingServices.API/Models/Binder/AppSettings.cs delete mode 100644 ReportingServices.API/appsettings.Release.json create mode 100644 ReportingServices.UI/Models/AppSettings.cs create mode 100644 ReportingServices.UI/Models/Binder/AppSettings.cs delete mode 100644 ReportingServices.UI/appsettings.Release.json diff --git a/ReportingServices.API/Models/AppSettings.cs b/ReportingServices.API/Models/AppSettings.cs new file mode 100644 index 0000000..1905f1c --- /dev/null +++ b/ReportingServices.API/Models/AppSettings.cs @@ -0,0 +1,23 @@ +using System.Text.Json; + +namespace ReportingServices.API.Models; + +public record AppSettings(string BuildNumber, + string Company, + string ConnectionString, + string GitCommitSeven, + string LoggingDirectory, + bool IsDevelopment, + bool IsStaging, + string MonAResource, + string MonASite, + string URLs) +{ + + public override string ToString() + { + string result = JsonSerializer.Serialize(this, new JsonSerializerOptions() { WriteIndented = true }); + return result; + } + +} \ No newline at end of file diff --git a/ReportingServices.API/Models/Binder/AppSettings.cs b/ReportingServices.API/Models/Binder/AppSettings.cs new file mode 100644 index 0000000..111f39f --- /dev/null +++ b/ReportingServices.API/Models/Binder/AppSettings.cs @@ -0,0 +1,77 @@ +using System.ComponentModel.DataAnnotations; +using System.Text.Json; + +namespace ReportingServices.API.Models.Binder; + +public class AppSettings +{ + +#nullable disable + + [Display(Name = "Build Number"), Required] public string BuildNumber { get; set; } + [Display(Name = "Company"), Required] public string Company { get; set; } + [Display(Name = "Connection String"), Required] public string ConnectionString { get; set; } + [Display(Name = "Git Commit Seven"), Required] public string GitCommitSeven { get; set; } + [Display(Name = "Logging Directory"), Required] public string LoggingDirectory { get; set; } + [Display(Name = "Is Development"), Required] public bool? IsDevelopment { get; set; } + [Display(Name = "Is Staging"), Required] public bool? IsStaging { get; set; } + [Display(Name = "MonA Resource"), Required] public string MonAResource { get; set; } + [Display(Name = "MonA Site"), Required] public string MonASite { get; set; } + [Display(Name = "URLs"), Required] public string URLs { get; set; } + +#nullable enable + + public override string ToString() + { + string result = JsonSerializer.Serialize(this, new JsonSerializerOptions() { WriteIndented = true }); + return result; + } + + private static Models.AppSettings Get(AppSettings? appSettings) + { + Models.AppSettings result; + if (appSettings is null) + throw new NullReferenceException(nameof(appSettings)); + if (appSettings.BuildNumber is null) + throw new NullReferenceException(nameof(BuildNumber)); + if (appSettings.Company is null) + throw new NullReferenceException(nameof(Company)); + if (appSettings.ConnectionString is null) + throw new NullReferenceException(nameof(ConnectionString)); + if (appSettings.GitCommitSeven is null) + throw new NullReferenceException(nameof(GitCommitSeven)); + if (appSettings.LoggingDirectory is null) + throw new NullReferenceException(nameof(LoggingDirectory)); + if (appSettings.IsDevelopment is null) + throw new NullReferenceException(nameof(IsDevelopment)); + if (appSettings.IsStaging is null) + throw new NullReferenceException(nameof(IsStaging)); + if (appSettings.MonAResource is null) + throw new NullReferenceException(nameof(MonAResource)); + if (appSettings.MonASite is null) + throw new NullReferenceException(nameof(MonASite)); + if (appSettings.URLs is null) + throw new NullReferenceException(nameof(URLs)); + result = new( + appSettings.BuildNumber, + appSettings.Company, + appSettings.ConnectionString, + appSettings.GitCommitSeven, + appSettings.LoggingDirectory, + appSettings.IsDevelopment.Value, + appSettings.IsStaging.Value, + appSettings.MonAResource, + appSettings.MonASite, + appSettings.URLs); + return result; + } + + public static Models.AppSettings Get(IConfigurationRoot configurationRoot) + { + Models.AppSettings result; + AppSettings? appSettings = configurationRoot.Get(); + result = Get(appSettings); + return result; + } + +} \ No newline at end of file diff --git a/ReportingServices.API/Program.cs b/ReportingServices.API/Program.cs index c790913..80ceb00 100644 --- a/ReportingServices.API/Program.cs +++ b/ReportingServices.API/Program.cs @@ -1,3 +1,4 @@ +using ReportingServices.API.Models; using ReportingServices.Shared.Repositories; using Serilog; @@ -8,7 +9,9 @@ internal class Program LoggerConfiguration loggerConfiguration = new(); WebApplicationBuilder builder = WebApplication.CreateBuilder(args); - Environment.SetEnvironmentVariable("workingDirectory", "D:\\tmp\\logging\\MesaReportingServices\\API"); + _ = builder.Configuration.AddUserSecrets(); + AppSettings appSettings = ReportingServices.API.Models.Binder.AppSettings.Get(builder.Configuration); + Environment.SetEnvironmentVariable("workingDirectory", appSettings.LoggingDirectory); _ = loggerConfiguration.ReadFrom.Configuration(builder.Configuration); _ = builder.Host.UseSerilog(); Log.Logger = loggerConfiguration.CreateLogger(); @@ -18,7 +21,8 @@ internal class Program // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle _ = builder.Services.AddEndpointsApiExplorer(); _ = builder.Services.AddSwaggerGen(); - _ = builder.Services.AddScoped(); + _ = builder.Services.AddSingleton(_ => appSettings); + _ = builder.Services.AddScoped(_ => new(appSettings.ConnectionString)); WebApplication app = builder.Build(); diff --git a/ReportingServices.API/appsettings.Development.json b/ReportingServices.API/appsettings.Development.json index 2e004db..06934ea 100644 --- a/ReportingServices.API/appsettings.Development.json +++ b/ReportingServices.API/appsettings.Development.json @@ -1,12 +1,42 @@ { - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft.AspNetCore": "Warning" + "ConnectionString": "Server=MESSV01EC.EC.LOCAL\\PROD1,53959;Database=LSL2SQL;User Id=srpadmin;Password=0okm9ijn;TrustServerCertificate=true", + "IsDevelopment": true, + "MonAResource": "ReportingServicesApiIfx", + "Serilog": { + "Using": [ + "Serilog.Sinks.Console", + "Serilog.Sinks.File" + ], + "MinimumLevel": "Information", + "WriteTo": [ + { + "Name": "Debug", + "Args": { + "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] ({SourceContext}.{MethodName} ({InstanceId}) ({RemoteIpAddress}) {Message}{NewLine}{Exception}" + } + }, + { + "Name": "Console", + "Args": { + "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] ({SourceContext}.{MethodName} ({InstanceId}) ({RemoteIpAddress}) {Message}{NewLine}{Exception}" + } + }, + { + "Name": "File", + "Args": { + "path": "%workingDirectory% - Log/log-.txt", + "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] ({SourceContext}.{MethodName} ({InstanceId}) ({RemoteIpAddress}) {Message}{NewLine}{Exception}", + "rollingInterval": "Hour" + } + } + ], + "Enrich": [ + "FromLogContext", + "WithMachineName", + "WithThreadId" + ], + "Properties": { + "Application": "Sample" } - }, - "ConnectionStrings": { - "DefaultConnection": "Server=MESSV01EC.EC.LOCAL\\PROD1,53959;Database=LSL2SQL;User Id=srpadmin;Password=0okm9ijn;TrustServerCertificate=true" - }, - "LoggingDirectory": "C:\\tmp\\logging" -} + } +} \ No newline at end of file diff --git a/ReportingServices.API/appsettings.Release.json b/ReportingServices.API/appsettings.Release.json deleted file mode 100644 index 5a2241d..0000000 --- a/ReportingServices.API/appsettings.Release.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft.AspNetCore": "Warning" - } - }, - "ConnectionStrings": { - "DefaultConnection": "Server=MESSV01EC.EC.LOCAL\\PROD1,53959;Database=LSL2SQL;User Id=srpadmin;Password=0okm9ijn;TrustServerCertificate=true" - }, - "LoggingDirectory": "D:\\tmp\\logging\\MesaReportingServices\\API" -} diff --git a/ReportingServices.API/appsettings.json b/ReportingServices.API/appsettings.json index d669507..08c4d9c 100644 --- a/ReportingServices.API/appsettings.json +++ b/ReportingServices.API/appsettings.json @@ -1,11 +1,22 @@ { + "AllowedHosts": "*", + "BuildNumber": "1", + "Company": "Infineon Technologies Americas Corp.", + "ConnectionString": "Server=MESSV01EC.EC.LOCAL\\PROD1,53959;Database=LSL2SQL;User Id=srpadmin;Password=0okm9ijn;TrustServerCertificate=true", + "GitCommitSeven": "1234567", "Logging": { "LogLevel": { "Default": "Information", - "Microsoft.AspNetCore": "Warning" + "Microsoft": "Warning", + "Log4netProvider": "Debug", + "Microsoft.Hosting.Lifetime": "Information" } }, - "AllowedHosts": "*", + "LoggingDirectory": "D:/tmp/logging/MesaReportingServices/API", + "IsDevelopment": false, + "IsStaging": false, + "MonAResource": "ReportingServicesApiEc", + "MonASite": "auc", "Serilog": { "Using": [ "Serilog.Sinks.Console", @@ -41,7 +52,6 @@ ], "Properties": { "Application": "Sample" - }, - "LoggingDirectory": "D:\\tmp\\logging\\MesaReportingServices\\API" + } } -} +} \ No newline at end of file diff --git a/ReportingServices.Shared/Repositories/Implementations/ScrapeDatabaseRepository.cs b/ReportingServices.Shared/Repositories/Implementations/ScrapeDatabaseRepository.cs index 85de12b..60ce065 100644 --- a/ReportingServices.Shared/Repositories/Implementations/ScrapeDatabaseRepository.cs +++ b/ReportingServices.Shared/Repositories/Implementations/ScrapeDatabaseRepository.cs @@ -10,7 +10,7 @@ public class ScrapeDatabaseRepository : IScrapeDatabaseRepository private SqlConnection _connection; private readonly string _connectionString; - public ScrapeDatabaseRepository() => _connectionString = "Server=MESSV01EC.EC.LOCAL\\PROD1,53959;Database=LSL2SQL;User Id=srpadmin;Password=0okm9ijn;TrustServerCertificate=true"; + public ScrapeDatabaseRepository(string connectionString) => _connectionString = connectionString; public void OpenConnection() { diff --git a/ReportingServices.UI/Controllers/ProductionReportController.cs b/ReportingServices.UI/Controllers/ProductionReportController.cs index e11ec9c..fc66e87 100644 --- a/ReportingServices.UI/Controllers/ProductionReportController.cs +++ b/ReportingServices.UI/Controllers/ProductionReportController.cs @@ -2,20 +2,23 @@ using ReportingServices.Shared.HelperClasses; using ReportingServices.Shared.Models.ProductionReport; using ReportingServices.Shared.ViewModels.ProductionReport; +using ReportingServices.UI.Models; namespace ReportingServices.UI.Controllers; public class ProductionReportController : Controller { private readonly ILogger _logger; - private readonly string _dailyRptFilePath = "wwwroot/Assets/DailyReportInfo.json"; - private readonly string _toolStateOwnerFilePath = "wwwroot/Assets/ToolStates.json"; + private readonly string _dailyRptFilePath; + private readonly string _toolStateOwnerFilePath; private readonly string _baseDBUrl; - public ProductionReportController(ILogger logger) + public ProductionReportController(ILogger logger, AppSettings appSettings) { _logger = logger; - _baseDBUrl = "http://localhost:50201/api/ScrapeDB/"; + _baseDBUrl = appSettings.BaseAPIAddress; + _dailyRptFilePath = appSettings.DailyReportFilePath; + _toolStateOwnerFilePath = appSettings.ToolStateOwnerFilePath; _logger.LogInformation("Base Database Address: {baseUrl}", _baseDBUrl); } diff --git a/ReportingServices.UI/Models/AppSettings.cs b/ReportingServices.UI/Models/AppSettings.cs new file mode 100644 index 0000000..dc935f4 --- /dev/null +++ b/ReportingServices.UI/Models/AppSettings.cs @@ -0,0 +1,25 @@ +using System.Text.Json; + +namespace ReportingServices.UI.Models; + +public record AppSettings(string BaseAPIAddress, + string BuildNumber, + string Company, + string DailyReportFilePath, + string GitCommitSeven, + string LoggingDirectory, + bool IsDevelopment, + bool IsStaging, + string MonAResource, + string MonASite, + string ToolStateOwnerFilePath, + string URLs) +{ + + public override string ToString() + { + string result = JsonSerializer.Serialize(this, new JsonSerializerOptions() { WriteIndented = true }); + return result; + } + +} \ No newline at end of file diff --git a/ReportingServices.UI/Models/Binder/AppSettings.cs b/ReportingServices.UI/Models/Binder/AppSettings.cs new file mode 100644 index 0000000..1a6a4e7 --- /dev/null +++ b/ReportingServices.UI/Models/Binder/AppSettings.cs @@ -0,0 +1,85 @@ +using System.ComponentModel.DataAnnotations; +using System.Text.Json; + +namespace ReportingServices.UI.Models.Binder; + +public class AppSettings +{ + +#nullable disable + + [Display(Name = "Base API Address"), Required] public string BaseAPIAddress { get; set; } + [Display(Name = "Build Number"), Required] public string BuildNumber { get; set; } + [Display(Name = "Company"), Required] public string Company { get; set; } + [Display(Name = "Daily Report File Path"), Required] public string DailyReportFilePath { get; set; } + [Display(Name = "Git Commit Seven"), Required] public string GitCommitSeven { get; set; } + [Display(Name = "Logging Directory"), Required] public string LoggingDirectory { get; set; } + [Display(Name = "Is Development"), Required] public bool? IsDevelopment { get; set; } + [Display(Name = "Is Staging"), Required] public bool? IsStaging { get; set; } + [Display(Name = "MonA Resource"), Required] public string MonAResource { get; set; } + [Display(Name = "MonA Site"), Required] public string MonASite { get; set; } + [Display(Name = "Tool State Owner File Path"), Required] public string ToolStateOwnerFilePath { get; set; } + [Display(Name = "URLs"), Required] public string URLs { get; set; } + +#nullable enable + + public override string ToString() + { + string result = JsonSerializer.Serialize(this, new JsonSerializerOptions() { WriteIndented = true }); + return result; + } + + private static Models.AppSettings Get(AppSettings? appSettings) + { + Models.AppSettings result; + if (appSettings is null) + throw new NullReferenceException(nameof(appSettings)); + if (appSettings.BaseAPIAddress is null) + throw new NullReferenceException(nameof(BaseAPIAddress)); + if (appSettings.BuildNumber is null) + throw new NullReferenceException(nameof(BuildNumber)); + if (appSettings.Company is null) + throw new NullReferenceException(nameof(Company)); + if (appSettings.DailyReportFilePath is null) + throw new NullReferenceException(nameof(DailyReportFilePath)); + if (appSettings.GitCommitSeven is null) + throw new NullReferenceException(nameof(GitCommitSeven)); + if (appSettings.LoggingDirectory is null) + throw new NullReferenceException(nameof(LoggingDirectory)); + if (appSettings.IsDevelopment is null) + throw new NullReferenceException(nameof(IsDevelopment)); + if (appSettings.IsStaging is null) + throw new NullReferenceException(nameof(IsStaging)); + if (appSettings.MonAResource is null) + throw new NullReferenceException(nameof(MonAResource)); + if (appSettings.MonASite is null) + throw new NullReferenceException(nameof(MonASite)); + if (appSettings.URLs is null) + throw new NullReferenceException(nameof(URLs)); + if (appSettings.ToolStateOwnerFilePath is null) + throw new NullReferenceException(nameof(ToolStateOwnerFilePath)); + result = new( + appSettings.BaseAPIAddress, + appSettings.BuildNumber, + appSettings.Company, + appSettings.DailyReportFilePath, + appSettings.GitCommitSeven, + appSettings.LoggingDirectory, + appSettings.IsDevelopment.Value, + appSettings.IsStaging.Value, + appSettings.MonAResource, + appSettings.MonASite, + appSettings.ToolStateOwnerFilePath, + appSettings.URLs); + return result; + } + + public static Models.AppSettings Get(IConfigurationRoot configurationRoot) + { + Models.AppSettings result; + AppSettings? appSettings = configurationRoot.Get(); + result = Get(appSettings); + return result; + } + +} \ No newline at end of file diff --git a/ReportingServices.UI/Program.cs b/ReportingServices.UI/Program.cs index 16473d3..d6f1740 100644 --- a/ReportingServices.UI/Program.cs +++ b/ReportingServices.UI/Program.cs @@ -1,3 +1,4 @@ +using ReportingServices.UI.Models; using Serilog; internal class Program @@ -7,6 +8,8 @@ internal class Program LoggerConfiguration loggerConfiguration = new(); WebApplicationBuilder builder = WebApplication.CreateBuilder(args); + _ = builder.Configuration.AddUserSecrets(); + AppSettings appSettings = ReportingServices.UI.Models.Binder.AppSettings.Get(builder.Configuration); Environment.SetEnvironmentVariable("workingDirectory", "D:\\tmp\\logging\\MesaReportingServices\\UI"); _ = loggerConfiguration.ReadFrom.Configuration(builder.Configuration); _ = builder.Host.UseSerilog(); @@ -15,6 +18,7 @@ internal class Program // Add services to the container. _ = builder.Services.AddControllersWithViews(); + _ = builder.Services.AddSingleton(_ => appSettings); WebApplication app = builder.Build(); diff --git a/ReportingServices.UI/appsettings.Development.json b/ReportingServices.UI/appsettings.Development.json index 7de1aa2..262bcdb 100644 --- a/ReportingServices.UI/appsettings.Development.json +++ b/ReportingServices.UI/appsettings.Development.json @@ -1,16 +1,42 @@ { - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft.AspNetCore": "Warning" - } - }, + "IsDevelopment": true, + "BaseAPIAddress": "https://localhost:7196/api/ScrapeDB/", + "MonAResource": "ReportingServicesUiIfx", "Serilog": { - "MinimumLevel": "Debug" - }, - "ConnectionStrings": { - "DefaultConnection": "Server=MESSV01EC.EC.LOCAL\\PROD1,53959;Database=LSL2SQL;User Id=srpadmin;Password=0okm9ijn;TrustServerCertificate=true" - }, - "BaseAPIAddress": "https://localhost:7196/api/", - "LoggingDirectory": "C:\\tmp\\logging" -} + "Using": [ + "Serilog.Sinks.Console", + "Serilog.Sinks.File" + ], + "MinimumLevel": "Debug", + "WriteTo": [ + { + "Name": "Debug", + "Args": { + "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] ({SourceContext}.{MethodName}) ({InstanceId}) ({RemoteIpAddress}) {Message}{NewLine}{Exception}" + } + }, + { + "Name": "Console", + "Args": { + "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] ({SourceContext}.{MethodName}) ({InstanceId}) ({RemoteIpAddress}) {Message}{NewLine}{Exception}" + } + }, + { + "Name": "File", + "Args": { + "path": "%workingDirectory% - Log/log-.txt", + "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] ({SourceContext}.{MethodName}) ({InstanceId}) ({RemoteIpAddress}) {Message}{NewLine}{Exception}", + "rollingInterval": "Hour" + } + } + ], + "Enrich": [ + "FromLogContext", + "WithMachineName", + "WithThreadId" + ], + "Properties": { + "Application": "Sample" + } + } +} \ No newline at end of file diff --git a/ReportingServices.UI/appsettings.Release.json b/ReportingServices.UI/appsettings.Release.json deleted file mode 100644 index 4a358b6..0000000 --- a/ReportingServices.UI/appsettings.Release.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft.AspNetCore": "Warning" - } - }, - "Serilog": { - "MinimumLevel": "Debug" - }, - "ConnectionStrings": { - "DefaultConnection": "Server=MESSV01EC.EC.LOCAL\\PROD1,53959;Database=LSL2SQL;User Id=srpadmin;Password=0okm9ijn;TrustServerCertificate=true" - }, - "BaseAPIAddress": "http://localhost:50201/api/", - "LoggingDirectory": "D:\\tmp\\logging\\MesaReportingServices\\UI" -} diff --git a/ReportingServices.UI/appsettings.json b/ReportingServices.UI/appsettings.json index 5382c35..88a8e04 100644 --- a/ReportingServices.UI/appsettings.json +++ b/ReportingServices.UI/appsettings.json @@ -1,14 +1,23 @@ { + "AllowedHosts": "*", + "BaseAPIAddress": "http://localhost:50201/api/ScrapeDB/", + "BuildNumber": "1", + "Company": "Infineon Technologies Americas Corp.", + "DailyReportFilePath": "wwwroot/Assets/DailyReportInfo.json", + "GitCommitSeven": "1234567", "Logging": { "LogLevel": { "Default": "Information", - "Microsoft.AspNetCore": "Warning" + "Microsoft": "Warning", + "Log4netProvider": "Debug", + "Microsoft.Hosting.Lifetime": "Information" } }, - "ConnectionStrings": { - "DefaultConnection": "Server=MESSV01EC.EC.LOCAL\\PROD1,53959;Database=LSL2SQL;User Id=srpadmin;Password=0okm9ijn;TrustServerCertificate=true" - }, - "AllowedHosts": "*", + "LoggingDirectory": "D:/tmp/logging/MesaReportingServices/UI", + "IsDevelopment": false, + "IsStaging": false, + "MonAResource": "ReportingServicesUiEc", + "MonASite": "auc", "Serilog": { "Using": [ "Serilog.Sinks.Console", @@ -44,8 +53,7 @@ ], "Properties": { "Application": "Sample" - }, - "BaseAPIAddress": "http://localhost:50201/api/", - "LoggingDirectory": "D:\\tmp\\logging\\MesaReportingServices\\UI" - } -} + } + }, + "ToolStateOwnerFilePath": "wwwroot/Assets/ToolStates.json" +} \ No newline at end of file