3
3
4
4
using System ;
5
5
using System . Collections . Generic ;
6
- using System . Diagnostics ;
7
6
using System . IO ;
8
7
using System . IO . Compression ;
9
8
using System . Linq ;
13
12
using System . Threading . Tasks ;
14
13
using Microsoft . ApplicationInsights ;
15
14
using Microsoft . ApplicationInsights . DataContracts ;
15
+ using Microsoft . Extensions . Logging ;
16
16
using Newtonsoft . Json ;
17
17
using Newtonsoft . Json . Linq ;
18
18
using Xunit ;
@@ -24,10 +24,12 @@ public class FunctionAppFixture : IDisposable
24
24
private bool _disposed ;
25
25
private KuduClient _kuduClient ;
26
26
private readonly TrackedAssert _assert ;
27
+ private readonly ILogger _logger ;
27
28
28
29
public FunctionAppFixture ( )
29
30
{
30
- Trace . Listeners . Add ( new ConsoleTraceListener ( ) ) ;
31
+ ILoggerFactory loggerFactory = new LoggerFactory ( ) . AddConsole ( ) ;
32
+ _logger = loggerFactory . CreateLogger < FunctionAppFixture > ( ) ;
31
33
32
34
_assert = new TrackedAssert ( Telemetry ) ;
33
35
@@ -49,7 +51,7 @@ public FunctionAppFixture()
49
51
private async Task Initialize ( )
50
52
{
51
53
Telemetry . TrackEvent ( new EventTelemetry ( "EnvironmentInitialization" ) ) ;
52
- Trace . WriteLine ( "Initializing environment..." ) ;
54
+ _logger . LogInformation ( "Initializing environment..." ) ;
53
55
54
56
_kuduClient = new KuduClient ( $ "https://{ Settings . SiteName } .scm.azurewebsites.net", Settings . SitePublishingUser , Settings . SitePublishingPassword ) ;
55
57
FunctionAppMasterKey = await _kuduClient . GetFunctionsMasterKey ( ) ;
@@ -65,10 +67,10 @@ private async Task Initialize()
65
67
// after all initialization is done, do a final restart for good measure
66
68
await RestartSite ( ) ;
67
69
68
- Trace . WriteLine ( "Environment initialized" ) ;
70
+ _logger . LogInformation ( "Environment initialized" ) ;
69
71
Telemetry . TrackEvent ( new EventTelemetry ( "EnvironmentInitialized" ) ) ;
70
72
71
- Trace . WriteLine ( "Test run starting..." ) ;
73
+ _logger . LogInformation ( "Test run starting..." ) ;
72
74
}
73
75
74
76
private async Task RedeployTestSite ( )
@@ -88,7 +90,7 @@ private async Task RedeployTestSite()
88
90
89
91
private async Task InitializeFunctionKeys ( )
90
92
{
91
- Trace . WriteLine ( "Initializing keys..." ) ;
93
+ _logger . LogInformation ( "Initializing keys..." ) ;
92
94
93
95
using ( var keysUpdate = Telemetry . StartOperation < RequestTelemetry > ( "KeysUpdate" ) )
94
96
{
@@ -111,10 +113,13 @@ private async Task InitializeFunctionKeys()
111
113
}
112
114
}
113
115
}
114
-
116
+
115
117
private async Task WaitForSite ( )
116
118
{
117
- Trace . WriteLine ( "Waiting for site..." ) ;
119
+ _logger . LogInformation ( "Waiting for site..." ) ;
120
+
121
+ TimeSpan delay = TimeSpan . FromSeconds ( 3 ) ;
122
+ int attemptsCount = 5 ;
118
123
119
124
using ( var client = new HttpClient ( ) )
120
125
{
@@ -124,35 +129,42 @@ private async Task WaitForSite()
124
129
{
125
130
if ( attempts ++ > 0 )
126
131
{
127
- await Task . Delay ( TimeSpan . FromSeconds ( 3 ) ) ;
132
+ await Task . Delay ( delay ) ;
128
133
}
129
134
130
135
// Workaround for https://github.com/Azure/azure-functions-host/issues/2397 as the base URL
131
136
// doesn't currently start the host.
132
137
// var result = await client.GetAsync($"{Settings.SiteBaseAddress}");
133
138
var functions = await _kuduClient . GetFunctions ( ) ;
134
- var result = await client . GetAsync ( $ "{ Settings . SiteBaseAddress } /admin/functions/{ functions . First ( ) . Name } /status?code={ FunctionAppMasterKey } ") ;
139
+ var result = await client . GetAsync ( $ "{ Settings . SiteBaseAddress } /admin/functions/{ functions . First ( ) . Name } /status?code={ FunctionAppMasterKey } ") ;
135
140
statusCode = result . StatusCode ;
141
+ if ( statusCode == HttpStatusCode . OK )
142
+ {
143
+ _logger . LogInformation ( "Site is up and running!" ) ;
144
+ return ;
145
+ }
136
146
}
137
- while ( statusCode != HttpStatusCode . OK && attempts < 5 ) ;
147
+ while ( attempts < attemptsCount ) ;
148
+
138
149
}
139
150
140
- Trace . WriteLine ( "Site is up and running!" ) ;
151
+ throw new InvalidOperationException ( "Wait for site timeout: {delay.TotalSeconds * 5} seconds." ) ;
152
+
141
153
}
142
154
143
155
private async Task AddSettings ( )
144
156
{
145
- Trace . WriteLine ( "Updating app settings..." ) ;
157
+ _logger . LogInformation ( "Updating app settings..." ) ;
146
158
Telemetry . TrackEvent ( "SettingsUpdate" ) ;
147
159
148
160
await AddAppSetting ( Constants . ServiceBusKey , Environment . GetEnvironmentVariable ( Constants . ServiceBusKey ) ) ;
149
161
150
- Trace . WriteLine ( "App settings updated" ) ;
162
+ _logger . LogInformation ( "App settings updated" ) ;
151
163
}
152
164
153
165
private async Task UpdateSiteContents ( )
154
166
{
155
- Trace . WriteLine ( "Updating site contents..." ) ;
167
+ _logger . LogInformation ( "Updating site contents..." ) ;
156
168
Telemetry . TrackEvent ( "ContentUpdate" ) ;
157
169
158
170
await _kuduClient . DeleteDirectory ( "site/wwwroot" , true ) ;
@@ -161,12 +173,12 @@ private async Task UpdateSiteContents()
161
173
162
174
await _kuduClient . UploadZip ( "site" , filePath ) ;
163
175
164
- Trace . WriteLine ( "Site contents updated" ) ;
176
+ _logger . LogInformation ( "Site contents updated" ) ;
165
177
}
166
178
167
179
private async Task UpdateRuntime ( )
168
180
{
169
- Trace . WriteLine ( $ "Updating runtime from: { Settings . RuntimeExtensionPackageUrl } ") ;
181
+ _logger . LogInformation ( $ "Updating runtime from: { Settings . RuntimeExtensionPackageUrl } ") ;
170
182
Telemetry . TrackEvent ( "RuntimeUpdate" , new Dictionary < string , string > { { "runtimeSource" , Settings . RuntimeExtensionPackageUrl } } ) ;
171
183
172
184
string extensionFilePath = Path . ChangeExtension ( Path . GetTempFileName ( ) , "zip" ) ;
@@ -192,34 +204,34 @@ private async Task UpdateRuntime()
192
204
await _kuduClient . DeleteDirectory ( "SiteExtensions" , true ) ;
193
205
await _kuduClient . UploadZip ( "/" , extensionFilePath ) ;
194
206
195
- Trace . WriteLine ( $ "Updated to function runtime version: { Settings . RuntimeVersion } ") ;
207
+ _logger . LogInformation ( $ "Updated to function runtime version: { Settings . RuntimeVersion } ") ;
196
208
}
197
209
198
210
public async Task RestartSite ( )
199
211
{
200
- Trace . WriteLine ( "Restarting site..." ) ;
212
+ _logger . LogInformation ( "Restarting site..." ) ;
201
213
202
214
await IssueSiteCommand ( $ "/subscriptions/{ Settings . SiteSubscriptionId } /resourceGroups/{ Settings . SiteResourceGroup } /providers/Microsoft.Web/sites/{ Settings . SiteName } /restart?api-version=2015-08-01&softRestart=true&synchronous=true") ;
203
215
204
- Trace . WriteLine ( "Site restarted" ) ;
216
+ _logger . LogInformation ( "Site restarted" ) ;
205
217
}
206
218
207
219
public async Task StopSite ( )
208
220
{
209
- Trace . WriteLine ( "Stopping site..." ) ;
221
+ _logger . LogInformation ( "Stopping site..." ) ;
210
222
211
223
await IssueSiteCommand ( $ "/subscriptions/{ Settings . SiteSubscriptionId } /resourceGroups/{ Settings . SiteResourceGroup } /providers/Microsoft.Web/sites/{ Settings . SiteName } /stop?api-version=2015-08-01") ;
212
224
213
- Trace . WriteLine ( "Site stopped" ) ;
225
+ _logger . LogInformation ( "Site stopped" ) ;
214
226
}
215
227
216
228
public async Task StartSite ( )
217
229
{
218
- Trace . WriteLine ( "Starting site..." ) ;
230
+ _logger . LogInformation ( "Starting site..." ) ;
219
231
220
232
await IssueSiteCommand ( $ "/subscriptions/{ Settings . SiteSubscriptionId } /resourceGroups/{ Settings . SiteResourceGroup } /providers/Microsoft.Web/sites/{ Settings . SiteName } /start?api-version=2015-08-01") ;
221
233
222
- Trace . WriteLine ( "Site started" ) ;
234
+ _logger . LogInformation ( "Site started" ) ;
223
235
}
224
236
225
237
public async Task AddAppSetting ( string name , string value )
0 commit comments