Line data Source code
1 : #region Copyright
2 : // // -----------------------------------------------------------------------
3 : // // <copyright company="Chinchilla Software Limited">
4 : // // Copyright Chinchilla Software Limited. All rights reserved.
5 : // // </copyright>
6 : // // -----------------------------------------------------------------------
7 : #endregion
8 :
9 : using System;
10 : using System.Net;
11 : using Cqrs.Configuration;
12 : using Cqrs.Hosts;
13 : using Microsoft.ApplicationInsights;
14 : using Microsoft.ApplicationInsights.Extensibility;
15 :
16 : namespace Cqrs.Azure.ConfigurationManager
17 : {
18 : /// <summary>
19 : /// Configure and start command and event handlers in a host with telemetry
20 : /// </summary>
21 : public abstract class TelemetryCoreHost<TAuthenticationToken> : CoreHost<TAuthenticationToken>
22 1 : {
23 : /// <summary>
24 : /// Gets or sets the <see cref="IConfigurationManager"/>.
25 : /// </summary>
26 : protected static readonly IConfigurationManager _configurationManager = new CloudConfigurationManager();
27 :
28 : /// <summary>
29 : /// The <see cref="IConfigurationManager"/> that can be use before the <see cref="DependencyResolver.Current"/> is set.
30 : /// </summary>
31 : protected override IConfigurationManager ConfigurationManager
32 : {
33 : get { return _configurationManager; }
34 : }
35 :
36 : /// <summary>
37 : /// Gets or sets the <see cref="TelemetryClient"/>.
38 : /// </summary>
39 : public TelemetryClient TelemetryClient { get; private set; }
40 :
41 : #region Overrides of CoreHost<TAuthenticationToken>
42 :
43 : /// <summary>
44 : /// When overridden, allows you to configure Telemetry
45 : /// </summary>
46 1 : protected override void ConfigureTelemetry()
47 : {
48 : TelemetryConfiguration.Active.InstrumentationKey = ConfigurationManager.GetSetting("Cqrs.Hosts.ApplicationInsightsInstrumentationKey");
49 : bool enabledApplicationInsightsDeveloperMode;
50 : if (!bool.TryParse(ConfigurationManager.GetSetting("Cqrs.Hosts.EnabledApplicationInsightsDeveloperMode"), out enabledApplicationInsightsDeveloperMode))
51 : enabledApplicationInsightsDeveloperMode = false;
52 : TelemetryConfiguration.Active.TelemetryChannel.DeveloperMode = enabledApplicationInsightsDeveloperMode;
53 :
54 : TelemetryClient = new TelemetryClient {InstrumentationKey = TelemetryConfiguration.Active.InstrumentationKey};
55 : TelemetryClient.TrackEvent(string.Format("{0}/Instantiating", TelemetryName));
56 : TelemetryClient.Flush();
57 : }
58 :
59 : /// <summary>
60 : /// Calls <see cref="Prepare"/>, <paramref name="handlerRegistation"/> and then <see cref="Start"/>
61 : /// </summary>
62 1 : public override void Run(Action handlerRegistation = null)
63 : {
64 : base.Run(handlerRegistation);
65 : TelemetryClient.TrackEvent(string.Format("{0}/Ran", TelemetryName));
66 : TelemetryClient.Flush();
67 : }
68 :
69 : /// <summary>
70 : /// Sets the <see cref="System.Net.ServicePointManager.SecurityProtocol"/> to
71 : /// <see cref="System.Net.SecurityProtocolType.Tls12"/> | <see cref="System.Net.SecurityProtocolType.Tls11"/> | <see cref="System.Net.SecurityProtocolType.Tls"/>.
72 : /// </summary>
73 1 : protected override void PrepareSecurityProtocol()
74 : {
75 : ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
76 : }
77 :
78 : /// <summary>
79 : /// Prepare the host before registering handlers and starting the host.
80 : /// </summary>
81 1 : protected override void Prepare()
82 : {
83 : base.Prepare();
84 :
85 : TelemetryClient.TrackEvent(string.Format("{0}/Prepared", TelemetryName));
86 : }
87 :
88 : /// <summary>
89 : /// Start the host post preparing and registering handlers.
90 : /// </summary>
91 1 : protected override void Start()
92 : {
93 : base.Start();
94 :
95 : TelemetryClient.TrackEvent(string.Format("{0}/Started", TelemetryName));
96 : }
97 :
98 : #endregion
99 : }
100 : }
|