/
Global.asax.cs
86 lines (69 loc) · 2.48 KB
/
Global.asax.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Reflection;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using System.Xml;
using Microsoft.AspNet.SignalR;
using Newtonsoft.Json;
namespace Microsoft.LogHub
{
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
StartUdpServer();
}
private async void StartUdpServer()
{
var endPoint = new IPEndPoint(IPAddress.Any, 0);
var udp = new UdpClient(8080);
var context = GlobalHost.ConnectionManager.GetHubContext<LogMessageHub>();
while (true)
{
var received = await udp.ReceiveAsync();
//TODO: Are we still listening for UDP messages at this point?
var buffer = received.Buffer;
var xmlText = System.Text.Encoding.UTF8.GetString(buffer);
var json = ConvertLog4JToJson(xmlText);
//This should be in the hub wrapper
var applicationId = GetApplicationId(xmlText);
if(applicationId != null)
context.Clients.Group(applicationId).newLogMessage(json);
}
}
public static string GetApplicationId(string xml)
{
const string Prefix = "<log4j:data name=\"ApplicationId\" value=\"";
try
{
var start = xml.IndexOf(Prefix);
if (start == -1)
return null;
start += Prefix.Length;
var end = xml.IndexOf('\"', start + 1);
return xml.Substring(start, end - start);
}
catch (Exception)
{
//Just eat exceptions to deal with malformed data
return null;
}
}
//Xml parsing in .NET sucks, convert it to JSON and let the client process it
public static string ConvertLog4JToJson(string xml)
{
var doc = new XmlDocument();
doc.LoadXml(xml.Replace("log4j:", ""));
var jsonText = JsonConvert.SerializeXmlNode(doc);
return jsonText;
}
}
}