Change Logging to LoggingFacade

This commit is contained in:
Daniel_I_Am 2020-08-30 12:43:54 +02:00
parent f8b37391cb
commit 754f53c404
No known key found for this signature in database
GPG Key ID: 80C428FCC9743E84
27 changed files with 166 additions and 231 deletions

View File

@ -1,5 +1,4 @@
using NLog;
using System;
using System;
using Discord;
using Discord.Commands;
using Discord.WebSocket;
@ -8,13 +7,11 @@ using ChaosBot.Discord.Services;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace ChaosBot.Discord
{
public class DiscordConnect
{
public static DiscordSocketClient _client = null;
private static ILogger _logger = Program.Logger;
public static async Task StartUp()
{
@ -45,13 +42,13 @@ namespace ChaosBot.Discord
}
catch (Exception ex)
{
_logger.Error($"DiscordConnect.StartUp: Exception [{ex}] thrown, <[{ex.Message}]>.");
LoggingFacade.Exception(ex);
}
}
private static Task ReadyAsync()
{
_logger.Info($"Connected as -> [{_client.CurrentUser}] :)");
LoggingFacade.Info($"Connected as -> [{_client.CurrentUser}] :)");
return Task.CompletedTask;
}
@ -69,7 +66,7 @@ namespace ChaosBot.Discord
}
catch (Exception ex)
{
_logger.Error($"DiscordConnect.ConfigureServices: Exception [{ex}] thrown, <[{ex.Message}]>.");
LoggingFacade.Exception(ex);
}
return csInfo;
@ -82,34 +79,34 @@ namespace ChaosBot.Discord
switch (msg.Severity)
{
case LogSeverity.Critical:
_logger.Fatal(msg.Message);
LoggingFacade.Fatal(msg.Message);
break;
case LogSeverity.Debug:
_logger.Debug(msg.Message);
LoggingFacade.Debug(msg.Message);
break;
case LogSeverity.Error:
_logger.Error(msg.Message);
LoggingFacade.Error(msg.Message);
break;
case LogSeverity.Info:
_logger.Info(msg.Message);
LoggingFacade.Info(msg.Message);
break;
case LogSeverity.Warning:
_logger.Warn(msg.Message);
LoggingFacade.Warn(msg.Message);
break;
case LogSeverity.Verbose:
_logger.Trace(msg.Message);
LoggingFacade.Trace(msg.Message);
break;
default:
_logger.Trace(msg.Message);
LoggingFacade.Trace(msg.Message);
break;
}
}
catch (Exception ex)
{
_logger.Error($"DiscordConnect.Log: Exception [{ex}] thrown, <[{ex.Message}]>.");
LoggingFacade.Exception(ex);
}
return Task.CompletedTask;
}
}
}
}

View File

