Browse Source

Updated exception logs to include exceptions and better messages.

master
Teknikode 3 years ago
parent
commit
0afa77fc85
7 changed files with 43 additions and 39 deletions
  1. 7
    22
      Combot/Bot.cs
  2. 7
    7
      Combot/Databases/Database.cs
  3. 5
    0
      Combot/Logger.cs
  4. 5
    5
      Combot/Modules/Module.cs
  5. 14
    0
      Combot/Utility.cs
  6. 4
    4
      Modules/Github/Github.cs
  7. 1
    1
      Modules/Sed/Sed.cs

+ 7
- 22
Combot/Bot.cs View File

@@ -882,24 +882,24 @@ namespace Combot
}
}

private void HandleMysqlErrorEvent(object sender, string message)
private void HandleMysqlErrorEvent(object sender, Exception message)
{
BotError error = new BotError();
error.Message = message;
error.Message = message.GetExceptionMessage(true);
error.Type = ErrorType.MySQL;
Logger.LogToFile(ServerConfig.LogFilePath, "errors", "Mysql Error: " + message, ServerConfig.LogFileSizeMax);
Logger.LogToFile(ServerConfig.LogFilePath, "errors", "Mysql Error: " + message.GetExceptionMessage(true, true), ServerConfig.LogFileSizeMax);
if (ErrorEvent != null)
{
ErrorEvent(error);
}
}

private void HandleModuleErrorEvent(object sender, string message)
private void HandleModuleErrorEvent(object sender, Exception message)
{
BotError error = new BotError();
error.Message = message;
error.Message = message.GetExceptionMessage(true);
error.Type = ErrorType.Module;
Logger.LogToFile(ServerConfig.LogFilePath, "errors", "Module Error: " + message, ServerConfig.LogFileSizeMax);
Logger.LogToFile(ServerConfig.LogFilePath, "errors", message.GetExceptionMessage(true, true), ServerConfig.LogFileSizeMax);
if (ErrorEvent != null)
{
ErrorEvent(error);
@@ -937,26 +937,11 @@ namespace Combot
public void ThrowException(Exception ex, string message)
{
Exception newEx = new Exception(message, ex);
Logger.LogToFile(ServerConfig.LogFilePath, "errors", CreateExceptionMessage(newEx), ServerConfig.LogFileSizeMax);
Logger.LogToFile(ServerConfig.LogFilePath, "errors", newEx.GetExceptionMessage(true, true), ServerConfig.LogFileSizeMax);
if (ExceptionThrown != null)
{
ExceptionThrown(newEx);
}
}

public string CreateExceptionMessage(Exception ex)
{
string message = ex.Message;
if (ex.InnerException != null)
{
message += " Inner Exception: ";
message += CreateExceptionMessage(ex.InnerException);
}
else if (ex.StackTrace != null)
{
message += " Stack Trace: " + ex.StackTrace;
}
return message;
}
}
}

+ 7
- 7
Combot/Databases/Database.cs View File

@@ -8,7 +8,7 @@ namespace Combot.Databases
{
public class Database
{
public event EventHandler<string> MysqlErrorEvent;
public event EventHandler<Exception> MysqlErrorEvent;

private bool Connected { get; set; }
private MySqlConnection Connection { get; set; }
@@ -47,14 +47,14 @@ namespace Combot.Databases
{
if (MysqlErrorEvent != null)
{
MysqlErrorEvent(this, exception.Message);
MysqlErrorEvent(this, exception);
}
}
catch (Exception exception)
{
if (MysqlErrorEvent != null)
{
MysqlErrorEvent(this, exception.Message);
MysqlErrorEvent(this, exception);
}
}
DatabaseLock.ExitWriteLock();
@@ -78,14 +78,14 @@ namespace Combot.Databases
{
if (MysqlErrorEvent != null)
{
MysqlErrorEvent(this, exception.Message);
MysqlErrorEvent(this, exception);
}
}
catch (Exception exception)
{
if (MysqlErrorEvent != null)
{
MysqlErrorEvent(this, exception.Message);
MysqlErrorEvent(this, exception);
}
}
DatabaseLock.ExitWriteLock();
@@ -108,14 +108,14 @@ namespace Combot.Databases
{
if (MysqlErrorEvent != null)
{
MysqlErrorEvent(this, exception.Message);
MysqlErrorEvent(this, exception);
}
}
catch (Exception exception)
{
if (MysqlErrorEvent != null)
{
MysqlErrorEvent(this, exception.Message);
MysqlErrorEvent(this, exception);
}
}
DatabaseLock.ExitWriteLock();

+ 5
- 0
Combot/Logger.cs View File

@@ -10,6 +10,11 @@ namespace Combot
{
public static class Logger
{
public static void LogToFile(string directory, string fileName, Exception ex, int maxSize)
{
LogToFile(directory, fileName, ex.GetExceptionMessage(true), maxSize);
}

public static void LogToFile(string directory, string fileName, string message, int maxSize)
{
if (!Directory.Exists(directory))

+ 5
- 5
Combot/Modules/Module.cs View File

@@ -23,7 +23,7 @@ namespace Combot.Modules
public List<Command> Commands { get; set; }
public List<Option> Options { get; set; }

public event EventHandler<string> ModuleErrorEvent;
public event EventHandler<Exception> ModuleErrorEvent;

public event Action<Exception> ExceptionThrown;

@@ -111,13 +111,13 @@ namespace Combot.Modules

virtual public void ParseCommand(CommandMessage command) { }

protected void ThrowError(string e)
protected void ThrowError(Exception e)
{
string errorMsg = string.Format("[{0}] {1}", Name, e);
EventHandler<string> handler = ModuleErrorEvent;
Exception newEx = new Exception(string.Format("Module Error: {0}", Name), e);
EventHandler<Exception> handler = ModuleErrorEvent;
if (handler != null)
{
handler(this, errorMsg);
handler(this, newEx);
}
}


+ 14
- 0
Combot/Utility.cs View File

@@ -17,5 +17,19 @@ namespace Combot
string path = Uri.UnescapeDataString(uri.Path);
return Path.GetDirectoryName(path);
}

public static string GetExceptionMessage(this Exception ex, bool recursive = false, bool stackTrace = false)
{
string message = ex.Message;
if (ex.InnerException != null && recursive)
{
message += "Inner Exception: " + GetExceptionMessage(ex.InnerException, recursive);
}
else if(ex.StackTrace != null && stackTrace)
{
message += "Stack Trace: " + ex.StackTrace;
}
return message;
}
}
}

+ 4
- 4
Modules/Github/Github.cs View File

@@ -81,11 +81,11 @@ namespace Combot.Modules.Plugins
}
catch (Octokit.RateLimitExceededException ex)
{
ThrowError(ex.Message);
ThrowError(ex);
}
catch (Octokit.ApiValidationException ex)
{
ThrowError(ex.Message);
ThrowError(ex);
}
}

@@ -117,11 +117,11 @@ namespace Combot.Modules.Plugins
}
catch (Octokit.RateLimitExceededException ex)
{
ThrowError(ex.Message);
ThrowError(ex);
}
catch (Octokit.ApiValidationException ex)
{
ThrowError(ex.Message);
ThrowError(ex);
return;
}
}

+ 1
- 1
Modules/Sed/Sed.cs View File

@@ -113,7 +113,7 @@ namespace Combot.Modules.Plugins
}
catch (Exception ex)
{
ThrowError(ex.Message);
ThrowError(ex);
}
}
}

Loading…
Cancel
Save