101 lines
3.5 KiB
C#
101 lines
3.5 KiB
C#
using LCU.Api.Internal.MaidBotLib.Models;
|
|
using Newtonsoft.Json;
|
|
using Seq.Apps;
|
|
using Seq.Apps.LogEvents;
|
|
using System;
|
|
using System.Net.Http;
|
|
using System.Text;
|
|
|
|
namespace LCU.Api.Internal.MaidBotSeqApp
|
|
{
|
|
/// <summary>
|
|
/// Queues LogEvent.
|
|
/// </summary>
|
|
[SeqApp("QueueLogs", Description = "This will queue logs based on a signal event")]
|
|
public class SeqToRabbit : SeqApp, ISubscribeTo<LogEventData>
|
|
{
|
|
private static readonly HttpClient _client = new HttpClient();
|
|
|
|
[SeqAppSetting(DisplayName = "Url", HelpText = "Url of MaidBot")]
|
|
public string Url { get; set; }
|
|
[SeqAppSetting(DisplayName = "DebugMode", HelpText = "Setting this will set DebugData field to the events serialized object and save to our DebugEventLog database table.")]
|
|
public bool DebugMode { get; set; }
|
|
|
|
protected override void OnAttached()
|
|
{
|
|
base.OnAttached();
|
|
}
|
|
|
|
// Called by Seq whenever an event is sent to the app
|
|
public void On(Event<LogEventData> evt)
|
|
{
|
|
EventLog logRequest = CreateLogRequest(evt);
|
|
|
|
if(logRequest == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
QueueLogEvent(logRequest);
|
|
}
|
|
|
|
public bool MissingRequiredFields(Event<LogEventData> evt)
|
|
{
|
|
return evt == null ||
|
|
evt.Data == null ||
|
|
evt.Data.RenderedMessage == null ||
|
|
evt.Data.Properties == null ||
|
|
!evt.Data.Properties.ContainsKey("Application") ||
|
|
!evt.Data.Properties.ContainsKey("Group");
|
|
}
|
|
|
|
public EventLog CreateLogRequest(Event<LogEventData> evt)
|
|
{
|
|
var eventLog = new EventLog();
|
|
|
|
try
|
|
{
|
|
if (MissingRequiredFields(evt))
|
|
{
|
|
Log.Debug("Seq LogEventData missing required fields to be logged by MaidBot.");
|
|
return null;
|
|
}
|
|
|
|
eventLog.Application = evt.Data.Properties["Application"].ToString();
|
|
eventLog.Level = Enum.GetName(typeof(LogEventLevel), evt.Data.Level);
|
|
eventLog.LogTimeStamp = evt.TimestampUtc;
|
|
eventLog.Message = evt.Data.RenderedMessage;
|
|
eventLog.Group = evt.Data.Properties["Group"].ToString();
|
|
eventLog.Environment = evt.Data.Properties.ContainsKey("Environment") ? evt.Data.Properties["Environment"].ToString() : "";
|
|
eventLog.DebugData = DebugMode ? JsonConvert.SerializeObject(evt) : "";
|
|
|
|
return eventLog;
|
|
}
|
|
catch(Exception ex)
|
|
{
|
|
Log.Debug(ex, "[Error] MaidBot: Error Creating Event Log.");
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public void QueueLogEvent(EventLog eventLog)
|
|
{
|
|
try
|
|
{
|
|
var encodedContent = JsonConvert.SerializeObject(eventLog);
|
|
var content = new StringContent(encodedContent, Encoding.UTF8, "application/json");
|
|
HttpResponseMessage response = _client.PostAsync(Url, content).Result;
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
{
|
|
Log.Debug("[Error] Failure to send Event Log to MaidBot.");
|
|
}
|
|
}
|
|
catch(Exception ex)
|
|
{
|
|
Log.Debug(ex, "[Error] MaidBot: Failure to send Event Log to MaidBot.");
|
|
}
|
|
}
|
|
}
|
|
}
|