-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathExtendedEventsEventReader.cs
More file actions
128 lines (106 loc) · 3.68 KB
/
Copy pathExtendedEventsEventReader.cs
File metadata and controls
128 lines (106 loc) · 3.68 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
using Microsoft.Data.SqlClient;
using NLog;
using WorkloadTools;
using WorkloadTools.Listener.ExtendedEvents;
namespace ConvertWorkload
{
public class ExtendedEventsEventReader : EventReader
{
private static Logger logger = LogManager.GetCurrentClassLogger();
private string filePath;
private bool started = false;
private bool finished = false;
private FileTargetXEventDataReader reader;
public ExtendedEventsEventReader(string path)
{
Events = new BinarySerializedBufferedEventQueue();
Events.BufferSize = 10000;
filePath = path;
Filter = new ExtendedEventsEventFilter();
}
private void ReadEventsFromFile()
{
try
{
var info = new SqlConnectionInfo();
info.ServerName = "(localdb)\\MSSQLLocalDB";
var sqlCreateTable = @"
IF OBJECT_ID('tempdb.dbo.trace_reader_queue') IS NULL
BEGIN
CREATE TABLE tempdb.dbo.trace_reader_queue (
ts datetime DEFAULT GETDATE(),
path nvarchar(4000)
)
END
TRUNCATE TABLE tempdb.dbo.trace_reader_queue;
INSERT INTO tempdb.dbo.trace_reader_queue (path) VALUES(@path);
";
using (var conn = new SqlConnection())
{
conn.ConnectionString = info.ConnectionString();
conn.Open();
using (var cmd = conn.CreateCommand())
{
cmd.CommandText = sqlCreateTable;
var prm = new SqlParameter()
{
ParameterName = "@path",
DbType = System.Data.DbType.String,
Size = 4000,
Value = filePath
};
cmd.Parameters.Add(prm);
cmd.ExecuteNonQuery();
}
}
reader = new FileTargetXEventDataReader(info.ConnectionString(), null, Events, ExtendedEventsWorkloadListener.ServerType.LocalDB);
reader.ReadEvents();
finished = true;
}
catch (Exception ex)
{
logger.Error(ex.Message);
if (ex.InnerException != null)
{
logger.Error(ex.InnerException.Message);
}
Dispose();
}
}
public override bool HasFinished()
{
return finished && !Events.HasMoreElements();
}
public override bool HasMoreElements()
{
return !finished && !stopped && (started ? Events.HasMoreElements() : true);
}
public override WorkloadEvent Read()
{
if (!started)
{
var t = Task.Factory.StartNew(ReadEventsFromFile);
started = true;
}
WorkloadEvent result = null;
while (!Events.TryDequeue(out result))
{
if (stopped || finished)
{
return null;
}
Thread.Sleep(5);
}
return result;
}
protected override void Dispose(bool disposing)
{
if (!stopped)
{
stopped = true;
reader.Stop();
reader.Dispose();
}
}
}
}