Improved logging with exceptions and remove config enum parameter

This commit is contained in:
Alex
2025-01-15 11:49:42 +00:00
parent f55f3f45ec
commit 6a2f11d2c4
39 changed files with 90 additions and 58 deletions
+1 -1
View File
@@ -25,7 +25,7 @@ public class BundleCallbacks
_httpResponseUtil = httpResponseUtil;
// _bundleLoader = bundleLoader;
_configServer = configServer;
_httpConfig = configServer.GetConfig<HttpConfig>(ConfigTypes.HTTP);
_httpConfig = configServer.GetConfig<HttpConfig>();
}
/// <summary>
+2 -2
View File
@@ -54,7 +54,7 @@ public class ClientLogCallbacks
/// <returns></returns>
public string ReleaseNotes()
{
var data = _configServer.GetConfig<CoreConfig>(ConfigTypes.CORE).Release;
var data = _configServer.GetConfig<CoreConfig>().Release;
data.BetaDisclaimerText = "BetaDisclaimerText";
data.BetaDisclaimerAcceptText = "BetaDisclaimerAcceptText";
data.ServerModsLoadedText = "ServerModsLoadedText";
@@ -97,7 +97,7 @@ public class ClientLogCallbacks
/// <returns></returns>
public string BsgLogging()
{
var data = _configServer.GetConfig<CoreConfig>(ConfigTypes.CORE).BsgLogging;
var data = _configServer.GetConfig<CoreConfig>().BsgLogging;
return _httpResponseUtil.NoBody(data);
}
}
+1 -1
View File
@@ -25,7 +25,7 @@ public class HideoutCallbacks : OnUpdate
{
_hideoutController = hideoutController;
_configServer = configServer;
_hideoutConfig = configServer.GetConfig<HideoutConfig>(ConfigTypes.HIDEOUT);
_hideoutConfig = configServer.GetConfig<HideoutConfig>();
}
/// <summary>
+1 -1
View File
@@ -36,7 +36,7 @@ public class InsuranceCallbacks : OnUpdate
_insuranceService = insuranceService;
_httpResponseUtil = httpResponseUtil;
_configServer = configServer;
_insuranceConfig = configServer.GetConfig<InsuranceConfig>(ConfigTypes.INSURANCE);
_insuranceConfig = configServer.GetConfig<InsuranceConfig>();
}
/// <summary>
+1 -1
View File
@@ -39,7 +39,7 @@ public class RagfairCallbacks : OnLoad, OnUpdate
_ragfairController = ragfairController;
_ragfairTaxService = ragfairTaxService;
_configServer = configServer;
_ragfairConfig = _configServer.GetConfig<RagfairConfig>(ConfigTypes.RAGFAIR);
_ragfairConfig = _configServer.GetConfig<RagfairConfig>();
}
public async Task OnLoad()
+1 -1
View File
@@ -22,7 +22,7 @@ public class SaveCallbacks : OnLoad, OnUpdate
)
{
_saveServer = saveServer;
_coreConfig = configServer.GetConfig<CoreConfig>(ConfigTypes.CORE);
_coreConfig = configServer.GetConfig<CoreConfig>();
_backupService = backupService;
}
+1 -1
View File
@@ -94,7 +94,7 @@ public class TraderCallbacks : OnLoad, OnUpdate
/// <returns></returns>
public string GetModdedTraderData(string url, EmptyRequestData info, string sessionID)
{
var traderConfig = _configServer.GetConfig<TraderConfig>(ConfigTypes.TRADER);
var traderConfig = _configServer.GetConfig<TraderConfig>();
return _httpResponseUtil.NoBody(traderConfig.ModdedTraders);
}
}
+2 -2
View File
@@ -76,8 +76,8 @@ public class BotController
_applicationContext = applicationContext;
_randomUtil = randomUtil;
_cloner = cloner;
_botConfig = _configServer.GetConfig<BotConfig>(ConfigTypes.BOT);
_pmcConfig = _configServer.GetConfig<PmcConfig>(ConfigTypes.PMC);
_botConfig = _configServer.GetConfig<BotConfig>();
_pmcConfig = _configServer.GetConfig<PmcConfig>();
}
public int GetBotPresetGenerationLimit(string type)
+5 -5
View File
@@ -96,11 +96,11 @@ public class GameController
_applicationContext = applicationContext;
_cloner = cloner;
_coreConfig = configServer.GetConfig<CoreConfig>(ConfigTypes.CORE);
_httpConfig = configServer.GetConfig<HttpConfig>(ConfigTypes.HTTP);
_ragfairConfig = configServer.GetConfig<RagfairConfig>(ConfigTypes.RAGFAIR);
_hideoutConfig = configServer.GetConfig<HideoutConfig>(ConfigTypes.HIDEOUT);
_botConfig = configServer.GetConfig<BotConfig>(ConfigTypes.BOT);
_coreConfig = configServer.GetConfig<CoreConfig>();
_httpConfig = configServer.GetConfig<HttpConfig>();
_ragfairConfig = configServer.GetConfig<RagfairConfig>();
_hideoutConfig = configServer.GetConfig<HideoutConfig>();
_botConfig = configServer.GetConfig<BotConfig>();
}
/// <summary>
+2 -2
View File
@@ -39,8 +39,8 @@ public class InRaidController
_localisationService = localisationService;
_applicationContext = applicationContext;
_configServer = configServer;
_inRaidConfig = configServer.GetConfig<InRaidConfig>(ConfigTypes.IN_RAID);
_botConfig = configServer.GetConfig<BotConfig>(ConfigTypes.BOT);
_inRaidConfig = configServer.GetConfig<InRaidConfig>();
_botConfig = configServer.GetConfig<BotConfig>();
}
/// <summary>
+1 -1
View File
@@ -54,7 +54,7 @@ public class LauncherController
_profileHelper = profileHelper;
_databaseService = databaseService;
_localisationService = localisationService;
_coreConfig = configServer.GetConfig<CoreConfig>(ConfigTypes.CORE);
_coreConfig = configServer.GetConfig<CoreConfig>();
}
public ConnectResponse Connect()
+2 -2
View File
@@ -71,8 +71,8 @@ public class TradeController
_ragfairPriceService = ragfairPriceService;
_configServer = configServer;
_ragfairConfig = _configServer.GetConfig<RagfairConfig>(ConfigTypes.RAGFAIR);
_traderConfig = _configServer.GetConfig<TraderConfig>(ConfigTypes.TRADER);
_ragfairConfig = _configServer.GetConfig<RagfairConfig>();
_traderConfig = _configServer.GetConfig<TraderConfig>();
}
/// <summary>
+1 -1
View File
@@ -67,7 +67,7 @@ public class TraderController
_configServer = configServer;
_cloner = cloner;
_traderConfig = configServer.GetConfig<TraderConfig>(ConfigTypes.TRADER);
_traderConfig = configServer.GetConfig<TraderConfig>();
}
/// <summary>
+1 -1
View File
@@ -39,7 +39,7 @@ public class WeatherController
_weatherHelper = weatherHelper;
_configServer = configServer;
_weatherConfig = _configServer.GetConfig<WeatherConfig>(ConfigTypes.WEATHER);
_weatherConfig = _configServer.GetConfig<WeatherConfig>();
}
/// <summary>
+1 -1
View File
@@ -71,7 +71,7 @@ public class PlayerScavGenerator
_cloner = cloner;
_timeUtil = timeUtil;
_playerScavConfig = configServer.GetConfig<PlayerScavConfig>(ConfigTypes.PLAYERSCAV);
_playerScavConfig = configServer.GetConfig<PlayerScavConfig>();
}
/// <summary>
+1 -1
View File
@@ -30,7 +30,7 @@ public class WeatherGenerator
_weatherHelper = weatherHelper;
_configServer = configServer;
_weatherConfig = _configServer.GetConfig<WeatherConfig>(ConfigTypes.WEATHER);
_weatherConfig = _configServer.GetConfig<WeatherConfig>();
}
/**
+1 -1
View File
@@ -26,7 +26,7 @@ public class HttpServerHelper
public HttpServerHelper(ConfigServer configServer)
{
_httpConfig = configServer.GetConfig<HttpConfig>(ConfigTypes.HTTP);
_httpConfig = configServer.GetConfig<HttpConfig>();
}
public string GetMimeText(string key)
+1 -1
View File
@@ -16,7 +16,7 @@ public class PaymentHelper
{
_configServer = configServer;
_inventoryConfig = _configServer.GetConfig<InventoryConfig>(ConfigTypes.INVENTORY);
_inventoryConfig = _configServer.GetConfig<InventoryConfig>();
}
/// <summary>
+1 -1
View File
@@ -46,7 +46,7 @@ public class ProfileHelper
_timeUtil = timeUtil;
_localisationService = localisationService;
_hashUtil = hashUtil;
_inventoryConfig = configServer.GetConfig<InventoryConfig>(ConfigTypes.INVENTORY);
_inventoryConfig = configServer.GetConfig<InventoryConfig>();
}
/// <summary>
+1 -1
View File
@@ -47,7 +47,7 @@ public class QuestHelper
_localeService = localeService;
_cloner = Cloner;
_questConfig = configServer.GetConfig<QuestConfig>(ConfigTypes.QUEST);
_questConfig = configServer.GetConfig<QuestConfig>();
}
/// <summary>
+1 -1
View File
@@ -59,7 +59,7 @@ public class QuestRewardHelper
_localisationService = localisationService;
_cloner = cloner;
_questConfig = configServer.GetConfig<QuestConfig>(ConfigTypes.QUEST);
_questConfig = configServer.GetConfig<QuestConfig>();
}
/**
+1 -1
View File
@@ -44,7 +44,7 @@ public class TraderHelper
_profileHelper = profileHelper;
_databaseService = databaseService;
_traderConfig = _configServer.GetConfig<TraderConfig>(ConfigTypes.TRADER);
_traderConfig = _configServer.GetConfig<TraderConfig>();
}
/// <summary>
+1 -1
View File
@@ -27,7 +27,7 @@ public class WeatherHelper
_timeUtil = timeUtil;
_configServer = configServer;
_weatherConfig = _configServer.GetConfig<WeatherConfig>(ConfigTypes.WEATHER);
_weatherConfig = _configServer.GetConfig<WeatherConfig>();
}
/// <summary>
+13 -7
View File
@@ -1,7 +1,4 @@
using System.Runtime.InteropServices.JavaScript;
using System.Text.Json;
using System.Text.Json.Serialization;
using Core.Annotations;
using Core.Annotations;
using Core.Models.Enums;
using Core.Models.Spt.Config;
using Core.Utils;
@@ -30,11 +27,20 @@ public class ConfigServer
Initialize();
}
public T GetConfig<T>(ConfigTypes configType) where T : BaseConfig
public T GetConfig<T>() where T : BaseConfig
{
if (!configs.ContainsKey(configType.GetValue())) throw new Exception($"Config: {configType} is undefined. Ensure you have not broken it via editing");
var configKey = GetConfigKey(typeof(T));
if (!configs.ContainsKey(configKey.GetValue())) throw new Exception($"Config: {configKey} is undefined. Ensure you have not broken it via editing");
return configs[configType.GetValue()] as T;
return configs[configKey.GetValue()] as T;
}
private ConfigTypes GetConfigKey(Type type)
{
var configEnumerable = Enum.GetValues<ConfigTypes>().Where(e => e.GetConfigType() == type);
if (!configEnumerable.Any())
throw new Exception($"Config of type {type.Name} is not mapped to any ConfigTypes");
return configEnumerable.First();
}
public T GetConfigByString<T>(string configType) where T : BaseConfig
+1 -1
View File
@@ -39,7 +39,7 @@ public class HttpServer
_webSocketServer = webSocketServer;
_httpListeners = httpListeners;
httpConfig = _configServer.GetConfig<HttpConfig>(ConfigTypes.HTTP);
httpConfig = _configServer.GetConfig<HttpConfig>();
}
public void Load(WebApplicationBuilder builder)
+1 -1
View File
@@ -235,7 +235,7 @@ public class SaveServer
}
var start = Stopwatch.StartNew();
var jsonProfile = _jsonUtil.Serialize(profiles[sessionID], !_configServer.GetConfig<CoreConfig>(ConfigTypes.CORE).Features.CompressProfile);
var jsonProfile = _jsonUtil.Serialize(profiles[sessionID], !_configServer.GetConfig<CoreConfig>().Features.CompressProfile);
var fmd5 = _hashUtil.GenerateMd5ForData(jsonProfile);
if (!saveMd5.TryGetValue(sessionID, out var currentMd5) || currentMd5 != fmd5)
{
+1 -1
View File
@@ -24,7 +24,7 @@ public class GiftService
_configServer = configServer;
_profileHelper = profileHelper;
_giftConfig = _configServer.GetConfig<GiftsConfig>(ConfigTypes.GIFTS);
_giftConfig = _configServer.GetConfig<GiftsConfig>();
}
/**
+1 -1
View File
@@ -20,7 +20,7 @@ public class LocaleService
_logger = logger;
_databaseServer = databaseServer;
_configServer = configServer;
_localeConfig = configServer.GetConfig<LocaleConfig>(ConfigTypes.LOCALE);
_localeConfig = configServer.GetConfig<LocaleConfig>();
}
/**
+1 -1
View File
@@ -53,7 +53,7 @@ public class ProfileFixerService
_configServer = configServer;
_inventoryHelper = inventoryHelper;
_coreConfig = _configServer.GetConfig<CoreConfig>(ConfigTypes.CORE);
_coreConfig = _configServer.GetConfig<CoreConfig>();
}
/// <summary>
+1 -1
View File
@@ -42,7 +42,7 @@ public class RaidWeatherService
_weightedRandomHelper = weightedRandomHelper;
_configServer = configServer;
_weatherConfig = _configServer.GetConfig<WeatherConfig>(ConfigTypes.WEATHER);
_weatherConfig = _configServer.GetConfig<WeatherConfig>();
}
/// <summary>
@@ -36,7 +36,7 @@ public class TraderPurchasePersisterService
_localisationService = localisationService;
_configServer = configServer;
_traderConfig = _configServer.GetConfig<TraderConfig>(ConfigTypes.TRADER);
_traderConfig = _configServer.GetConfig<TraderConfig>();
}
/**
+1 -1
View File
@@ -47,7 +47,7 @@ public class App
_onLoad = onLoadComponents;
_onUpdate = onUpdateComponents;
_coreConfig = configServer.GetConfig<CoreConfig>(ConfigTypes.CORE);
_coreConfig = configServer.GetConfig<CoreConfig>();
}
public async Task Run()
+1 -1
View File
@@ -54,7 +54,7 @@ public class DatabaseImporter : OnLoad
_configServer = configServer;
_fileUtil = fileUtil;
_imageRouter = imageRouter;
httpConfig = _configServer.GetConfig<HttpConfig>(ConfigTypes.HTTP);
httpConfig = _configServer.GetConfig<HttpConfig>();
}
/**
+14 -3
View File
@@ -3,6 +3,7 @@ using System.Text.Json;
using System.Text.Json.Serialization;
using Core.Annotations;
using Core.Utils.Json.Converters;
using ILogger = Core.Models.Utils.ILogger;
namespace Core.Utils;
@@ -11,11 +12,13 @@ public class ImporterUtil
{
private readonly FileUtil _fileUtil;
private readonly JsonUtil _jsonUtil;
private readonly Models.Utils.ILogger _logger;
private readonly HashSet<string> filesToIgnore = ["bearsuits.json", "usecsuits.json", "archivedquests.json"];
public ImporterUtil(FileUtil fileUtil, JsonUtil jsonUtil)
public ImporterUtil(ILogger logger, FileUtil fileUtil, JsonUtil jsonUtil)
{
_logger = logger;
_fileUtil = fileUtil;
_jsonUtil = jsonUtil;
}
@@ -96,8 +99,16 @@ public class ImporterUtil
return Task.WhenAll(tasks).ContinueWith((t) =>
{
if (t.IsCanceled || t.IsFaulted)
tasks.Where(t => t.IsFaulted || t.IsCanceled).ToList().ForEach(t => Console.WriteLine(t.Exception));
}).ContinueWith(_ => result);
{
var exceptionList = tasks.Where(t => t.IsFaulted || t.IsCanceled).Select(t => t.Exception!).ToList();
throw new Exception("Error processing one or more DatabaseFiles", new AggregateException(exceptionList));
}
}).ContinueWith(t =>
{
if (t.IsFaulted || t.IsCanceled)
throw t.Exception!;
return result;
});
}
public MethodInfo GetSetMethod(string propertyName, Type type, out Type propertyType, out bool isDictionary)
+1 -1
View File
@@ -65,7 +65,7 @@ public class Watermark {
_configServer = configServer;
_localisationService = localisationService;
_watermarkLocale = watermarkLocale;
sptConfig = _configServer.GetConfig<CoreConfig>(ConfigTypes.CORE);
sptConfig = _configServer.GetConfig<CoreConfig>();
}
public virtual void Initialize()
+1 -1
View File
@@ -6,7 +6,7 @@ public class FileFormatter : AbstractFormatter
{
protected override string ProcessText(string message)
{
foreach (Match match in Regex.Matches(message, @"\x1b\[((\d.+?)?\d)m"))
foreach (Match match in Regex.Matches(message, @"\x1b\[[0-9]{1,2}(;[0-1]{1,2}){0,1}m"))
{
message = message.Replace(match.Value, "");
}
+14 -6
View File
@@ -1,5 +1,4 @@
using System.Reflection;
using System.Security.Cryptography;
using Core.Annotations;
using Core.Context;
using Core.Models.Enums;
@@ -7,8 +6,12 @@ using Core.Models.External;
using Core.Models.Spt.Config;
using Core.Servers;
using Core.Utils;
using Microsoft.Extensions.Logging.Configuration;
using Serilog;
using Serilog.Events;
using Serilog.Exceptions;
using Serilog.Extensions.Logging;
using ILogger = Microsoft.Extensions.Logging.ILogger;
namespace Server;
@@ -22,10 +25,11 @@ public static class Program
builder.Configuration.AddJsonFile("appsettings.json", true, true);
CreateAndRegisterLogger(builder);
CreateAndRegisterLogger(builder, out var registeredLogger);
RegisterSptComponents(builder.Services);
RegisterModOverrideComponents(builder.Services, assemblies);
ILogger logger = new SerilogLoggerProvider(registeredLogger).CreateLogger("Server");
try
{
var serviceProvider = builder.Services.BuildServiceProvider();
@@ -49,23 +53,27 @@ public static class Program
var app = serviceProvider.GetService<App>();
app.Run().Wait();
var httpConfig = serviceProvider.GetService<ConfigServer>().GetConfig<HttpConfig>(ConfigTypes.HTTP);
var httpConfig = serviceProvider.GetService<ConfigServer>().GetConfig<HttpConfig>();
// When we application gets started by the HttpServer it will add into the AppContext the WebApplication
// object, which we can use here to start the webapp.
(appContext.GetLatestValue(ContextVariableType.WEB_APPLICATION).Value as WebApplication).Run($"http://{httpConfig.Ip}:{httpConfig.Port}");
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
logger.LogCritical(ex, "Critical exception, stopping server...");
// throw ex;
}
}
private static void CreateAndRegisterLogger(WebApplicationBuilder builder)
private static void CreateAndRegisterLogger(WebApplicationBuilder builder, out Serilog.Core.Logger logger)
{
builder.Logging.ClearProviders();
var logger = new LoggerConfiguration()
logger = new LoggerConfiguration()
.ReadFrom.Configuration(builder.Configuration)
.MinimumLevel.Override("Microsoft.AspNetCore.Hosting.Diagnostics", LogEventLevel.Warning)
.Enrich.FromLogContext()
.Enrich.WithThreadId()
.Enrich.WithExceptionDetails()
.CreateLogger();
builder.Logging.AddSerilog(logger);
}
+3
View File
@@ -19,6 +19,9 @@
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.0"/>
<PackageReference Include="Microsoft.Extensions.Http" Version="9.0.0"/>
<PackageReference Include="Serilog.AspNetCore" Version="9.0.0" />
<PackageReference Include="Serilog.Enrichers.Context" Version="4.6.5" />
<PackageReference Include="Serilog.Enrichers.Thread" Version="4.0.0" />
<PackageReference Include="Serilog.Exceptions" Version="8.4.0" />
<PackageReference Include="Serilog.Expressions" Version="5.0.0" />
<PackageReference Include="Serilog.Sinks.Async" Version="2.1.0" />
</ItemGroup>
+4
View File
@@ -40,6 +40,10 @@
"expression": "StartsWith(SourceContext, 'TADAADA')"
}
}
],
"Enrich": [
"FromLogContext",
"WithExceptionDetails"
]
}
}