-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestService.cs
84 lines (73 loc) · 3.12 KB
/
TestService.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
namespace WindowsServiceAIFailure
{
using System;
using System.Diagnostics;
using System.Net.Http;
using System.ServiceProcess;
using System.Threading.Tasks;
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
/// <inheritdoc />
/// <summary>Service to test AI failure.</summary>
public partial class TestService : ServiceBase
{
/// <summary>Telemetry client.</summary>
private readonly TelemetryClient telemetryClient;
/// <summary>Service logger.</summary>
private readonly ILogger<TestService> logger;
/// <inheritdoc />
/// <summary>Initializes a new instance of the <see cref="TestService" /> class.</summary>
public TestService()
{
InitializeComponent();
var key = ConfigUtility.GetKeyValue();
var servicesCollection = new ServiceCollection();
servicesCollection.AddApplicationInsightsTelemetryWorkerService(key);
var serviceProvider = servicesCollection.BuildServiceProvider();
////this.telemetryClient = new TelemetryClient(new TelemetryConfiguration(key)) { InstrumentationKey = key };
this.telemetryClient = serviceProvider.GetRequiredService<TelemetryClient>();
this.logger = serviceProvider.GetRequiredService<ILogger<TestService>>();
}
/// <summary>Perform action causing AI failure.</summary>
public void PerformFailure()
{
Task.Run(
async () =>
{
var httpClient = new HttpClient();
while (true)
{
try
{
using (telemetryClient.StartOperation<RequestTelemetry>("operation"))
{
logger.LogWarning("A sample warning message.");
logger.LogInformation("Calling bing.com");
var responseMessage = await httpClient.GetAsync("https://bing.com").ConfigureAwait(false);
logger.LogInformation("Calling bing completed with status:" + responseMessage.StatusCode);
telemetryClient.TrackEvent("Bing call event completed");
}
}
catch (Exception e)
{
Debug.WriteLine(e);
this.Stop();
}
await Task.Delay(1000).ConfigureAwait(false);
}
});
}
/// <inheritdoc />
protected override void OnStart(string[] args)
{
this.PerformFailure();
}
/// <inheritdoc />
protected override void OnStop()
{
this.telemetryClient.Flush();
}
}
}