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 : #if NETCOREAPP3_0
24 : /// <summary>
25 : /// Gets or sets the <see cref="IConfigurationManager"/>. This must be set manually as dependency injection may not be ready in-time.
26 : /// </summary>
27 : protected static readonly IConfigurationManager _configurationManager = null;
28 : #endif
29 : #if NET472
30 : /// <summary>
31 : /// Gets or sets the <see cref="IConfigurationManager"/>.
32 : /// </summary>
33 : protected static readonly IConfigurationManager _configurationManager = new CloudConfigurationManager();
34 : #endif
35 :
36 : /// <summary>
37 : /// The <see cref="IConfigurationManager"/> that can be use before the <see cref="DependencyResolver.Current"/> is set.
38 : /// </summary>
39 : protected override IConfigurationManager ConfigurationManager
40 : {
41 : get { return _configurationManager; }
42 : }
43 :
44 : /// <summary>
45 : /// Gets or sets the <see cref="TelemetryClient"/>.
46 : /// </summary>
47 : public TelemetryClient TelemetryClient { get; private set; }
48 :
49 : #if NETCOREAPP3_0
50 : /// <summary>
51 : /// The delegate used internally to get the current <see cref="TelemetryConfiguration"/>.
52 : /// <see cref="TelemetryConfiguration.CreateDefault"/> will be used if this is not set.
53 : /// </summary>
54 : public static Func<TelemetryConfiguration> GetTelemetryConfigurationDelegate { get; set; }
55 : #endif
56 :
57 : #region Overrides of CoreHost<TAuthenticationToken>
58 :
59 : /// <summary>
60 : /// When overridden, allows you to configure Telemetry
61 : /// </summary>
62 1 : protected override void ConfigureTelemetry()
63 : {
64 : #if NETCOREAPP3_0
65 : TelemetryConfiguration config = GetTelemetryConfigurationDelegate() ?? TelemetryConfiguration.CreateDefault();
66 : config.InstrumentationKey = ConfigurationManager.GetSetting("Cqrs.Hosts.ApplicationInsightsInstrumentationKey");
67 : #endif
68 : #if NET472
69 : TelemetryConfiguration.Active.InstrumentationKey = ConfigurationManager.GetSetting("Cqrs.Hosts.ApplicationInsightsInstrumentationKey");
70 : #endif
71 : bool enabledApplicationInsightsDeveloperMode;
72 : if (!bool.TryParse(ConfigurationManager.GetSetting("Cqrs.Hosts.EnabledApplicationInsightsDeveloperMode"), out enabledApplicationInsightsDeveloperMode))
73 : enabledApplicationInsightsDeveloperMode = false;
74 : #if NETCOREAPP3_0
75 : config.TelemetryChannel.DeveloperMode = enabledApplicationInsightsDeveloperMode;
76 : TelemetryClient = new TelemetryClient (config);
77 : #endif
78 : #if NET472
79 : TelemetryConfiguration.Active.TelemetryChannel.DeveloperMode = enabledApplicationInsightsDeveloperMode;
80 : TelemetryClient = new TelemetryClient {InstrumentationKey = ConfigurationManager.GetSetting("Cqrs.Hosts.ApplicationInsightsInstrumentationKey") };
81 : #endif
82 :
83 : TelemetryClient.TrackEvent(string.Format("{0}/Instantiating", TelemetryName));
84 : TelemetryClient.Flush();
85 : }
86 :
87 : /// <summary>
88 : /// Calls <see cref="Prepare"/>, <paramref name="handlerRegistation"/> and then <see cref="Start"/>
89 : /// </summary>
90 1 : public override void Run(Action handlerRegistation = null)
91 : {
92 : base.Run(handlerRegistation);
93 : TelemetryClient.TrackEvent(string.Format("{0}/Ran", TelemetryName));
94 : TelemetryClient.Flush();
95 : }
96 :
97 : /// <summary>
98 : /// Sets the <see cref="System.Net.ServicePointManager.SecurityProtocol"/> to
99 : /// <see cref="System.Net.SecurityProtocolType.Tls12"/> | <see cref="System.Net.SecurityProtocolType.Tls11"/> | <see cref="System.Net.SecurityProtocolType.Tls"/>.
100 : /// </summary>
101 1 : protected override void PrepareSecurityProtocol()
102 : {
103 : ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
104 : }
105 :
106 : /// <summary>
107 : /// Prepare the host before registering handlers and starting the host.
108 : /// </summary>
109 1 : protected override void Prepare()
110 : {
111 : base.Prepare();
112 :
113 : TelemetryClient.TrackEvent(string.Format("{0}/Prepared", TelemetryName));
114 : }
115 :
116 : /// <summary>
117 : /// Start the host post preparing and registering handlers.
118 : /// </summary>
119 1 : protected override void Start()
120 : {
121 : base.Start();
122 :
123 : TelemetryClient.TrackEvent(string.Format("{0}/Started", TelemetryName));
124 : }
125 :
126 : #endregion
127 : }
128 : }
|