@ -5,25 +5,22 @@ using System.Threading.Tasks;
using System.Collections.Generic;
using System.Reflection;
using ChaosBot.Discord.PreConditions;
using NLog;
namespace ChaosBot.Discord.Modules.Admin
{
public class Clear : ModuleBase
{
private static readonly ILogger _logger = Program.Logger;
[Command("clear")]
[Alias("purge")]
[CheckCommandPerm("Admin")]
public async Task ClearCommand(int msgtoDelete = 1)
public async Task ClearCommand(int msgToDelete = 1)
{
try
{
IEnumerable<IMessage> messages = await Context.Channel.GetMessagesAsync(msgtoDelete + 1).FlattenAsync();
IEnumerable<IMessage> messages = await Context.Channel.GetMessagesAsync(msgToDelete + 1).FlattenAsync();
await ((ITextChannel) Context.Channel).DeleteMessagesAsync(messages);
const int delay = 3000;
IUserMessage m = await ReplyAsync($"{Context.User.Mention}, {msgtoDelete} messages deleted.");
IUserMessage m = await ReplyAsync($"{Context.User.Mention}, {msgToDelete} messages deleted.");
await Task.Delay(delay);
await m.DeleteAsync();
}
@ -31,8 +28,9 @@ namespace ChaosBot.Discord.Modules.Admin
{
if(ex.Message.Contains("Messages must be younger than two weeks old."))
await ReplyAsync($"{Context.User.Mention}, You cannot delete messages older then 2 weeks old.");
_logger.Error($"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
LoggingFacade.Error($"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
LoggingFacade.Debug($"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.\n{ex.StackTrace}");
}
}
}
}
}

View File

@ -1,8 +1,6 @@
using System;
using System.Linq;
using Discord.Commands;
using System.Threading.Tasks;
using System.Reflection;
using System.Text;
using ChaosBot.Discord.PreConditions;
using ChaosBot.Models;
@ -11,14 +9,11 @@ using ChaosBot.Services;
using Discord;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using NLog;
namespace ChaosBot.Discord.Modules.Admin
{
public class Config : ModuleBase
{
private static readonly ILogger _logger = Program.Logger;
[Command("config")]
[CheckCommandPerm("Admin")]
public async Task ConfigCommand(string cmd = "help", string key = null, string value = null)
@ -38,8 +33,7 @@ namespace ChaosBot.Discord.Modules.Admin
}
catch (Exception ex)
{
_logger.Error(
$"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
LoggingFacade.Exception(ex);
}
}
@ -75,8 +69,7 @@ namespace ChaosBot.Discord.Modules.Admin
}
catch (Exception ex)
{
_logger.Error(
$"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
LoggingFacade.Exception(ex);
}
}
@ -111,8 +104,7 @@ namespace ChaosBot.Discord.Modules.Admin
}
catch (Exception ex)
{
_logger.Error(
$"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
LoggingFacade.Exception(ex);
}
}
@ -152,8 +144,7 @@ namespace ChaosBot.Discord.Modules.Admin
}
catch (Exception ex)
{
_logger.Error(
$"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
LoggingFacade.Exception(ex);
}
}
@ -191,9 +182,8 @@ namespace ChaosBot.Discord.Modules.Admin
}
catch (Exception ex)
{
_logger.Error(
$"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
LoggingFacade.Exception(ex);
}
}
}
}
}

View File

@ -6,22 +6,16 @@ using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Text;
using Antlr4.Runtime.Misc;
using ChaosBot.Discord.PreConditions;
using ChaosBot.Lodestone;
using ChaosBot.Models;
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;
using NLog;
namespace ChaosBot.Discord.Modules.Admin
{
public class RankCheck : ModuleBase
{
private static readonly ILogger _logger = Program.Logger;
[Command("rankCheck")]
[Alias("rc")]
[CheckCommandPerm("Admin")]
@ -127,7 +121,7 @@ namespace ChaosBot.Discord.Modules.Admin
}
catch (Exception ex)
{
_logger.Error($"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
LoggingFacade.Exception(ex);
}
}
@ -149,11 +143,10 @@ namespace ChaosBot.Discord.Modules.Admin
}
catch (Exception ex)
{
_logger.Error(
$"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
LoggingFacade.Exception(ex);
}
return JsonConvert.DeserializeObject<List<LodestoneRank>>(JsonConvert.SerializeObject(JsonConvert.DeserializeObject<LodestoneRankApi>(response).Data));
}
}
}
}

View File

@ -3,21 +3,17 @@ using Discord;
using Discord.Commands;
using System.Threading.Tasks;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using ChaosBot.Discord.PreConditions;
using ChaosBot.Models;
using ChaosBot.Repositories;
using Microsoft.EntityFrameworkCore;
using NLog;
namespace ChaosBot.Discord.Modules.Admin
{
public class Role : ModuleBase
{
private static readonly ILogger Logger = Program.Logger;
[Command("role")]
[Alias("role help", "role info")]
[CheckCommandPerm("Admin")]
@ -53,7 +49,7 @@ namespace ChaosBot.Discord.Modules.Admin
}
catch (Exception ex)
{
Logger.Error($"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
LoggingFacade.Exception(ex);
}
}
@ -118,8 +114,7 @@ namespace ChaosBot.Discord.Modules.Admin
catch (Exception ex)
{
await ReplyAsync($"Something went wrong trying to process {m.Value}: {ex.Message}");
Logger.Error(
$"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
LoggingFacade.Exception(ex);
}
}
@ -128,7 +123,7 @@ namespace ChaosBot.Discord.Modules.Admin
}
catch (Exception ex)
{
Logger.Error($"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
LoggingFacade.Exception(ex);
}
}
@ -151,7 +146,7 @@ namespace ChaosBot.Discord.Modules.Admin
foreach (Match m in Regex.Matches(input, pattern))
{
Logger.Info(m.Value);
LoggingFacade.Info(m.Value);
try
{
IEmote emote;
@ -188,8 +183,7 @@ namespace ChaosBot.Discord.Modules.Admin
catch (Exception ex)
{
await ReplyAsync($"Something went wrong trying to process {m.Value}: {ex.Message}");
Logger.Error(
$"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
LoggingFacade.Exception(ex);
}
}
@ -198,7 +192,7 @@ namespace ChaosBot.Discord.Modules.Admin
}
catch (Exception ex)
{
Logger.Error($"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
LoggingFacade.Exception(ex);
}
}
}

