This is just Jetbrains formatting and code syntax styling
This commit is contained in:
@@ -5,13 +5,6 @@ namespace Server.Logger;
|
||||
|
||||
public abstract class AbstractFormatter : ITextFormatter
|
||||
{
|
||||
protected abstract string ProcessText(string text);
|
||||
|
||||
protected virtual string GetFormattedText(string timestamp, string logLevel, string sourceContext, string message)
|
||||
{
|
||||
return $"[{timestamp} {logLevel}][{sourceContext}] {message}";
|
||||
}
|
||||
|
||||
public void Format(LogEvent logEvent, TextWriter output)
|
||||
{
|
||||
var newLine = Environment.NewLine;
|
||||
@@ -23,4 +16,11 @@ public abstract class AbstractFormatter : ITextFormatter
|
||||
var logMessage = ProcessText(GetFormattedText(timestamp, logLevel, sourceContext, $"{message}{exception}"));
|
||||
output.WriteLine(logMessage);
|
||||
}
|
||||
|
||||
protected abstract string ProcessText(string text);
|
||||
|
||||
protected virtual string GetFormattedText(string timestamp, string logLevel, string sourceContext, string message)
|
||||
{
|
||||
return $"[{timestamp} {logLevel}][{sourceContext}] {message}";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@ namespace Server.Logger;
|
||||
|
||||
public class ConsoleFormatter : AbstractFormatter
|
||||
{
|
||||
public static ConsoleFormatter Default { get; } = new();
|
||||
|
||||
protected override string ProcessText(string text)
|
||||
{
|
||||
return text;
|
||||
@@ -11,6 +13,4 @@ public class ConsoleFormatter : AbstractFormatter
|
||||
{
|
||||
return message;
|
||||
}
|
||||
|
||||
public static ConsoleFormatter Default { get; } = new();
|
||||
}
|
||||
|
||||
@@ -4,14 +4,11 @@ namespace Server.Logger;
|
||||
|
||||
public class FileFormatter : AbstractFormatter
|
||||
{
|
||||
public static FileFormatter Default { get; } = new();
|
||||
|
||||
protected override string ProcessText(string message)
|
||||
{
|
||||
foreach (Match match in Regex.Matches(message, @"\x1b\[[0-9]{1,2}(;[0-1]{1,2}){0,1}m"))
|
||||
{
|
||||
message = message.Replace(match.Value, "");
|
||||
}
|
||||
foreach (Match match in Regex.Matches(message, @"\x1b\[[0-9]{1,2}(;[0-1]{1,2}){0,1}m")) message = message.Replace(match.Value, "");
|
||||
return message.Replace("\"", "");
|
||||
}
|
||||
|
||||
public static FileFormatter Default { get; } = new FileFormatter();
|
||||
}
|
||||
|
||||
@@ -24,31 +24,11 @@ public class SptWebApplicationLogger<T> : ISptLogger<T>
|
||||
)
|
||||
{
|
||||
if (textColor != null || backgroundColor != null)
|
||||
{
|
||||
_logger.LogInformation(ex, GetColorizedText(data, textColor, backgroundColor));
|
||||
}
|
||||
else
|
||||
_logger.LogInformation(ex, data);
|
||||
}
|
||||
|
||||
private string GetColorizedText(
|
||||
string data,
|
||||
LogTextColor? textColor = null,
|
||||
LogBackgroundColor? backgroundColor = null
|
||||
)
|
||||
{
|
||||
var colorString = string.Empty;
|
||||
if (textColor != null)
|
||||
colorString += ((int)textColor.Value).ToString();
|
||||
|
||||
if (backgroundColor != null)
|
||||
colorString += string.IsNullOrEmpty(colorString)
|
||||
? ((int)backgroundColor.Value).ToString()
|
||||
: $";{((int)backgroundColor.Value).ToString()}";
|
||||
|
||||
return $"\x1b[{colorString}m{data}\x1b[0m";
|
||||
}
|
||||
|
||||
public void Success(string data, Exception? ex = null)
|
||||
{
|
||||
_logger.LogInformation(ex, GetColorizedText(data, LogTextColor.Green));
|
||||
@@ -90,6 +70,24 @@ public class SptWebApplicationLogger<T> : ISptLogger<T>
|
||||
return _logger.IsEnabled(ConvertLogLevel(level));
|
||||
}
|
||||
|
||||
private string GetColorizedText(
|
||||
string data,
|
||||
LogTextColor? textColor = null,
|
||||
LogBackgroundColor? backgroundColor = null
|
||||
)
|
||||
{
|
||||
var colorString = string.Empty;
|
||||
if (textColor != null)
|
||||
colorString += ((int)textColor.Value).ToString();
|
||||
|
||||
if (backgroundColor != null)
|
||||
colorString += string.IsNullOrEmpty(colorString)
|
||||
? ((int)backgroundColor.Value).ToString()
|
||||
: $";{((int)backgroundColor.Value).ToString()}";
|
||||
|
||||
return $"\x1b[{colorString}m{data}\x1b[0m";
|
||||
}
|
||||
|
||||
protected Microsoft.Extensions.Logging.LogLevel ConvertLogLevel(LogLevel level)
|
||||
{
|
||||
return level switch
|
||||
|
||||
@@ -5,13 +5,13 @@ namespace Server;
|
||||
public class ModDllLoader
|
||||
{
|
||||
private const string ModPath = "./user/mods/";
|
||||
|
||||
public static List<Assembly> LoadAllMods()
|
||||
{
|
||||
if (!Directory.Exists(ModPath))
|
||||
Directory.CreateDirectory(ModPath);
|
||||
var mods = new List<Assembly>();
|
||||
foreach (var file in Directory.GetFiles(ModPath, "*.dll", SearchOption.AllDirectories))
|
||||
{
|
||||
try
|
||||
{
|
||||
mods.Add(Assembly.LoadFile(Path.GetFullPath(file)));
|
||||
@@ -20,7 +20,7 @@ public class ModDllLoader
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
}
|
||||
}
|
||||
|
||||
return mods;
|
||||
}
|
||||
}
|
||||
|
||||
+7
-10
@@ -23,14 +23,14 @@ public static class Program
|
||||
builder.Host.UseSerilog();
|
||||
|
||||
builder.Configuration.AddJsonFile("appsettings.json", true, true);
|
||||
|
||||
|
||||
CreateAndRegisterLogger(builder, out var registeredLogger);
|
||||
|
||||
ProgramStatics.Initialize();
|
||||
|
||||
DependencyInjectionRegistrator.RegisterSptComponents(typeof(Program).Assembly, typeof(App).Assembly, builder.Services);
|
||||
DependencyInjectionRegistrator.RegisterModOverrideComponents(builder.Services, assemblies);
|
||||
ILogger logger = new SerilogLoggerProvider(registeredLogger).CreateLogger("Server");
|
||||
var logger = new SerilogLoggerProvider(registeredLogger).CreateLogger("Server");
|
||||
try
|
||||
{
|
||||
var serviceProvider = builder.Services.BuildServiceProvider();
|
||||
@@ -40,16 +40,13 @@ public static class Program
|
||||
|
||||
// Initialize PreSptMods
|
||||
var preSptLoadMods = serviceProvider.GetServices<IPreSptLoadMod>();
|
||||
foreach (var preSptLoadMod in preSptLoadMods)
|
||||
{
|
||||
preSptLoadMod.PreSptLoad();
|
||||
}
|
||||
foreach (var preSptLoadMod in preSptLoadMods) preSptLoadMod.PreSptLoad();
|
||||
var appContext = serviceProvider.GetService<ApplicationContext>();
|
||||
// Add the Loaded Mod Assemblies for later
|
||||
appContext?.AddValue(ContextVariableType.LOADED_MOD_ASSEMBLIES, assemblies);
|
||||
// This is the builder that will get use by the HttpServer to start up the web application
|
||||
appContext?.AddValue(ContextVariableType.APP_BUILDER, builder);
|
||||
|
||||
|
||||
// Get the Built app and run it
|
||||
var app = serviceProvider.GetService<App>();
|
||||
app?.Run().Wait();
|
||||
@@ -68,15 +65,15 @@ public static class Program
|
||||
// throw ex;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void CreateAndRegisterLogger(WebApplicationBuilder builder, out Serilog.Core.Logger logger)
|
||||
{
|
||||
builder.Logging.ClearProviders();
|
||||
logger = new LoggerConfiguration()
|
||||
.ReadFrom.Configuration(builder.Configuration)
|
||||
# if DEBUG
|
||||
# if DEBUG
|
||||
.MinimumLevel.Debug()
|
||||
# endif
|
||||
# endif
|
||||
.MinimumLevel.Override("Microsoft.AspNetCore.Hosting.Diagnostics", LogEventLevel.Warning)
|
||||
.Enrich.FromLogContext()
|
||||
.Enrich.WithThreadId()
|
||||
|
||||
Reference in New Issue
Block a user