2022-03-29 05:03:01 +00:00
|
|
|
|
using Discord;
|
|
|
|
|
using Discord.Webhook;
|
2022-05-20 00:03:03 +00:00
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Text;
|
2022-03-29 05:03:01 +00:00
|
|
|
|
|
|
|
|
|
namespace RegexBot.Services.Logging;
|
|
|
|
|
/// <summary>
|
2022-05-20 00:03:03 +00:00
|
|
|
|
/// Implements program-wide logging.
|
2022-03-29 05:03:01 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
class LoggingService : Service {
|
|
|
|
|
// NOTE: Service.Log's functionality is implemented here. DO NOT use within this class.
|
|
|
|
|
private readonly DiscordWebhookClient _instLogWebhook;
|
2022-05-20 00:03:03 +00:00
|
|
|
|
private readonly string? _logBasePath;
|
2022-03-29 05:03:01 +00:00
|
|
|
|
|
|
|
|
|
internal LoggingService(RegexbotClient bot) : base(bot) {
|
|
|
|
|
_instLogWebhook = new DiscordWebhookClient(bot.Config.InstanceLogTarget);
|
2022-05-20 00:03:03 +00:00
|
|
|
|
_logBasePath = Path.GetDirectoryName(Assembly.GetEntryAssembly()!.Location)
|
|
|
|
|
+ Path.DirectorySeparatorChar + "logs";
|
|
|
|
|
try {
|
|
|
|
|
if (!Directory.Exists(_logBasePath)) Directory.CreateDirectory(_logBasePath);
|
|
|
|
|
Directory.GetFiles(_logBasePath);
|
|
|
|
|
} catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) {
|
|
|
|
|
_logBasePath = null;
|
|
|
|
|
Output(Name, "Cannot create or access logging directory. File logging will be disabled.");
|
|
|
|
|
}
|
2022-03-29 05:03:01 +00:00
|
|
|
|
|
|
|
|
|
bot.DiscordClient.Log += DiscordClient_Log;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Discord.Net logging events handled here.
|
|
|
|
|
/// Only events with high importance are stored. Others are just printed to console.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private Task DiscordClient_Log(LogMessage arg) {
|
2022-05-20 00:03:03 +00:00
|
|
|
|
var msg = $"[{Enum.GetName(typeof(LogSeverity), arg.Severity)}] {arg.Message}";
|
2022-03-29 05:03:01 +00:00
|
|
|
|
if (arg.Exception != null) msg += "\n```\n" + arg.Exception.ToString() + "\n```";
|
|
|
|
|
|
2022-05-20 00:03:03 +00:00
|
|
|
|
var important = arg.Severity != LogSeverity.Info;
|
|
|
|
|
switch (arg.Message) { // Prevent webhook logs for these 'important' Discord.Net messages
|
|
|
|
|
case "Connecting":
|
|
|
|
|
case "Connected":
|
|
|
|
|
case "Ready":
|
|
|
|
|
case "Disconnecting":
|
|
|
|
|
case "Disconnected":
|
|
|
|
|
case "Resumed previous session":
|
|
|
|
|
case "Failed to resume previous session":
|
|
|
|
|
important = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
DoLog(important, "Discord.Net", msg);
|
2022-03-29 05:03:01 +00:00
|
|
|
|
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-20 00:03:03 +00:00
|
|
|
|
private void Output(string source, string message) {
|
|
|
|
|
var now = DateTimeOffset.UtcNow;
|
|
|
|
|
var output = new StringBuilder();
|
|
|
|
|
var prefix = $"[{now:u}] [{source}] ";
|
2022-03-29 05:03:01 +00:00
|
|
|
|
foreach (var line in message.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None)) {
|
2022-05-20 00:03:03 +00:00
|
|
|
|
output.Append(prefix).AppendLine(line);
|
|
|
|
|
}
|
|
|
|
|
var outstr = output.ToString();
|
|
|
|
|
Console.Write(outstr);
|
|
|
|
|
if (_logBasePath != null) {
|
|
|
|
|
var filename = _logBasePath + Path.DirectorySeparatorChar + $"{now:yyyy-MM}.log";
|
|
|
|
|
File.AppendAllText(filename, outstr, Encoding.UTF8);
|
2022-03-29 05:03:01 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Hooked
|
2022-05-20 00:03:03 +00:00
|
|
|
|
internal void DoLog(bool report, string source, string? message) {
|
2022-03-29 05:03:01 +00:00
|
|
|
|
message ??= "(null)";
|
2022-05-20 00:03:03 +00:00
|
|
|
|
Output(source, message);
|
2022-03-29 05:03:01 +00:00
|
|
|
|
if (report) Task.Run(() => ReportInstanceWebhook(source, message));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task ReportInstanceWebhook(string source, string message) {
|
|
|
|
|
try {
|
|
|
|
|
EmbedBuilder e = new() {
|
|
|
|
|
Footer = new EmbedFooterBuilder() { Text = source },
|
|
|
|
|
Timestamp = DateTimeOffset.UtcNow,
|
|
|
|
|
Description = message
|
|
|
|
|
};
|
|
|
|
|
await _instLogWebhook.SendMessageAsync(embeds: new[] { e.Build() });
|
|
|
|
|
} catch (Exception ex) {
|
2022-05-20 00:03:03 +00:00
|
|
|
|
DoLog(false, Name, "Failed to send message to reporting channel: " + ex.Message);
|
2022-03-29 05:03:01 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|