View File

@ -1,5 +1,4 @@
using System;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using ChaosBot.Discord.PreConditions;
@ -7,14 +6,11 @@ using ChaosBot.Repositories;
using Discord;
using Discord.Commands;
using Microsoft.Extensions.Configuration;
using NLog;
namespace ChaosBot.Discord.Modules.User
{
public class Info : ModuleBase
{
private static readonly ILogger Logger = Program.Logger;
[Command("info")]
[Alias("version")]
[CheckCommandPerm("User")]
@ -41,9 +37,8 @@ namespace ChaosBot.Discord.Modules.User
}
catch(Exception ex)
{
Logger.Error(
$"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
LoggingFacade.Exception(ex);
}
}
}
}
}

View File

@ -1,22 +1,16 @@
using System;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using ChaosBot.Discord.PreConditions;
using ChaosBot.Models;
using ChaosBot.Repositories;
using Discord;
using Discord.Commands;
using Microsoft.Extensions.Configuration;
using NLog;
namespace ChaosBot.Discord.Modules.User
{
public class Level : ModuleBase
{
private static readonly ILogger Logger = Program.Logger;
[Command("level")]
[Alias("xp", "exp", "experience", "lvl")]
[CheckCommandPerm("User")]
@ -71,9 +65,8 @@ namespace ChaosBot.Discord.Modules.User
}
catch(Exception ex)
{
Logger.Error(
$"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
LoggingFacade.Exception(ex);
}
}
}
}
}

View File

@ -5,23 +5,17 @@ using Discord.Commands;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using ChaosBot.Discord.PreConditions;
using ChaosBot.Lodestone;
using ChaosBot.Models;
using ChaosBot.Repositories;
using ChaosBot.Services;
using Discord.Net;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using NLog;
namespace ChaosBot.Discord.Modules.User
{
public class Lodestone : ModuleBase
{
private static readonly ILogger _logger = Program.Logger;
[Command("lodestone")]
[Alias("ls")]
[CheckCommandPerm("User")]
@ -69,8 +63,7 @@ namespace ChaosBot.Discord.Modules.User
}
catch (Exception ex)
{
_logger.Error(
$"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
LoggingFacade.Exception(ex);
}
}
@ -115,8 +108,7 @@ namespace ChaosBot.Discord.Modules.User
}
catch (Exception ex)
{
_logger.Error(
$"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
LoggingFacade.Exception(ex);
}
}
@ -143,7 +135,7 @@ namespace ChaosBot.Discord.Modules.User
}
catch (Exception ex)
{
_logger.Error($"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
LoggingFacade.Exception(ex);
}
}
@ -240,7 +232,7 @@ namespace ChaosBot.Discord.Modules.User
}
catch (Exception ex)
{
_logger.Error($"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
LoggingFacade.Exception(ex);
}
}
@ -267,7 +259,7 @@ namespace ChaosBot.Discord.Modules.User
}
catch (Exception ex)
{
_logger.Error($"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
LoggingFacade.Exception(ex);
}
}
@ -364,4 +356,4 @@ namespace ChaosBot.Discord.Modules.User
return embed;
}
}
}
}

View File

