Skip to content

Commit e996351

Browse files
committed
Write Exception to Event Log
1 parent 9a67e19 commit e996351

File tree

2 files changed

+134
-65
lines changed

2 files changed

+134
-65
lines changed

utPLSQL.Api/utPLSQL.Api/RealTimeTestRunner.cs

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System;
33
using System.Collections.Generic;
44
using System.Data;
5+
using System.Diagnostics;
56
using System.IO;
67
using System.Threading.Tasks;
78
using System.Xml.Serialization;
@@ -15,14 +16,25 @@ public class RealTimeTestRunner : TestRunner<@event>
1516
{
1617
public override async Task RunTestsAsync(List<string> paths, Action<@event> consumer)
1718
{
18-
if (paths != null && paths.Count > 0)
19+
try
1920
{
20-
var realtimeReporterId = Guid.NewGuid().ToString().Replace("-", "");
21+
if (paths != null && paths.Count > 0)
22+
{
23+
var realtimeReporterId = Guid.NewGuid().ToString().Replace("-", "");
2124

22-
var taskRun = Task.Run(() => UtRun(realtimeReporterId, paths));
23-
var taskConsume = Task.Run(() => ConsumeResult(realtimeReporterId, consumer));
25+
var taskRun = Task.Run(() => UtRun(realtimeReporterId, paths));
26+
var taskConsume = Task.Run(() => ConsumeResult(realtimeReporterId, consumer));
2427

25-
await Task.WhenAll(taskRun, taskConsume);
28+
await Task.WhenAll(taskRun, taskConsume);
29+
}
30+
}
31+
catch (Exception e)
32+
{
33+
using (EventLog eventLog = new EventLog("Application"))
34+
{
35+
eventLog.Source = "Application";
36+
eventLog.WriteEntry($"{e.Message}\r\n{e.StackTrace}", EventLogEntryType.Error);
37+
}
2638
}
2739
}
2840

@@ -33,21 +45,33 @@ public override async Task RunTestsAsync(string path, Action<@event> consumer)
3345

3446
public override async Task<string> RunTestsWithCoverageAsync(List<string> paths, Action<@event> consumer, List<string> coverageSchemas = null, List<string> includeObjects = null, List<string> excludeObjects = null)
3547
{
36-
if (paths != null && paths.Count > 0)
48+
try
3749
{
38-
var realtimeReporterId = Guid.NewGuid().ToString().Replace("-", "");
39-
var coverageReporterId = Guid.NewGuid().ToString().Replace("-", "");
40-
41-
var taskRun = Task.Run(() => UtRunWithCoverage(realtimeReporterId, coverageReporterId, paths, coverageSchemas, includeObjects, excludeObjects));
42-
var taskConsume = Task.Run(() => ConsumeResult(realtimeReporterId, consumer));
43-
var taskCoverageReport = Task.Run(() => GetCoverageReport(coverageReporterId));
44-
45-
await Task.WhenAll(taskRun, taskConsume, taskCoverageReport);
46-
47-
return taskCoverageReport.Result;
50+
if (paths != null && paths.Count > 0)
51+
{
52+
var realtimeReporterId = Guid.NewGuid().ToString().Replace("-", "");
53+
var coverageReporterId = Guid.NewGuid().ToString().Replace("-", "");
54+
55+
var taskRun = Task.Run(() => UtRunWithCoverage(realtimeReporterId, coverageReporterId, paths, coverageSchemas, includeObjects, excludeObjects));
56+
var taskConsume = Task.Run(() => ConsumeResult(realtimeReporterId, consumer));
57+
var taskCoverageReport = Task.Run(() => GetCoverageReport(coverageReporterId));
58+
59+
await Task.WhenAll(taskRun, taskConsume, taskCoverageReport);
60+
61+
return taskCoverageReport.Result;
62+
}
63+
else
64+
{
65+
return null;
66+
}
4867
}
49-
else
68+
catch (Exception e)
5069
{
70+
using (EventLog eventLog = new EventLog("Application"))
71+
{
72+
eventLog.Source = "Application";
73+
eventLog.WriteEntry($"{e.Message}\r\n{e.StackTrace}", EventLogEntryType.Error);
74+
}
5175
return null;
5276
}
5377
}

utPLSQL.Api/utPLSQL.Api/TestRunner.cs

Lines changed: 93 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System;
33
using System.Collections.Generic;
44
using System.Data;
5+
using System.Diagnostics;
56
using System.Text;
67
using System.Threading.Tasks;
78

@@ -28,28 +29,39 @@ public abstract class TestRunner<T>
2829
/// <param name="connectAs">Connect as</param>
2930
public void Connect(string username, string password, string database, string connectAs = null)
3031
{
31-
string connectionString;
32-
if (string.IsNullOrEmpty(connectAs))
32+
try
3333
{
34-
connectionString = $"User Id={username};Password={password};Data Source={database}";
35-
}
36-
else
37-
{
38-
connectionString = $"User Id={username};DBA Privilege={connectAs};Password={password};Data Source={database}";
39-
}
34+
string connectionString;
35+
if (string.IsNullOrEmpty(connectAs))
36+
{
37+
connectionString = $"User Id={username};Password={password};Data Source={database}";
38+
}
39+
else
40+
{
41+
connectionString = $"User Id={username};DBA Privilege={connectAs};Password={password};Data Source={database}";
42+
}
4043

41-
foreach (var command in runningCommands)
42-
{
43-
command.Cancel();
44-
}
44+
foreach (var command in runningCommands)
45+
{
46+
command.Cancel();
47+
}
4548

46-
produceConnection = new OracleConnection(connectionString);
47-
produceConnection.Open();
49+
produceConnection = new OracleConnection(connectionString);
50+
produceConnection.Open();
4851

49-
consumeConnection = new OracleConnection(connectionString);
50-
consumeConnection.Open();
52+
consumeConnection = new OracleConnection(connectionString);
53+
consumeConnection.Open();
54+
}
55+
catch (Exception e)
56+
{
57+
using (EventLog eventLog = new EventLog("Application"))
58+
{
59+
eventLog.Source = "Application";
60+
eventLog.WriteEntry($"{e.Message}\r\n{e.StackTrace}", EventLogEntryType.Error);
61+
}
62+
}
5163
}
52-
64+
5365
/// <summary>
5466
/// Closes both connections
5567
/// </summary>
@@ -62,11 +74,17 @@ public void Close()
6274

