Adding MaidBotApi
This commit is contained in:
+42
@@ -0,0 +1,42 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<Description>LCU.Api.Internal.MaidBotSeqApp</Description>
|
||||
<Version>1.0.0</Version>
|
||||
<RunPostBuildEvent>Always</RunPostBuildEvent>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Http" Version="3.1.9" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
|
||||
<PackageReference Include="Seq.Apps" Version="5.1.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\LCU.Api.Internal.MaidBotLib\LCU.Api.Internal.MaidBotLib.csproj" PrivateAssets="All" />
|
||||
</ItemGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetsForTfmSpecificBuildOutput>$(TargetsForTfmSpecificBuildOutput);CopyProjectReferencesToPackage</TargetsForTfmSpecificBuildOutput>
|
||||
</PropertyGroup>
|
||||
|
||||
<Target Name="CopyProjectReferencesToPackage" DependsOnTargets="BuildOnlySettings;ResolveReferences">
|
||||
<ItemGroup>
|
||||
<!-- Filter out unnecessary files -->
|
||||
<_ReferenceCopyLocalPaths Include="@(ReferenceCopyLocalPaths->WithMetadataValue('ReferenceSourceTarget', 'ProjectReference')->WithMetadataValue('PrivateAssets', 'All'))" />
|
||||
</ItemGroup>
|
||||
|
||||
<!-- Print batches for debug purposes -->
|
||||
<Message Text="Batch for .nupkg: ReferenceCopyLocalPaths = @(_ReferenceCopyLocalPaths), ReferenceCopyLocalPaths.DestinationSubDirectory = %(_ReferenceCopyLocalPaths.DestinationSubDirectory) Filename = %(_ReferenceCopyLocalPaths.Filename) Extension = %(_ReferenceCopyLocalPaths.Extension)" Importance="High" Condition="'@(_ReferenceCopyLocalPaths)' != ''" />
|
||||
|
||||
<ItemGroup>
|
||||
<!-- Add file to package with consideration of sub folder. If empty, the root folder is chosen. -->
|
||||
<BuildOutputInPackage Include="@(_ReferenceCopyLocalPaths)" TargetPath="%(_ReferenceCopyLocalPaths.DestinationSubDirectory)" />
|
||||
</ItemGroup>
|
||||
</Target>
|
||||
|
||||
</Project>
|
||||
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
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.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user