@ -1,27 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using ChaosBot.Discord.PreConditions;
using ChaosBot.Migrations;
using ChaosBot.Models;
using Discord;
using Discord.Commands;
using NLog;
using ChaosBot.Repositories;
using ChaosBot.Services;
using Discord.WebSocket;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
namespace ChaosBot.Discord.Modules.User
{
public class Points : ModuleBase
{
private static ILogger _logger = Program.Logger;
[Command("points")]
[CheckCommandPerm("User")]
public async Task PointsCommand(string cmd = "total", SocketUser user = null, ulong Amount = 0)
@ -73,8 +66,7 @@ namespace ChaosBot.Discord.Modules.User
}
catch (Exception ex)
{
_logger.Error(
$"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
LoggingFacade.Exception(ex);
}
}
@ -110,8 +102,7 @@ namespace ChaosBot.Discord.Modules.User
}
catch (Exception ex)
{
_logger.Error(
$"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
LoggingFacade.Exception(ex);
}
}
@ -132,8 +123,7 @@ namespace ChaosBot.Discord.Modules.User
}
catch (Exception ex)
{
_logger.Error(
$"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
LoggingFacade.Exception(ex);
}
await ReplyAsync($"{Context.User.Mention}, you have {cur} points.", false);
@ -175,8 +165,7 @@ namespace ChaosBot.Discord.Modules.User
}
catch (Exception ex)
{
_logger.Error(
$"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
LoggingFacade.Exception(ex);
}
await ReplyAsync(
@ -283,8 +272,7 @@ namespace ChaosBot.Discord.Modules.User
}
catch (Exception ex)
{
_logger.Error(
$"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
LoggingFacade.Exception(ex);
}
}
@ -306,8 +294,7 @@ namespace ChaosBot.Discord.Modules.User
}
catch (Exception ex)
{
_logger.Error(
$"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
LoggingFacade.Exception(ex);
}
await ReplyAsync(

View File

@ -1,13 +1,11 @@
using System;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using ChaosBot.Discord.PreConditions;
using ChaosBot.Models;
using Discord;
using Discord.Commands;
using NLog;
using ChaosBot.Repositories;
using ChaosBot.Services;
using Discord.WebSocket;
@ -16,8 +14,6 @@ namespace ChaosBot.Discord.Modules.User
{
public class RaffleCmd : ModuleBase
{
private static ILogger _logger = Program.Logger;
[Command("raffle")]
[CheckCommandPerm("User")]
public async Task RaffleCommand(string cmd = "total", SocketUser user = null, int Amount = 0)
@ -79,8 +75,7 @@ namespace ChaosBot.Discord.Modules.User
}
catch (Exception ex)
{
_logger.Error(
$"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
LoggingFacade.Exception(ex);
}
}
@ -120,8 +115,7 @@ namespace ChaosBot.Discord.Modules.User
}
catch (Exception ex)
{
_logger.Error(
$"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
LoggingFacade.Exception(ex);
}
}
@ -142,8 +136,7 @@ namespace ChaosBot.Discord.Modules.User
}
catch (Exception ex)
{
_logger.Error(
$"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
LoggingFacade.Exception(ex);
}
await ReplyAsync($"{Context.User.Mention}, you have {cur} raffle tickets.", false);
@ -185,8 +178,7 @@ namespace ChaosBot.Discord.Modules.User
}
catch (Exception ex)
{
_logger.Error(
$"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
LoggingFacade.Exception(ex);
}
}
@ -221,8 +213,7 @@ namespace ChaosBot.Discord.Modules.User
}
catch (Exception ex)
{
_logger.Error(
$"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
LoggingFacade.Exception(ex);
}
await ReplyAsync(
@ -250,16 +241,15 @@ namespace ChaosBot.Discord.Modules.User
await dbContext.SaveChangesAsync();
}
}
await ReplyAsync(
$"{Context.User.Mention} has removed all raffle tickets from {user.Mention}.",
false);
}
catch (Exception ex)
{
_logger.Error(
$"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
LoggingFacade.Exception(ex);
}
await ReplyAsync(
$"{Context.User.Mention} has removed all raffle tickets from {user.Mention}.",
false);
}
[Command("raffle clear")]
@ -299,8 +289,7 @@ namespace ChaosBot.Discord.Modules.User
}
catch (Exception ex)
{
_logger.Error(
$"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
LoggingFacade.Exception(ex);
}
}
@ -332,8 +321,7 @@ namespace ChaosBot.Discord.Modules.User
}
catch (Exception ex)
{
_logger.Error(
$"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
LoggingFacade.Exception(ex);
}
}
}

