Switch logging to local time
This commit is contained in:
parent
ffded1b45c
commit
2d4ae0b4a8
1 changed files with 6 additions and 9 deletions
|
@ -8,7 +8,7 @@ namespace RegexBot.Services.Logging;
|
||||||
/// </summary>
|
/// </summary>
|
||||||
class LoggingService : Service {
|
class LoggingService : Service {
|
||||||
// NOTE: Service.Log's functionality is implemented here. DO NOT use within this class.
|
// NOTE: Service.Log's functionality is implemented here. DO NOT use within this class.
|
||||||
private readonly string? _logBasePath;
|
private readonly string _logBasePath;
|
||||||
|
|
||||||
internal LoggingService(RegexbotClient bot) : base(bot) {
|
internal LoggingService(RegexbotClient bot) : base(bot) {
|
||||||
_logBasePath = Path.GetDirectoryName(Assembly.GetEntryAssembly()!.Location)
|
_logBasePath = Path.GetDirectoryName(Assembly.GetEntryAssembly()!.Location)
|
||||||
|
@ -17,8 +17,7 @@ class LoggingService : Service {
|
||||||
if (!Directory.Exists(_logBasePath)) Directory.CreateDirectory(_logBasePath);
|
if (!Directory.Exists(_logBasePath)) Directory.CreateDirectory(_logBasePath);
|
||||||
Directory.GetFiles(_logBasePath);
|
Directory.GetFiles(_logBasePath);
|
||||||
} catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) {
|
} catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) {
|
||||||
_logBasePath = null;
|
throw new Exception("Cannot create or access logging directory.");
|
||||||
DoLog(Name, "Cannot create or access logging directory. File logging will be disabled.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bot.DiscordClient.Log += DiscordClient_Log;
|
bot.DiscordClient.Log += DiscordClient_Log;
|
||||||
|
@ -50,17 +49,15 @@ class LoggingService : Service {
|
||||||
// Hooked
|
// Hooked
|
||||||
internal void DoLog(string source, string? message) {
|
internal void DoLog(string source, string? message) {
|
||||||
message ??= "(null)";
|
message ??= "(null)";
|
||||||
var now = DateTimeOffset.UtcNow;
|
var now = DateTimeOffset.Now;
|
||||||
var output = new StringBuilder();
|
var output = new StringBuilder();
|
||||||
var prefix = $"[{now:u}] [{source}] ";
|
var prefix = $"[{now:s}] [{source}] ";
|
||||||
foreach (var line in message.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None)) {
|
foreach (var line in message.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None)) {
|
||||||
output.Append(prefix).AppendLine(line);
|
output.Append(prefix).AppendLine(line);
|
||||||
}
|
}
|
||||||
var outstr = output.ToString();
|
var outstr = output.ToString();
|
||||||
Console.Write(outstr);
|
Console.Write(outstr);
|
||||||
if (_logBasePath != null) {
|
var filename = _logBasePath + Path.DirectorySeparatorChar + $"{now:yyyy-MM}.log";
|
||||||
var filename = _logBasePath + Path.DirectorySeparatorChar + $"{now:yyyy-MM}.log";
|
File.AppendAllText(filename, outstr, Encoding.UTF8);
|
||||||
File.AppendAllText(filename, outstr, Encoding.UTF8);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue