Groundwork for sumologic

This commit is contained in:
runebaas 2017-12-29 01:00:31 +01:00
parent efadbe9a21
commit 82b2b4c815
No known key found for this signature in database
GPG key ID: 2677AF508D0300D6
5 changed files with 182 additions and 58 deletions

View file

@ -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)
{

View file

@ -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))
@ -108,30 +81,6 @@ 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

View file

@ -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);
}
}

View file

@ -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; }
}
}
}

View file

@ -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<DiscordSocketClient>(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;