View File

@ -1,6 +1,5 @@
using System;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using ChaosBot.Discord.PreConditions;
@ -8,14 +7,11 @@ using Dice;
using Discord;
using Discord.Commands;
using Microsoft.VisualBasic;
using NLog;
namespace ChaosBot.Discord.Modules.User
{
public class Roll : ModuleBase
{
private static readonly ILogger _logger = Program.Logger;
[Command("roll")]
[Alias("random", "dice")]
[CheckCommandPerm("User")]
@ -53,8 +49,7 @@ namespace ChaosBot.Discord.Modules.User
}
catch (Exception ex)
{
_logger.Error(
$"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
LoggingFacade.Exception(ex);
}
}
@ -129,4 +124,4 @@ namespace ChaosBot.Discord.Modules.User
return sb.ToString();
}
}
}
}

View File

@ -1,26 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using ChaosBot.Models;
using ChaosBot.Repositories;
using Discord.Commands;
using Discord.WebSocket;
using NLog;
namespace ChaosBot.Discord.PreConditions
{
public class CheckCommandPerm : PreconditionAttribute
{
private static ILogger _logger = Program.Logger;
private readonly string _defaultRole;
public CheckCommandPerm(string defaultRole) => _defaultRole = defaultRole;
public override Task<PreconditionResult> CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services)
{
// Debug information
_logger.Trace($"CheckCommandPerm.CheckPermissionsAsync|Checking permissions for command: {command.Name}");
LoggingFacade.Trace($"CheckCommandPerm.CheckPermissionsAsync|Checking permissions for command: {command.Name}");
// If user is not SocketGuildUser, then return error
if (!(context.User is SocketGuildUser gUser)) return Task.FromResult(PreconditionResult.FromError("You must be in a guild to run this command."));
@ -79,7 +76,7 @@ namespace ChaosBot.Discord.PreConditions
return Task.FromResult(PreconditionResult.FromSuccess());
}
else
_logger.Info($"CheckCommandperm.CheckPermissionsAsync|commandPermissions: No Default");
LoggingFacade.Info($"CheckCommandperm.CheckPermissionsAsync|commandPermissions: No Default");
}
// Permission denied

View File

@ -1,25 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ChaosBot.Models;
using ChaosBot.Repositories;
using Discord.Commands;
using Discord.WebSocket;
using NLog;
namespace ChaosBot.Discord.PreConditions
{
public class CheckModuleEnabled : PreconditionAttribute
{
private static ILogger _logger = Program.Logger;
private readonly string _moduleName;
public CheckModuleEnabled(string moduleName) => _moduleName = moduleName;
public override Task<PreconditionResult> CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services)
{
// Debug information
_logger.Trace($"CheckModuleEnabled.CheckPermissionsAsync|Checking module enabled for module: {_moduleName}");
LoggingFacade.Trace($"CheckModuleEnabled.CheckPermissionsAsync|Checking module enabled for module: {_moduleName}");
if (context.Guild == null) return Task.FromResult(PreconditionResult.FromError("This must be run in a guild."));

View File

@ -19,7 +19,6 @@ namespace ChaosBot.Discord.Services
private readonly CommandService _commands;
private readonly DiscordSocketClient _client;
private readonly IServiceProvider _services;
private readonly ILogger _logger = Program.Logger;
public CommandHandler(IServiceProvider services)
{
@ -43,7 +42,7 @@ namespace ChaosBot.Discord.Services
}
catch (Exception ex)
{
_logger.Error($"CommandHandler.CommandHandler: Exception [{ex}] thrown, <[{ex.Message}]>.");
LoggingFacade.Exception(ex);
}
}
@ -84,7 +83,7 @@ namespace ChaosBot.Discord.Services
}
catch (Exception ex)
{
_logger.Error($"CommandHandler.MessageReceivedAsync: Exception [{ex}] thrown, <[{ex.Message}]>.");
LoggingFacade.Exception(ex);
}
}
@ -119,8 +118,7 @@ namespace ChaosBot.Discord.Services
}
catch (Exception ex)
{
_logger.Error(
$"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
LoggingFacade.Exception(ex);
}
}
@ -180,8 +178,7 @@ namespace ChaosBot.Discord.Services
}
catch (Exception ex)
{
_logger.Error(
$"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
LoggingFacade.Exception(ex);
}
}
@ -191,22 +188,22 @@ namespace ChaosBot.Discord.Services
{
if (!command.IsSpecified)
{
_logger.Error($"Command failed to execute for [{context.User.Username}] <-> [{result.ErrorReason}]!");
LoggingFacade.Error($"Command failed to execute for [{context.User.Username}] <-> [{result.ErrorReason}]!");
return;
}
if (result.IsSuccess)
{
_logger.Info($"Command [{command.Value.Name}] executed for -> [{context.User.Username}]");
LoggingFacade.Info($"Command [{command.Value.Name}] executed for -> [{context.User.Username}]");
return;
}
_logger.Warn($"{context.User.Username} attempted to access {command.Value.Name} and was denied -> [{result}]");
LoggingFacade.Warn($"{context.User.Username} attempted to access {command.Value.Name} and was denied -> [{result}]");
await context.Channel.SendMessageAsync($"Sorry, {context.User.Username}, that command won't work for you.!");
}
catch (Exception ex)
{
_logger.Error($"CommandHandler.CommandExecutedAsync: Exception [{ex}] thrown, <[{ex.Message}]>.");
LoggingFacade.Exception(ex);
}
}
}

View File

@ -1,17 +1,13 @@
using System;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using ChaosBot.Models;
using Discord.Commands;
using NLog;
namespace ChaosBot.Discord.Services
{
public static class CustomCommandHandler
{
private static readonly ILogger Logger = Program.Logger;
public static async Task<bool> CheckCommand(SocketCommandContext context, int argPos)
{
try
@ -39,7 +35,7 @@ namespace ChaosBot.Discord.Services
}
catch (Exception ex)
{
Logger.Error($"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
LoggingFacade.Exception(ex);
}
return false;
}

View File

@ -1,6 +1,5 @@
using System;
using System.Linq;
using System.Reflection;
using ChaosBot.Discord.PreConditions;
using ChaosBot.Models;
using ChaosBot.Repositories;
@ -8,14 +7,11 @@ using Discord;
using Discord.Commands;
using Discord.WebSocket;
using Microsoft.EntityFrameworkCore;
using NLog;
namespace ChaosBot.Discord.Services
{
public class ExperienceHandler
{
private static readonly ILogger Logger = Program.Logger;
public static async void AddXp(SocketCommandContext context)
{
try
@ -100,8 +96,7 @@ namespace ChaosBot.Discord.Services
}
catch (Exception ex)
{
Logger.Error(
$"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
LoggingFacade.Exception(ex);
}
}

View File

@ -1,18 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using ChaosBot.Models;
using Discord;
using Discord.WebSocket;
using NLog;
namespace ChaosBot.Discord.Services
{
public static class RoleReactionHandler
{
private static readonly ILogger Logger = Program.Logger;
public static async void HandleReactionAdded(Cacheable<IUserMessage, ulong> cacheableMessage, ISocketMessageChannel socketMessageChannel, SocketReaction reaction)
{
Optional<IUser> optionalUser = reaction.User;
@ -38,7 +34,7 @@ namespace ChaosBot.Discord.Services
}
catch (Exception ex)
{
Logger.Error($"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
LoggingFacade.Exception(ex);
}
}
}
@ -68,9 +64,9 @@ namespace ChaosBot.Discord.Services
}
catch (Exception ex)
{
Logger.Error($"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
LoggingFacade.Exception(ex);
}
}
}
}
}
}

View File

