geekbot/Geekbot.net/Lib/GeekbotLogger.cs

77 lines
2.7 KiB
C#
Raw Normal View History

2017-12-29 01:00:31 +01:00
using System;
using System.Threading.Tasks;
2018-05-01 02:31:54 +02:00
using Newtonsoft.Json;
2018-01-20 03:30:42 +01:00
using Serilog;
2017-12-29 01:00:31 +01:00
namespace Geekbot.net.Lib
{
public class GeekbotLogger : IGeekbotLogger
{
2018-01-20 03:30:42 +01:00
private readonly ILogger _serilog;
2017-12-29 01:00:31 +01:00
public GeekbotLogger()
{
2018-04-30 23:44:19 +02:00
_serilog = LoggerFactory.CreateLogger();
Information("Geekbot", "Using GeekbotLogger");
2017-12-29 01:00:31 +01:00
}
public void Debug(string source, string message, object extra = null)
{
HandleLogObject("Debug", source, message, null, extra);
}
public void Information(string source, string message, object extra = null)
2017-12-29 01:00:31 +01:00
{
HandleLogObject("Information", source, message, null, extra);
}
public void Warning(string source, string message, Exception stackTrace = null, object extra = null)
{
HandleLogObject("Warning", source, message, stackTrace, extra);
}
2017-12-29 01:00:31 +01:00
public void Error(string source, string message, Exception stackTrace, object extra = null)
{
HandleLogObject("Error", source, message, stackTrace, extra);
2017-12-29 01:00:31 +01:00
}
private Task HandleLogObject(string type, string source, string message, Exception stackTrace = null, object extra = null)
{
2018-02-14 23:40:44 +01:00
var logJson = CreateLogObject(type, source, message, stackTrace, extra);
2018-01-20 03:30:42 +01:00
// fuck serilog
_serilog.Information(logJson + "}");
2017-12-29 01:00:31 +01:00
return Task.CompletedTask;
}
private string CreateLogObject(string type, string source, string message, Exception stackTrace = null, object extra = null)
{
2018-04-30 23:44:19 +02:00
var logObject = new GeekbotLoggerObject
2017-12-29 01:00:31 +01:00
{
Timestamp = DateTime.Now,
Type = type,
Source = source,
Message = message,
StackTrace = stackTrace,
Extra = extra
};
2018-05-01 02:31:54 +02:00
return JsonConvert.SerializeObject(logObject);
2017-12-29 01:00:31 +01:00
}
}
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 Information(string source, string message, object extra = null);
void Warning(string source, string message, Exception stackTrace = null, object extra = null);
2017-12-29 01:00:31 +01:00
void Error(string source, string message, Exception stackTrace, object extra = null);
}
}