2020-11-27 22:52:52 -05:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Runtime.InteropServices;
|
2021-03-22 12:09:25 -04:00
|
|
|
|
using Data.MigrationContext;
|
2020-11-27 22:52:52 -05:00
|
|
|
|
using Microsoft.Data.Sqlite;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2020-11-11 18:31:26 -05:00
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2020-12-02 15:29:49 -05:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2020-11-11 18:31:26 -05:00
|
|
|
|
using Serilog;
|
2023-05-26 22:14:49 -04:00
|
|
|
|
using Serilog.Core;
|
2020-11-27 22:52:52 -05:00
|
|
|
|
using Serilog.Events;
|
2020-11-11 18:31:26 -05:00
|
|
|
|
using SharedLibraryCore;
|
|
|
|
|
using SharedLibraryCore.Configuration;
|
2020-12-02 15:29:49 -05:00
|
|
|
|
using ILogger = Serilog.ILogger;
|
2020-11-11 18:31:26 -05:00
|
|
|
|
|
|
|
|
|
namespace IW4MAdmin.Application.Extensions
|
|
|
|
|
{
|
|
|
|
|
public static class StartupExtensions
|
|
|
|
|
{
|
2023-05-26 22:14:49 -04:00
|
|
|
|
private static ILogger _defaultLogger;
|
|
|
|
|
private static readonly LoggingLevelSwitch LevelSwitch = new();
|
|
|
|
|
private static readonly LoggingLevelSwitch MicrosoftLevelSwitch = new();
|
|
|
|
|
private static readonly LoggingLevelSwitch SystemLevelSwitch = new();
|
2020-11-11 18:31:26 -05:00
|
|
|
|
|
|
|
|
|
public static IServiceCollection AddBaseLogger(this IServiceCollection services,
|
|
|
|
|
ApplicationConfiguration appConfig)
|
|
|
|
|
{
|
|
|
|
|
if (_defaultLogger == null)
|
|
|
|
|
{
|
|
|
|
|
var configuration = new ConfigurationBuilder()
|
|
|
|
|
.AddJsonFile(Path.Join(Utilities.OperatingDirectory, "Configuration", "LoggingConfiguration.json"))
|
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
|
|
var loggerConfig = new LoggerConfiguration()
|
2023-05-26 22:14:49 -04:00
|
|
|
|
.ReadFrom.Configuration(configuration);
|
|
|
|
|
|
|
|
|
|
LevelSwitch.MinimumLevel = Enum.Parse<LogEventLevel>(configuration["Serilog:MinimumLevel:Default"]);
|
|
|
|
|
MicrosoftLevelSwitch.MinimumLevel = Enum.Parse<LogEventLevel>(configuration["Serilog:MinimumLevel:Override:Microsoft"]);
|
|
|
|
|
SystemLevelSwitch.MinimumLevel = Enum.Parse<LogEventLevel>(configuration["Serilog:MinimumLevel:Override:System"]);
|
|
|
|
|
|
|
|
|
|
loggerConfig = loggerConfig.MinimumLevel.ControlledBy(LevelSwitch);
|
|
|
|
|
loggerConfig = loggerConfig.MinimumLevel.Override("Microsoft", MicrosoftLevelSwitch)
|
|
|
|
|
.MinimumLevel.Override("System", SystemLevelSwitch);
|
2020-11-11 18:31:26 -05:00
|
|
|
|
|
|
|
|
|
if (Utilities.IsDevelopment)
|
|
|
|
|
{
|
|
|
|
|
loggerConfig = loggerConfig.WriteTo.Console(
|
2020-11-27 22:52:52 -05:00
|
|
|
|
outputTemplate:
|
2023-05-26 22:14:49 -04:00
|
|
|
|
"[{Timestamp:HH:mm:ss} {Server} {Level:u3}] {Message:lj}{NewLine}{Exception}")
|
2023-06-11 18:30:06 -04:00
|
|
|
|
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
|
|
|
|
|
.MinimumLevel.Debug();
|
2020-11-11 18:31:26 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_defaultLogger = loggerConfig.CreateLogger();
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-26 22:14:49 -04:00
|
|
|
|
services.AddSingleton((string context) =>
|
|
|
|
|
{
|
|
|
|
|
return context.ToLower() switch
|
|
|
|
|
{
|
|
|
|
|
"microsoft" => MicrosoftLevelSwitch,
|
|
|
|
|
"system" => SystemLevelSwitch,
|
|
|
|
|
_ => LevelSwitch
|
|
|
|
|
};
|
|
|
|
|
});
|
2020-11-11 18:31:26 -05:00
|
|
|
|
services.AddLogging(builder => builder.AddSerilog(_defaultLogger, dispose: true));
|
2020-12-02 15:29:49 -05:00
|
|
|
|
services.AddSingleton(new LoggerFactory()
|
|
|
|
|
.AddSerilog(_defaultLogger, true));
|
2020-11-11 18:31:26 -05:00
|
|
|
|
return services;
|
|
|
|
|
}
|
2020-11-27 22:52:52 -05:00
|
|
|
|
|
2020-11-29 17:01:52 -05:00
|
|
|
|
public static IServiceCollection AddDatabaseContextOptions(this IServiceCollection services,
|
2020-11-27 22:52:52 -05:00
|
|
|
|
ApplicationConfiguration appConfig)
|
|
|
|
|
{
|
2020-11-29 17:01:52 -05:00
|
|
|
|
var activeProvider = appConfig.DatabaseProvider?.ToLower();
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(appConfig.ConnectionString) || activeProvider == "sqlite")
|
2020-11-27 22:52:52 -05:00
|
|
|
|
{
|
|
|
|
|
var currentPath = Utilities.OperatingDirectory;
|
|
|
|
|
currentPath = !RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
|
|
|
|
|
? $"{Path.DirectorySeparatorChar}{currentPath}"
|
|
|
|
|
: currentPath;
|
|
|
|
|
|
|
|
|
|
var connectionStringBuilder = new SqliteConnectionStringBuilder
|
|
|
|
|
{DataSource = Path.Join(currentPath, "Database", "Database.db")};
|
|
|
|
|
var connectionString = connectionStringBuilder.ToString();
|
|
|
|
|
|
2021-03-22 12:09:25 -04:00
|
|
|
|
services.AddSingleton(sp => (DbContextOptions) new DbContextOptionsBuilder<SqliteDatabaseContext>()
|
|
|
|
|
.UseSqlite(connectionString)
|
|
|
|
|
.UseLoggerFactory(sp.GetRequiredService<ILoggerFactory>())
|
|
|
|
|
.EnableSensitiveDataLogging().Options);
|
2020-11-27 22:52:52 -05:00
|
|
|
|
return services;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-29 17:01:52 -05:00
|
|
|
|
switch (activeProvider)
|
2020-11-27 22:52:52 -05:00
|
|
|
|
{
|
|
|
|
|
case "mysql":
|
|
|
|
|
var appendTimeout = !appConfig.ConnectionString.Contains("default command timeout",
|
|
|
|
|
StringComparison.InvariantCultureIgnoreCase);
|
2022-01-26 11:32:16 -05:00
|
|
|
|
var connectionString =
|
|
|
|
|
appConfig.ConnectionString + (appendTimeout ? ";default command timeout=0" : "");
|
2020-12-02 15:29:49 -05:00
|
|
|
|
services.AddSingleton(sp => (DbContextOptions) new DbContextOptionsBuilder<MySqlDatabaseContext>()
|
2022-01-26 11:32:16 -05:00
|
|
|
|
.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString),
|
2020-12-02 15:29:49 -05:00
|
|
|
|
mysqlOptions => mysqlOptions.EnableRetryOnFailure())
|
|
|
|
|
.UseLoggerFactory(sp.GetRequiredService<ILoggerFactory>()).Options);
|
2020-11-29 17:01:52 -05:00
|
|
|
|
return services;
|
2020-11-27 22:52:52 -05:00
|
|
|
|
case "postgresql":
|
|
|
|
|
appendTimeout = !appConfig.ConnectionString.Contains("Command Timeout",
|
|
|
|
|
StringComparison.InvariantCultureIgnoreCase);
|
2020-12-02 15:29:49 -05:00
|
|
|
|
services.AddSingleton(sp =>
|
|
|
|
|
(DbContextOptions) new DbContextOptionsBuilder<PostgresqlDatabaseContext>()
|
|
|
|
|
.UseNpgsql(appConfig.ConnectionString + (appendTimeout ? ";Command Timeout=0" : ""),
|
2021-03-22 12:09:25 -04:00
|
|
|
|
postgresqlOptions =>
|
|
|
|
|
{
|
|
|
|
|
postgresqlOptions.EnableRetryOnFailure();
|
2022-02-01 15:27:16 -05:00
|
|
|
|
postgresqlOptions.SetPostgresVersion(new Version("12.9"));
|
2021-03-22 12:09:25 -04:00
|
|
|
|
})
|
2020-12-02 15:29:49 -05:00
|
|
|
|
.UseLoggerFactory(sp.GetRequiredService<ILoggerFactory>()).Options);
|
2020-11-29 17:01:52 -05:00
|
|
|
|
return services;
|
|
|
|
|
default:
|
|
|
|
|
throw new ArgumentException($"No context available for {appConfig.DatabaseProvider}");
|
2020-11-27 22:52:52 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-11-11 18:31:26 -05:00
|
|
|
|
}
|
2022-01-26 11:32:16 -05:00
|
|
|
|
}
|