@ -10,7 +10,6 @@ namespace ChaosBot.Discord.Services
{
public static class TimerHandler
{
private static readonly ILogger _logger = Program.Logger;
private static DiscordSocketClient _client;
public static void Initialize(IServiceProvider services)
@ -31,7 +30,7 @@ namespace ChaosBot.Discord.Services
SocketGuild guild = _client.GetGuild(Convert.ToUInt64(serverConfig.Key));
if (guild == null)
{
_logger.Warn($"Guild {Convert.ToUInt64(serverConfig.Key)} not found");
LoggingFacade.Warn($"Guild {Convert.ToUInt64(serverConfig.Key)} not found");
return;
}
@ -39,7 +38,7 @@ namespace ChaosBot.Discord.Services
guild.GetChannel(Convert.ToUInt64(lodestoneChannelSloganDescriptionId));
if (socketChannel == null)
{
_logger.Warn(
LoggingFacade.Warn(
$"Channel {Convert.ToUInt64(lodestoneChannelSloganDescriptionId)} not found in server {guild.Name}");
return;
}
@ -47,7 +46,7 @@ namespace ChaosBot.Discord.Services
ITextChannel channel = socketChannel as ITextChannel;
if (channel == null)
{
_logger.Warn($"Could not cast channel {socketChannel.Id} to ITextChannel");
LoggingFacade.Warn($"Could not cast channel {socketChannel.Id} to ITextChannel");
return;
}
@ -61,7 +60,7 @@ namespace ChaosBot.Discord.Services
}
catch (Exception ex)
{
_logger.Error($"TimerHandler.UpdateChannelSloganDescription: Exception [{ex}] thrown, <[{ex.Message}]>.");
LoggingFacade.Exception(ex);
}
}

View File

@ -1,4 +1,6 @@
using System;
using System.Diagnostics;
using System.Reflection;
using Microsoft.Extensions.Configuration;
using NLog;
using NLog.Extensions.Logging;
@ -22,4 +24,4 @@ namespace ChaosBot
}
}
}
}
}

59
ChaosBot/LoggingFacade.cs Normal file
View File

@ -0,0 +1,59 @@
using System;
using System.Diagnostics;
using System.Reflection;
using NLog;
namespace ChaosBot
{
public static class LoggingFacade
{
private static ILogger GetLogger()
{
return Program.GetLogger();
}
public static bool IsTraceEnabled => GetLogger().IsTraceEnabled;
public static bool IsDebugEnabled => GetLogger().IsDebugEnabled;
public static bool IsInfoEnabled => GetLogger().IsInfoEnabled;
public static bool IsWarnEnabled => GetLogger().IsWarnEnabled;
public static bool IsErrorEnabled => GetLogger().IsErrorEnabled;
public static bool IsFatalEnabled => GetLogger().IsFatalEnabled;
public static void Trace(object value)
{
GetLogger().Trace(value);
}
public static void Debug(object value)
{
GetLogger().Debug(value);
}
public static void Info(object value)
{
GetLogger().Info(value);
}
public static void Warn(object value)
{
GetLogger().Warn(value);
}
public static void Error(object value)
{
GetLogger().Error(value);
}
public static void Fatal(object value)
{
GetLogger().Fatal(value);
}
public static void Exception(Exception ex)
{
MethodBase method = new StackFrame(1).GetMethod();
Error($"{method?.ReflectedType?.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.");
Debug($"{method?.ReflectedType?.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.\n{ex.StackTrace}");
}
}
}

View File

@ -10,7 +10,7 @@ namespace ChaosBot
{
internal class Program
{
public static ILogger Logger;
private static ILogger _logger;
public static IConfiguration AppSettingsHandler;
private static string _appsettingsPath;
@ -21,6 +21,11 @@ namespace ChaosBot
new Program().MainFunction().GetAwaiter().GetResult();
}
public static ILogger GetLogger()
{
return _logger;
}
private async Task MainFunction()
{
try
@ -33,7 +38,7 @@ namespace ChaosBot
/*
* Initialize the _logger for logging purposes
*/
Logger = Logging.GenLog(configurationHandler);
_logger = Logging.GenLog(configurationHandler);
/*
* Set AppSettingsHandler on ConfigurationRepository
@ -43,7 +48,7 @@ namespace ChaosBot
/*
* Initialize the Discord Client and Login
*/
Logger.Info($"Starting Up {AppSettingsHandler.GetValue<string>("Bot:Name")} v{AppSettingsHandler.GetValue<string>("Bot:Version")}");
_logger.Info($"Starting Up {AppSettingsHandler.GetValue<string>("Bot:Name")} v{AppSettingsHandler.GetValue<string>("Bot:Version")}");
var discordBot = LoadDiscord();
@ -52,7 +57,7 @@ namespace ChaosBot
}
catch (Exception ex)
{
Logger.Error(ex, $"Program.MainFunction: Exception [{ex}] thrown, <[{ex.Message}]>.");
_logger.Error(ex, $"Program.MainFunction: Exception [{ex}] thrown, <[{ex.Message}]>.");
}
}
@ -73,4 +78,4 @@ namespace ChaosBot
return DiscordConnect.StartUp();
}
}
}
}

View File

@ -1,23 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ChaosBot.Models;
using ChaosBot.Repositories;
using Discord.Commands;
using Discord.WebSocket;
using NLog;
namespace ChaosBot.Services
{
public class CheckPermissions
{
private static ILogger _logger = Program.Logger;
public static Boolean CheckPerms(ICommandContext context, string command, string defaultRole = "User")
{
// Debug information
_logger.Trace($"CheckCommandPerm.CheckPermissionsAsync|Checking permissions for command: {command}");
LoggingFacade.Trace($"CheckCommandPerm.CheckPermissionsAsync|Checking permissions for command: {command}");
// If user is not SocketGuildUser, then return error
if (!(context.User is SocketGuildUser gUser)) return false;
@ -76,11 +72,11 @@ namespace ChaosBot.Services
return true;
}
else
_logger.Info($"CheckCommandperm.CheckPermissionsAsync|commandPermissions: No Default");
LoggingFacade.Info($"CheckCommandperm.CheckPermissionsAsync|commandPermissions: No Default");
}
// Permission denied
return false;
}
}
}
}

View File

@ -1,14 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using ChaosBot.Lodestone;
using Newtonsoft.Json;
using NLog;
using NLog.Fluent;
namespace ChaosBot.Services
{
@ -29,7 +25,6 @@ namespace ChaosBot.Services
static class LodestoneHttpConnection
{
private static readonly ILogger _logger = Program.Logger;
static HttpClient client = new HttpClient();
private static bool _firstRun = true;
@ -59,9 +54,9 @@ namespace ChaosBot.Services
}
catch (Exception ex)
{
_logger.Error(ex, $"HttpProxy.fetch<{typeof(T)}>: Exception [{ex}] thrown, <[{ex.Message}]>.");
LoggingFacade.Exception(ex);
throw;
}
}
}
}
}

View File

@ -1,19 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ChaosBot.Models;
using ChaosBot.Repositories;
using Discord.Commands;
using Discord.WebSocket;
using NLog;
namespace ChaosBot.Services
{
public class RestrictedConfig
{
private static ILogger _logger = Program.Logger;
public static Boolean IsAllowed(string key)
{
// TODO: List populated from DB
@ -25,4 +16,4 @@ namespace ChaosBot.Services
return true;
}
}
}
}

View File

@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace ChaosBot.Services
{
@ -114,4 +113,4 @@ namespace ChaosBot.Services
}
}
}
}
}

View File

@ -1,14 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using NLog;
namespace ChaosBot.WebServer.App
{
@ -18,7 +15,6 @@ namespace ChaosBot.WebServer.App
{
private static readonly AccessTokenCache Cache = WebServer.Cache;
private static readonly HttpClient client = new HttpClient();
private static readonly ILogger Logger = Program.Logger;
[HttpGet]
public async Task<IActionResult> Index(string code = null)
@ -138,4 +134,4 @@ namespace ChaosBot.WebServer.App
public int permissions { get; set; }
public int permissions_new { get; set; }
}
}
}

View File

@ -1,18 +1,14 @@
using System.IO;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using NLog;
namespace ChaosBot.WebServer
{
class Startup
{
private readonly ILogger _logger = Program.Logger;
public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration)
@ -38,7 +34,7 @@ namespace ChaosBot.WebServer
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
_logger.Info("Initializing Kestrel Startup and Configuration");
LoggingFacade.Info("Initializing Kestrel Startup and Configuration");
if (Program.AppSettingsHandler.GetValue<bool>("WebServer:Debug", false))
app.UseDeveloperExceptionPage();
@ -56,4 +52,4 @@ namespace ChaosBot.WebServer
app.UseStaticFiles();
}
}
}
}