Use LogSource Enum for logger and improve logging messages

This commit is contained in:
runebaas 2018-05-06 01:47:13 +02:00
parent 0fe273151c
commit 2b85caeb29
No known key found for this signature in database
GPG key ID: 2677AF508D0300D6
16 changed files with 122 additions and 95 deletions

View file

@ -18,35 +18,36 @@ namespace Geekbot.net.Lib.Logger
ReferenceLoopHandling = ReferenceLoopHandling.Serialize,
NullValueHandling = NullValueHandling.Include
};
Information("Geekbot", "Using GeekbotLogger");
Information(LogSource.Geekbot, "Using GeekbotLogger");
}
public void Trace(string source, string message, object extra = null)
public void Trace(LogSource source, string message, object extra = null)
{
_logger.Trace(CreateLogString("Trace", source, message, null, extra));
}
public void Debug(string source, string message, object extra = null)
public void Debug(LogSource source, string message, object extra = null)
{
_logger.Debug(CreateLogString("Debug", source, message, null, extra));
}
public void Information(string source, string message, object extra = null)
public void Information(LogSource source, string message, object extra = null)
{
_logger.Info(CreateLogString("Information", source, message, null, extra));
}
public void Warning(string source, string message, Exception stackTrace = null, object extra = null)
public void Warning(LogSource source, string message, Exception stackTrace = null, object extra = null)
{
_logger.Warn(CreateLogString("Warning", source, message, stackTrace, extra));
}
public void Error(string source, string message, Exception stackTrace, object extra = null)
public void Error(LogSource source, string message, Exception stackTrace, object extra = null)
{
_logger.Error(CreateLogString("Error", source, message, stackTrace, extra));
if (_logAsJson) _logger.Error(CreateLogString("Error", source, message, stackTrace, extra));
else _logger.Error(stackTrace, CreateLogString("Error", source, message, stackTrace, extra));
}
private string CreateLogString(string type, string source, string message, Exception stackTrace = null, object extra = null)
private string CreateLogString(string type, LogSource source, string message, Exception stackTrace = null, object extra = null)
{
if (_logAsJson)
{
@ -62,7 +63,7 @@ namespace Geekbot.net.Lib.Logger
return JsonConvert.SerializeObject(logObject, Formatting.None, _serializerSettings);
}
if (source != "Message") return $"[{source}] - {message}";
if (source != LogSource.Message) return $"[{source}] - {message}";
var m = (MessageDto) extra;
return $"[{source}] - [{m?.Guild.Name} - {m?.Channel.Name}] {m?.User.Name}: {m?.Message.Content}";