From 82b2b4c815d9857e03d82e145a43e056d7a9edef Mon Sep 17 00:00:00 2001 From: runebaas Date: Fri, 29 Dec 2017 01:00:31 +0100 Subject: [PATCH] Groundwork for sumologic --- Geekbot.net/Handlers.cs | 8 +- Geekbot.net/Lib/ErrorHandler.cs | 53 +----------- Geekbot.net/Lib/GeekbotLogger.cs | 69 ++++++++++++++++ Geekbot.net/Lib/SimpleConextConverter.cs | 100 +++++++++++++++++++++++ Geekbot.net/Program.cs | 10 ++- 5 files changed, 182 insertions(+), 58 deletions(-) create mode 100644 Geekbot.net/Lib/GeekbotLogger.cs create mode 100644 Geekbot.net/Lib/SimpleConextConverter.cs diff --git a/Geekbot.net/Handlers.cs b/Geekbot.net/Handlers.cs index 89fb61e..240be2b 100644 --- a/Geekbot.net/Handlers.cs +++ b/Geekbot.net/Handlers.cs @@ -15,15 +15,17 @@ namespace Geekbot.net { private readonly IDiscordClient _client; private readonly ILogger _logger; + private readonly IGeekbotLogger _gLogger; private readonly IDatabase _redis; private readonly IServiceProvider _servicesProvider; private readonly CommandService _commands; private readonly IUserRepository _userRepository; - public Handlers(IDiscordClient client, ILogger logger, IDatabase redis, IServiceProvider servicesProvider, CommandService commands, IUserRepository userRepository) + public Handlers(IDiscordClient client, ILogger logger, IDatabase redis, IServiceProvider servicesProvider, CommandService commands, IUserRepository userRepository, IGeekbotLogger gLogger) { _client = client; _logger = logger; + _gLogger = gLogger; _redis = redis; _servicesProvider = servicesProvider; _commands = commands; @@ -82,8 +84,8 @@ namespace Geekbot.net _redis.HashIncrementAsync($"{channel.Guild.Id}:Messages", 0.ToString()); if (message.Author.IsBot) return Task.CompletedTask; - _logger.Information( - $"[Message] {channel.Guild.Name} ({channel.Guild.Id}) - {message.Channel} ({message.Channel.Id}) - {message.Author.Username}#{message.Author.Discriminator} ({message.Author.Id}) - {message.Content}"); + _gLogger.Info("Message", message.Content, SimpleConextConverter.ConvertSocketMessage(message)); +// _logger.Information($"[Message] {channel.Guild.Name} ({channel.Guild.Id}) - {message.Channel} ({message.Channel.Id}) - {message.Author.Username}#{message.Author.Discriminator} ({message.Author.Id}) - {message.Content}"); } catch (Exception e) { diff --git a/Geekbot.net/Lib/ErrorHandler.cs b/Geekbot.net/Lib/ErrorHandler.cs index 1d6ffd8..20b326c 100644 --- a/Geekbot.net/Lib/ErrorHandler.cs +++ b/Geekbot.net/Lib/ErrorHandler.cs @@ -41,34 +41,7 @@ namespace Geekbot.net.Lib try { var errorString = errorMessage == "def" ? _translation.GetString(Context.Guild.Id, "errorHandler", "SomethingWentWrong") : errorMessage; - var errorObj = new ErrorObject() - { - Message = new ErrorMessage() - { - Content = Context.Message.Content, - Id = Context.Message.Id.ToString(), - Attachments = Context.Message.Attachments.Count, - ChannelMentions = Context.Message.MentionedChannelIds.Count, - UserMentions = Context.Message.MentionedUserIds.Count, - RoleMentions = Context.Message.MentionedRoleIds.Count - }, - User = new IdAndName() - { - Id = Context.User.Id.ToString(), - Name = $"{Context.User.Username}#{Context.User.Discriminator}" - }, - Guild = new IdAndName() - { - Id = Context.Guild.Id.ToString(), - Name = Context.Guild.Name - }, - Channel = new IdAndName() - { - Id = Context.Channel.Id.ToString(), - Name = Context.Channel.Name - }, - TimeStamp = DateTime.Now.ToString() - }; + var errorObj = SimpleConextConverter.ConvertContext(Context); var errorJson = JsonSerializer.ToJsonString(errorObj); _logger.Error(e, errorJson); if (!string.IsNullOrEmpty(errorMessage)) @@ -107,31 +80,7 @@ namespace Geekbot.net.Lib } } - - public class ErrorObject - { - public ErrorMessage Message { get; set; } - public IdAndName User { get; set; } - public IdAndName Guild { get; set; } - public IdAndName Channel { get; set; } - public string TimeStamp { get; set; } - } - public class ErrorMessage - { - public string Content { get; set; } - public string Id { get; set; } - public int Attachments { get; set; } - public int ChannelMentions { get; set; } - public int UserMentions { get; set; } - public int RoleMentions { get; set; } - } - - public class IdAndName - { - public string Id { get; set; } - public string Name { get; set; } - } } public interface IErrorHandler diff --git a/Geekbot.net/Lib/GeekbotLogger.cs b/Geekbot.net/Lib/GeekbotLogger.cs new file mode 100644 index 0000000..f4e191c --- /dev/null +++ b/Geekbot.net/Lib/GeekbotLogger.cs @@ -0,0 +1,69 @@ +using System; +using System.Threading.Tasks; +using Utf8Json; +using Utf8Json.Resolvers; + +namespace Geekbot.net.Lib +{ + public class GeekbotLogger : IGeekbotLogger + { + public GeekbotLogger() + { + JsonSerializer.SetDefaultResolver(StandardResolver.AllowPrivateExcludeNullSnakeCase); + Info("Geekbot", "Using GeekbotLogger"); + } + + public void Debug(string source, string message, object extra = null) + { + HandleLogObject("Debug", source, message, null, extra); + } + + public void Info(string source, string message, object extra = null) + { + HandleLogObject("Information", source, message, null, extra); + } + + public void Error(string source, string message, Exception stackTrace, object extra = null) + { + HandleLogObject("Debug", source, message, stackTrace, extra); + } + + private Task HandleLogObject(string type, string source, string message, Exception stackTrace = null, object extra = null) + { + var logJson = CreateLogObject(type, source, message, null, extra); + Console.WriteLine(logJson); + return Task.CompletedTask; + } + + private string CreateLogObject(string type, string source, string message, Exception stackTrace = null, object extra = null) + { + var logObject = new GeekbotLoggerObject() + { + Timestamp = DateTime.Now, + Type = type, + Source = source, + Message = message, + StackTrace = stackTrace, + Extra = extra + }; + return Utf8Json.JsonSerializer.ToJsonString(logObject); + } + } + + public class GeekbotLoggerObject + { + public DateTime Timestamp { get; set; } + public string Type { get; set; } + public string Source { get; set; } + public string Message { get; set; } + public Exception StackTrace { get; set; } + public object Extra { get; set; } + } + + public interface IGeekbotLogger + { + void Debug(string source, string message, object extra = null); + void Info(string source, string message, object extra = null); + void Error(string source, string message, Exception stackTrace, object extra = null); + } +} \ No newline at end of file diff --git a/Geekbot.net/Lib/SimpleConextConverter.cs b/Geekbot.net/Lib/SimpleConextConverter.cs new file mode 100644 index 0000000..ff6c2ef --- /dev/null +++ b/Geekbot.net/Lib/SimpleConextConverter.cs @@ -0,0 +1,100 @@ +using System; +using Discord.Commands; +using Discord.WebSocket; + +namespace Geekbot.net.Lib +{ + public class SimpleConextConverter + { + public static MessageDto ConvertContext(ICommandContext context) + { + return new MessageDto() + { + Message = new MessageDto.MessageContent() + { + Content = context.Message.Content, + Id = context.Message.Id.ToString(), + Attachments = context.Message.Attachments.Count, + ChannelMentions = context.Message.MentionedChannelIds.Count, + UserMentions = context.Message.MentionedUserIds.Count, + RoleMentions = context.Message.MentionedRoleIds.Count + }, + User = new MessageDto.IdAndName() + { + Id = context.User.Id.ToString(), + Name = $"{context.User.Username}#{context.User.Discriminator}" + }, + Guild = new MessageDto.IdAndName() + { + Id = context.Guild.Id.ToString(), + Name = context.Guild.Name + }, + Channel = new MessageDto.IdAndName() + { + Id = context.Channel.Id.ToString(), + Name = context.Channel.Name + }, + TimeStamp = DateTime.Now.ToString() + }; + } + public static MessageDto ConvertSocketMessage(SocketMessage message) + { + var channel = (SocketGuildChannel) message.Channel; + return new MessageDto() + { + Message = new MessageDto.MessageContent() + { + Content = message.Content, + Id = message.Id.ToString(), + Attachments = message.Attachments.Count, + ChannelMentions = message.MentionedChannels.Count, + UserMentions = message.MentionedUsers.Count, + RoleMentions = message.MentionedRoles.Count + }, + User = new MessageDto.IdAndName() + { + Id = message.Author.Id.ToString(), + Name = $"{message.Author.Username}#{message.Author.Discriminator}" + }, + Guild = new MessageDto.IdAndName() + { + Id = channel.Guild.Id.ToString(), + Name = channel.Guild.Name + }, + Channel = new MessageDto.IdAndName() + { + Id = channel.Id.ToString(), + Name = channel.Name + }, + TimeStamp = DateTime.Now.ToString() + }; + } + + } + + + public class MessageDto + { + public MessageContent Message { get; set; } + public IdAndName User { get; set; } + public IdAndName Guild { get; set; } + public IdAndName Channel { get; set; } + public string TimeStamp { get; set; } + + public class MessageContent + { + public string Content { get; set; } + public string Id { get; set; } + public int Attachments { get; set; } + public int ChannelMentions { get; set; } + public int UserMentions { get; set; } + public int RoleMentions { get; set; } + } + + public class IdAndName + { + public string Id { get; set; } + public string Name { get; set; } + } + } +} \ No newline at end of file diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index 4382309..9e25718 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -28,6 +28,7 @@ namespace Geekbot.net private IServiceProvider servicesProvider; private RedisValue token; private ILogger logger; + private IGeekbotLogger gLogger; private IUserRepository userRepository; private string[] args; private bool firstStart = false; @@ -43,10 +44,11 @@ namespace Geekbot.net logo.AppendLine("========================================="); Console.WriteLine(logo.ToString()); var logger = LoggerFactory.createLogger(args); + var gLogger = new GeekbotLogger(); logger.Information("[Geekbot] Starting..."); try { - new Program().MainAsync(args, logger).GetAwaiter().GetResult(); + new Program().MainAsync(args, logger, gLogger).GetAwaiter().GetResult(); } catch (Exception e) { @@ -54,11 +56,13 @@ namespace Geekbot.net } } - private async Task MainAsync(string[] args, ILogger logger) + private async Task MainAsync(string[] args, ILogger logger, IGeekbotLogger gLogger) { this.logger = logger; + this.gLogger = gLogger; this.args = args; logger.Information("[Geekbot] Initing Stuff"); + gLogger.Info("Geekbot", "Initing Stuff"); client = new DiscordSocketClient(new DiscordSocketConfig { @@ -142,7 +146,7 @@ namespace Geekbot.net services.AddSingleton(client); servicesProvider = services.BuildServiceProvider(); - var handlers = new Handlers(client, logger, redis, servicesProvider, commands, userRepository); + var handlers = new Handlers(client, logger, redis, servicesProvider, commands, userRepository, gLogger); client.MessageReceived += handlers.RunCommand; client.MessageReceived += handlers.UpdateStats;