Improved logging system.
This commit is contained in:
parent
d94b1cb813
commit
98d13ebbbb
24 changed files with 202 additions and 142 deletions
SharpChat
|
@ -1,9 +1,12 @@
|
|||
using Microsoft.Extensions.Logging;
|
||||
using SharpChat;
|
||||
using SharpChat.Configuration;
|
||||
using SharpChat.Flashii;
|
||||
using SharpChat.MariaDB;
|
||||
using SharpChat.SQLite;
|
||||
using System.Text;
|
||||
using ZLogger;
|
||||
using ZLogger.Providers;
|
||||
|
||||
const string CONFIG = "sharpchat.cfg";
|
||||
|
||||
|
@ -21,6 +24,34 @@ if(SharpInfo.IsDebugBuild) {
|
|||
} else
|
||||
Console.WriteLine(SharpInfo.VersionStringShort.PadLeft(28, ' '));
|
||||
|
||||
using ILoggerFactory logFactory = LoggerFactory.Create(logging => {
|
||||
logging.ClearProviders();
|
||||
|
||||
#if DEBUG
|
||||
logging.SetMinimumLevel(LogLevel.Trace);
|
||||
#else
|
||||
logging.SetMinimumLevel(LogLevel.Information);
|
||||
#endif
|
||||
|
||||
logging.AddZLoggerConsole(opts => {
|
||||
opts.OutputEncodingToUtf8 = true;
|
||||
opts.UsePlainTextFormatter(formatter => {
|
||||
formatter.SetPrefixFormatter($"{0} [{1} {2}] ", (in MessageTemplate template, in LogInfo info) => template.Format(info.Timestamp, info.Category, info.LogLevel));
|
||||
});
|
||||
});
|
||||
logging.AddZLoggerRollingFile(opts => {
|
||||
opts.FilePathSelector = (ts, seqNo) => $"logs/{ts.ToLocalTime():yyyy-MM-dd_HH-mm-ss}_{seqNo:000}.json";
|
||||
opts.RollingInterval = RollingInterval.Day;
|
||||
opts.RollingSizeKB = 1024;
|
||||
opts.FileShared = true;
|
||||
opts.UseJsonFormatter(formatter => {
|
||||
formatter.UseUtcTimestamp = true;
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
ILogger logger = logFactory.CreateLogger("main");
|
||||
|
||||
using CancellationTokenSource cts = new();
|
||||
|
||||
void exitHandler() {
|
||||
|
@ -28,7 +59,7 @@ void exitHandler() {
|
|||
return;
|
||||
|
||||
cts.Cancel();
|
||||
Logger.Write("Shutdown requested through console...");
|
||||
logger.ZLogInformation($"Shutdown requested through console...");
|
||||
}
|
||||
|
||||
AppDomain.CurrentDomain.ProcessExit += (sender, ev) => { exitHandler(); };
|
||||
|
@ -40,7 +71,7 @@ string configFile = CONFIG;
|
|||
|
||||
// If the config file doesn't exist and we're using the default path, run the converter
|
||||
if(!File.Exists(configFile) && configFile == CONFIG) {
|
||||
Logger.Write("Creating example configuration file...");
|
||||
logger.ZLogInformation($"Creating example configuration file...");
|
||||
|
||||
using Stream s = new FileStream(CONFIG, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
|
||||
s.SetLength(0);
|
||||
|
@ -112,12 +143,12 @@ if(!File.Exists(configFile) && configFile == CONFIG) {
|
|||
sw.Flush();
|
||||
}
|
||||
|
||||
Logger.Write("Initialising configuration...");
|
||||
logger.ZLogInformation($"Initialising configuration...");
|
||||
using StreamConfig config = StreamConfig.FromPath(configFile);
|
||||
|
||||
if(cts.IsCancellationRequested) return;
|
||||
|
||||
Logger.Write("Initialising HTTP client...");
|
||||
logger.ZLogInformation($"Initialising HTTP client...");
|
||||
using HttpClient httpClient = new(new HttpClientHandler() {
|
||||
UseProxy = false,
|
||||
});
|
||||
|
@ -125,31 +156,37 @@ httpClient.DefaultRequestHeaders.Add("User-Agent", SharpInfo.ProgramName);
|
|||
|
||||
if(cts.IsCancellationRequested) return;
|
||||
|
||||
Logger.Write("Initialising Flashii client...");
|
||||
FlashiiClient flashii = new(httpClient, config.ScopeTo("msz"));
|
||||
logger.ZLogInformation($"Initialising Flashii client...");
|
||||
FlashiiClient flashii = new(logFactory.CreateLogger("flashii"), httpClient, config.ScopeTo("msz"));
|
||||
|
||||
if(cts.IsCancellationRequested) return;
|
||||
|
||||
Logger.Write("Initialising storage...");
|
||||
logger.ZLogInformation($"Initialising storage...");
|
||||
Storage storage = string.IsNullOrWhiteSpace(config.SafeReadValue("mariadb:host", string.Empty))
|
||||
? new SQLiteStorage(SQLiteStorage.BuildConnectionString(config.ScopeTo("sqlite")))
|
||||
: new MariaDBStorage(MariaDBStorage.BuildConnectionString(config.ScopeTo("mariadb")));
|
||||
? new SQLiteStorage(logFactory.CreateLogger("sqlite"), SQLiteStorage.BuildConnectionString(config.ScopeTo("sqlite")))
|
||||
: new MariaDBStorage(logFactory.CreateLogger("mariadb"), MariaDBStorage.BuildConnectionString(config.ScopeTo("mariadb")));
|
||||
|
||||
try {
|
||||
if(cts.IsCancellationRequested) return;
|
||||
|
||||
Logger.Write("Upgrading storage...");
|
||||
await storage.UpgradeStorage();
|
||||
|
||||
if(cts.IsCancellationRequested) return;
|
||||
|
||||
Logger.Write("Preparing server...");
|
||||
await new SockChatServer(cts, flashii, flashii, storage.CreateMessageStorage(), config.ScopeTo("chat")).Listen(cts.Token);
|
||||
logger.ZLogInformation($"Preparing server...");
|
||||
await new SockChatServer(
|
||||
logFactory,
|
||||
cts,
|
||||
flashii,
|
||||
flashii,
|
||||
storage.CreateMessageStorage(),
|
||||
config.ScopeTo("chat")
|
||||
).Listen(cts.Token);
|
||||
} finally {
|
||||
if(storage is IDisposable disp) {
|
||||
Logger.Write("Cleaning storage...");
|
||||
logger.ZLogInformation($"Cleaning storage...");
|
||||
disp.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
Logger.Write("Exiting...");
|
||||
logger.ZLogInformation($"Exiting...");
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue