Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added message templating and corrected for Seq v2 #1

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 77 additions & 5 deletions Seq.App.HipChat/HipChatReactor.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Text.RegularExpressions;
using Seq.Apps;
using Seq.Apps.LogEvents;

Expand All @@ -13,6 +15,8 @@ namespace Seq.App.HipChat
Description = "Sends log events to HipChat.")]
public class HipChatReactor : Reactor, ISubscribeTo<LogEventData>
{
static Regex placeholdersRx = new Regex("(\\[(?<key>[^\\[\\]]+?)(\\:(?<format>[^\\[\\]]+?))?\\])", RegexOptions.CultureInvariant | RegexOptions.Compiled);

private static IDictionary<LogEventLevel, string> _levelColorMap = new Dictionary<LogEventLevel, string>
{
{LogEventLevel.Verbose, "gray"},
Expand Down Expand Up @@ -43,6 +47,11 @@ public class HipChatReactor : Reactor, ISubscribeTo<LogEventData>
IsOptional = true)]
public string Color { get; set; }

[SeqAppSetting(
HelpText = "The message template to use when writing the message to HipChat. Can consist of any standard HTML. Event property values can be added in the format [PropertyKey]. (default: <strong>[Level]:</strong> [RenderedMessage])",
IsOptional = true)]
public string MessageTemplate { get; set; }

[SeqAppSetting(
HelpText = "Whether or not messages should trigger notifications for people in the room (change the tab color, play a sound, etc). Each recipient's notification preferences are taken into account.",
IsOptional = true)]
Expand All @@ -56,7 +65,8 @@ public async void On(Event<LogEventData> evt)
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

var msg = new StringBuilder("<strong>" + evt.Data.Level + ":</strong> " + evt.Data.RenderedMessage);
var msg = new StringBuilder();
AddMessage(evt, msg);
if (msg.Length > 1000)
{
msg.Length = 1000;
Expand All @@ -65,10 +75,7 @@ public async void On(Event<LogEventData> evt)
if (!string.IsNullOrWhiteSpace(BaseUrl))
{
msg.AppendLine();
msg.AppendLine(
string.Format(
"<a href=\"{0}/#/now?filter=@Id%20%3D%3D%20%22{1}%22&show=expanded\">Click here to open in Seq</a>",
BaseUrl, evt.Id));
msg.AppendLine(GenerateSeqUrl(evt));
}

var color = Color;
Expand Down Expand Up @@ -96,5 +103,70 @@ public async void On(Event<LogEventData> evt)
}
}
}

private void AddMessage(Event<LogEventData> evt, StringBuilder msg)
{
var messageTemplateToUse = MessageTemplate;

if (string.IsNullOrWhiteSpace(MessageTemplate))
{
MessageTemplate = "<strong>[Level]:</strong> [RenderedMessage]";
}

msg.AppendFormat(SubstitutePlaceholders(messageTemplateToUse, evt));
}

private string SubstitutePlaceholders(string messageTemplateToUse, Event<LogEventData> evt)
{
var data = evt.Data;
var eventType = evt.EventType;

var placeholders = data.Properties.ToDictionary(k => k.Key.ToLower(), v => v.Value);

AddValueIfKeyDoesntExist(placeholders, "EventType", eventType);
AddValueIfKeyDoesntExist(placeholders, "RenderedMessage", data.RenderedMessage);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see you set some custom properties here, but you never set "Level". Or am I missing something obvious?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good spot, I've added it.

Will give it a while in the way you've suggested cheers


return placeholdersRx.Replace(messageTemplateToUse, delegate(Match m)
{
var key = m.Groups["key"].Value.ToLower();
var format = m.Groups["format"].Value;
return placeholders.ContainsKey(key) ? FormatValue(placeholders[key], format) : m.Value;
});
}

private string FormatValue(object value, string format)
{
var rawValue = value != null ? value.ToString() : "(Null)";

if (string.IsNullOrWhiteSpace(format))
{
return rawValue;
}

try
{
return string.Format(format, rawValue);
}
catch (Exception ex)
{
Log.Error(ex, "Could not format HipChat message: {value} {format}", value, format);
}

return rawValue;
}

private static void AddValueIfKeyDoesntExist(Dictionary<string, object> placeholders, string key, object value)
{
var loweredKey = key.ToLower();
if (!placeholders.ContainsKey(loweredKey))
{
placeholders.Add(loweredKey, value);
}
}

private string GenerateSeqUrl(Event<LogEventData> evt)
{
return string.Format("<a href=\"{0}/#/events?filter=@Id%20%3D%3D%20%22{1}%22&show=expanded\">Click here to open in Seq</a>", BaseUrl, evt.Id);
}
}
}
1 change: 1 addition & 0 deletions Seq.App.HipChat/Seq.App.HipChat.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Microsoft.AspNet.WebApi.Client.5.1.2\lib\net45\System.Net.Http.Formatting.dll</HintPath>
</Reference>
<Reference Include="System.Runtime" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
Expand Down