Created appsettings class to pull from appsettings.json files
This commit is contained in:
23
ReportingServices.API/Models/AppSettings.cs
Normal file
23
ReportingServices.API/Models/AppSettings.cs
Normal file
@ -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;
|
||||
}
|
||||
|
||||
}
|
77
ReportingServices.API/Models/Binder/AppSettings.cs
Normal file
77
ReportingServices.API/Models/Binder/AppSettings.cs
Normal file
@ -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<AppSettings>();
|
||||
result = Get(appSettings);
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
@ -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<Program>();
|
||||
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<IScrapeDatabaseRepository, ScrapeDatabaseRepository>();
|
||||
_ = builder.Services.AddSingleton(_ => appSettings);
|
||||
_ = builder.Services.AddScoped<IScrapeDatabaseRepository, ScrapeDatabaseRepository>(_ => new(appSettings.ConnectionString));
|
||||
|
||||
WebApplication app = builder.Build();
|
||||
|
||||
|
@ -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"
|
||||
}
|
||||
}
|
||||
}
|
@ -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"
|
||||
}
|
@ -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"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user