2017-12-29 01:00:31 +01:00
|
|
|
|
using System;
|
2018-05-01 02:31:54 +02:00
|
|
|
|
using Newtonsoft.Json;
|
2017-12-29 01:00:31 +01:00
|
|
|
|
|
2018-05-03 00:56:06 +02:00
|
|
|
|
namespace Geekbot.net.Lib.Logger
|
2017-12-29 01:00:31 +01:00
|
|
|
|
{
|
|
|
|
|
public class GeekbotLogger : IGeekbotLogger
|
|
|
|
|
{
|
2018-05-02 23:32:40 +02:00
|
|
|
|
private readonly bool _logAsJson;
|
2018-05-01 22:55:47 +02:00
|
|
|
|
private readonly NLog.Logger _logger;
|
2018-05-01 16:50:48 +02:00
|
|
|
|
private readonly JsonSerializerSettings _serializerSettings;
|
|
|
|
|
|
2018-05-02 20:19:11 +02:00
|
|
|
|
public GeekbotLogger(RunParameters runParameters, bool sumologicActive)
|
2017-12-29 01:00:31 +01:00
|
|
|
|
{
|
2018-05-02 23:32:40 +02:00
|
|
|
|
_logAsJson = sumologicActive || runParameters.LogJson;
|
2018-05-02 20:19:11 +02:00
|
|
|
|
_logger = LoggerFactory.CreateNLog(runParameters, sumologicActive);
|
2018-05-01 16:50:48 +02:00
|
|
|
|
_serializerSettings = new JsonSerializerSettings
|
|
|
|
|
{
|
|
|
|
|
ReferenceLoopHandling = ReferenceLoopHandling.Serialize,
|
|
|
|
|
NullValueHandling = NullValueHandling.Include
|
|
|
|
|
};
|
2018-05-06 01:47:13 +02:00
|
|
|
|
Information(LogSource.Geekbot, "Using GeekbotLogger");
|
2017-12-29 01:00:31 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-06 01:47:13 +02:00
|
|
|
|
public void Trace(LogSource source, string message, object extra = null)
|
2018-05-03 00:56:06 +02:00
|
|
|
|
{
|
2018-05-03 21:20:49 +02:00
|
|
|
|
_logger.Trace(CreateLogString("Trace", source, message, null, extra));
|
2018-05-03 00:56:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-06 01:47:13 +02:00
|
|
|
|
public void Debug(LogSource source, string message, object extra = null)
|
2017-12-29 01:00:31 +01:00
|
|
|
|
{
|
2018-05-08 00:22:14 +02:00
|
|
|
|
if (_logAsJson) _logger.Info(CreateLogString("Debug", source, message, null, extra));
|
|
|
|
|
else _logger.Debug(CreateLogString("Debug", source, message, null, extra));
|
2017-12-29 01:00:31 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-06 01:47:13 +02:00
|
|
|
|
public void Information(LogSource source, string message, object extra = null)
|
2017-12-29 01:00:31 +01:00
|
|
|
|
{
|
2018-05-01 22:55:47 +02:00
|
|
|
|
_logger.Info(CreateLogString("Information", source, message, null, extra));
|
2017-12-29 01:00:31 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-06 01:47:13 +02:00
|
|
|
|
public void Warning(LogSource source, string message, Exception stackTrace = null, object extra = null)
|
2018-01-20 01:38:49 +01:00
|
|
|
|
{
|
2018-05-08 00:22:14 +02:00
|
|
|
|
if (_logAsJson) _logger.Info(CreateLogString("Warning", source, message, stackTrace, extra));
|
|
|
|
|
else _logger.Warn(CreateLogString("Warning", source, message, stackTrace, extra));
|
2018-01-20 01:38:49 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-06 01:47:13 +02:00
|
|
|
|
public void Error(LogSource source, string message, Exception stackTrace, object extra = null)
|
2017-12-29 01:00:31 +01:00
|
|
|
|
{
|
2018-05-08 00:22:14 +02:00
|
|
|
|
if (_logAsJson) _logger.Info(CreateLogString("Error", source, message, stackTrace, extra));
|
2018-05-06 01:47:13 +02:00
|
|
|
|
else _logger.Error(stackTrace, CreateLogString("Error", source, message, stackTrace, extra));
|
2017-12-29 01:00:31 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-10 00:00:51 +02:00
|
|
|
|
public NLog.Logger GetNLogger()
|
|
|
|
|
{
|
|
|
|
|
return _logger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool LogAsJson()
|
|
|
|
|
{
|
|
|
|
|
return _logAsJson;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-06 01:47:13 +02:00
|
|
|
|
private string CreateLogString(string type, LogSource source, string message, Exception stackTrace = null, object extra = null)
|
2017-12-29 01:00:31 +01:00
|
|
|
|
{
|
2018-05-02 23:32:40 +02:00
|
|
|
|
if (_logAsJson)
|
2017-12-29 01:00:31 +01:00
|
|
|
|
{
|
2018-05-01 21:07:27 +02:00
|
|
|
|
var logObject = new GeekbotLoggerObject
|
|
|
|
|
{
|
|
|
|
|
Timestamp = DateTime.Now,
|
|
|
|
|
Type = type,
|
|
|
|
|
Source = source,
|
|
|
|
|
Message = message,
|
|
|
|
|
StackTrace = stackTrace,
|
|
|
|
|
Extra = extra
|
|
|
|
|
};
|
|
|
|
|
return JsonConvert.SerializeObject(logObject, Formatting.None, _serializerSettings);
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-06 01:47:13 +02:00
|
|
|
|
if (source != LogSource.Message) return $"[{source}] - {message}";
|
2018-05-01 21:07:27 +02:00
|
|
|
|
|
|
|
|
|
var m = (MessageDto) extra;
|
2018-05-03 21:20:49 +02:00
|
|
|
|
return $"[{source}] - [{m?.Guild.Name} - {m?.Channel.Name}] {m?.User.Name}: {m?.Message.Content}";
|
2017-12-29 01:00:31 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|