This repository was archived by the owner on Apr 20, 2018. It is now read-only.
forked from microsoft/Tx
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathProgram.cs
More file actions
155 lines (131 loc) · 5.46 KB
/
Program.cs
File metadata and controls
155 lines (131 loc) · 5.46 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
using System;
using System.Net;
using System.Reactive;
using System.Reactive.Linq;
using System.Text;
using Tx.Windows;
using Tx.Windows.BounceMessages;
namespace CausalityNavigation
{
class Program
{
const string LocalTrace = @"LocalTrace.etl";
const int LinesPerPage = 30;
static HttpListener _listener = new HttpListener();
static HttpListenerRequest _request;
static StringBuilder _sb;
static string _processFilter;
static Playback _playback;
static IObservable<TracedEvent> _all;
static void Main(string[] args)
{
_listener.Prefixes.Add("http://" + Environment.MachineName + ":9000/");
_listener.Start();
Console.WriteLine("Listening ...");
while (true)
{
HttpListenerContext context = _listener.GetContext();
_request = context.Request;
HttpListenerResponse response = context.Response;
_sb = new StringBuilder("<HTML>\n<HEAD>\n<TITLE>");
_sb.Append("Trace 2 Html");
_sb.Append("</TITLE>\n</HEAD>\n<BODY>\n");
_sb.Append("<pre style=\"font-family:Consolas; font-size: 10pt; width: 5000px;\">");
_playback = new Playback();
_playback.AddEtlFiles(LocalTrace);
_all = _playback.GetObservable<TracedEvent>();
if (_request.QueryString["process"] != null)
{
_processFilter = _request.QueryString["process"];
_all = _all.Where(e => e.Message.StartsWith(_processFilter));
}
else
{
_processFilter = null;
}
if (_request.QueryString["after"] != null)
{
var after = int.Parse(_request.QueryString.Get("after"));
AllHistory(after);
}
else if (_request.QueryString["afterReceive"] != null)
{
string messageId = _request.QueryString.Get("afterReceive");
AfterReceive(messageId);
}
else if (_request.QueryString["beforeSend"] != null)
{
string messageId = _request.QueryString.Get("beforeSend");
BeforeSend(messageId);
}
else
{
AllHistory(0);
}
_sb.Append("</BODY></HTML>");
byte[] buffer = Encoding.UTF8.GetBytes(_sb.ToString());
response.ContentLength64 = buffer.Length;
response.OutputStream.Write(buffer, 0, buffer.Length);
response.OutputStream.Close();
_playback.Dispose();
}
}
static void AllHistory(int start)
{
if (start > 0)
{
_sb.AppendFormat("<A HREF=?after={0}>Previous</A><BR/>", start - LinesPerPage);
}
_all.Skip(start).Take(LinesPerPage).Subscribe(e => _sb.AppendLine(Format(e.Message)));
_playback.Run();
_sb.AppendFormat("<BR/><A HREF=?after={0}>Next</A>", start + LinesPerPage);
}
static void AfterReceive(string messageId)
{
string pattern = "received message " + messageId;
var received = _all.Where(e => e.Message.Contains(pattern));
var after = received.SelectMany(_all).Take(LinesPerPage);
var output = received.Merge(after);
output.Subscribe(e => _sb.AppendLine(Format(e.Message)));
_playback.Run();
}
static void BeforeSend(string messageId)
{
string pattern = "sending message " + messageId;
var send = _all.Where(e => e.Message.Contains(pattern));
var before = _all.TakeUntil(send);
var output = before.Merge(send);
output.TakeLast(LinesPerPage).Subscribe(e => _sb.AppendLine(Format(e.Message)));
_playback.Run();
}
static string Format(string message)
{
int index = message.IndexOf("sending message");
if (index > 0)
{
string[] tokens = message.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries);
string s = string.Format("<font color=\"Red\"><b>{0} {1} sending message <A HREF=\"http://{2}:9000/?afterReceive={3}&process={4}\">{3}</A> to {4} at {2}</b></font>",
tokens[0],
tokens[1],
tokens[8],
tokens[4],
tokens[6]);
return s;
}
index = message.IndexOf("received message");
if (index > 0)
{
string[] tokens = message.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
string s = string.Format("<font color=\"Green\"><b>{0} {1} received message <A HREF=\"http://{2}:9000/?beforeSend={3}&process={4}\">{3}</A> from {4} at {2}</b></font>",
tokens[0],
tokens[1],
tokens[8],
tokens[4],
tokens[6]);
return s;
}
return message;
}
}
}