6375
if (produceConnection != null)
6476
{
65-
try {
66-
produceConnection.Close();
77+
try
78+
{
79+
produceConnection.Close();
6780
}
68-
catch
81+
catch (Exception e)
6982
{
83+
using (EventLog eventLog = new EventLog("Application"))
84+
{
85+
eventLog.Source = "Application";
86+
eventLog.WriteEntry($"{e.Message}\r\n{e.StackTrace}", EventLogEntryType.Error);
87+
}
7088
}
7189
}
7290
if (consumeConnection != null)
@@ -75,8 +93,13 @@ public void Close()
7593
{
7694
consumeConnection.Close();
7795
}
78-
catch
96+
catch (Exception e)
7997
{
98+
using (EventLog eventLog = new EventLog("Application"))
99+
{
100+
eventLog.Source = "Application";
101+
eventLog.WriteEntry($"{e.Message}\r\n{e.StackTrace}", EventLogEntryType.Error);
102+
}
80103
}
81104
}
82105
}
@@ -87,20 +110,31 @@ public void Close()
87110
/// <returns>Version as string</returns>
88111
public string GetVersion()
89112
{
90-
var cmd = new OracleCommand("select ut.version() from dual", produceConnection);
91-
runningCommands.Add(cmd);
113+
try
114+
{
115+
var cmd = new OracleCommand("select ut.version() from dual", produceConnection);
116+
runningCommands.Add(cmd);
92117

93-
var reader = cmd.ExecuteReader();
94-
reader.Read();
118+
var reader = cmd.ExecuteReader();
119+
reader.Read();
95120

96-
var version = reader.GetString(0);
121+
var version = reader.GetString(0);
97122

98-
reader.Close();
123+
reader.Close();
99124

100-
runningCommands.Remove(cmd);
101-
cmd.Dispose();
125+
runningCommands.Remove(cmd);
126+
cmd.Dispose();
102127

103-
return version;
128+
return version;
129+
}
130+
catch (Exception e)
131+
{
132+
using (EventLog eventLog = new EventLog("Application"))
133+
{
134+
eventLog.Source = "Application";
135+
eventLog.WriteEntry($"{e.Message}\r\n{e.StackTrace}", EventLogEntryType.Error);
136+
}
137+
}
104138
}
105139

106140
/// <summary>
@@ -141,38 +175,49 @@ public string GetVersion()
141175

142176
protected string GetCoverageReport(string id)
143177
{
144-
var sb = new StringBuilder();
178+
try
179+
{
180+
var sb = new StringBuilder();
145181

146-
var proc = @"DECLARE
182+
var proc = @"DECLARE
147183
l_reporter ut_coverage_html_reporter := ut_coverage_html_reporter();
148184
BEGIN
149185
l_reporter.set_reporter_id(:id);
150186
:lines_cursor := l_reporter.get_lines_cursor();
151187
END;";
152188

153-
var cmd = new OracleCommand(proc, consumeConnection);
154-
runningCommands.Add(cmd);
189+
var cmd = new OracleCommand(proc, consumeConnection);
190+
runningCommands.Add(cmd);
155191

156-
cmd.Parameters.Add("id", OracleDbType.Varchar2, ParameterDirection.Input).Value = id;
157-
cmd.Parameters.Add("lines_cursor", OracleDbType.RefCursor, ParameterDirection.Output);
192+
cmd.Parameters.Add("id", OracleDbType.Varchar2, ParameterDirection.Input).Value = id;
193+
cmd.Parameters.Add("lines_cursor", OracleDbType.RefCursor, ParameterDirection.Output);
158194

159-
// https://stackoverflow.com/questions/2226769/bad-performance-with-oracledatareader
160-
cmd.InitialLOBFetchSize = -1;
195+
// https://stackoverflow.com/questions/2226769/bad-performance-with-oracledatareader
196+
cmd.InitialLOBFetchSize = -1;
161197

162-
var reader = cmd.ExecuteReader();
198+
var reader = cmd.ExecuteReader();
163199

164-
while (reader.Read())
165-
{
166-
var line = reader.GetString(0);
167-
sb.Append(line);
168-
}
200+
while (reader.Read())
201+
{
202+
var line = reader.GetString(0);
203+
sb.Append(line);
204+
}
169205

170-
reader.Close();
206+
reader.Close();
171207

172-
runningCommands.Remove(cmd);
173-
cmd.Dispose();
208+
runningCommands.Remove(cmd);
209+
cmd.Dispose();
174210

175-
return sb.ToString();
211+
return sb.ToString();
212+
}
213+
catch (Exception e)
214+
{
215+
using (EventLog eventLog = new EventLog("Application"))
216+
{
217+
eventLog.Source = "Application";
218+
eventLog.WriteEntry($"{e.Message}\r\n{e.StackTrace}", EventLogEntryType.Error);
219+
}
220+
}
176221
}
177222

178223
protected string ConvertToUtVarchar2List(List<string> elements)

0 commit comments

Comments
 (0)