33
44using System ;
55using System . Collections . Generic ;
6- using System . Diagnostics ;
76using System . IO ;
87using System . IO . Compression ;
98using System . Linq ;
1312using System . Threading . Tasks ;
1413using Microsoft . ApplicationInsights ;
1514using Microsoft . ApplicationInsights . DataContracts ;
15+ using Microsoft . Extensions . Logging ;
1616using Newtonsoft . Json ;
1717using Newtonsoft . Json . Linq ;
1818using Xunit ;
@@ -24,10 +24,12 @@ public class FunctionAppFixture : IDisposable
2424 private bool _disposed ;
2525 private KuduClient _kuduClient ;
2626 private readonly TrackedAssert _assert ;
27+ private readonly ILogger _logger ;
2728
2829 public FunctionAppFixture ( )
2930 {
30- Trace . Listeners . Add ( new ConsoleTraceListener ( ) ) ;
31+ ILoggerFactory loggerFactory = new LoggerFactory ( ) . AddConsole ( ) ;
32+ _logger = loggerFactory . CreateLogger < FunctionAppFixture > ( ) ;
3133
3234 _assert = new TrackedAssert ( Telemetry ) ;
3335
@@ -49,7 +51,7 @@ public FunctionAppFixture()
4951 private async Task Initialize ( )
5052 {
5153 Telemetry . TrackEvent ( new EventTelemetry ( "EnvironmentInitialization" ) ) ;
52- Trace . WriteLine ( "Initializing environment..." ) ;
54+ _logger . LogInformation ( "Initializing environment..." ) ;
5355
5456 _kuduClient = new KuduClient ( $ "https://{ Settings . SiteName } .scm.azurewebsites.net", Settings . SitePublishingUser , Settings . SitePublishingPassword ) ;
5557 FunctionAppMasterKey = await _kuduClient . GetFunctionsMasterKey ( ) ;
@@ -65,10 +67,10 @@ private async Task Initialize()
6567 // after all initialization is done, do a final restart for good measure
6668 await RestartSite ( ) ;
6769
68- Trace . WriteLine ( "Environment initialized" ) ;
70+ _logger . LogInformation ( "Environment initialized" ) ;
6971 Telemetry . TrackEvent ( new EventTelemetry ( "EnvironmentInitialized" ) ) ;
7072
71- Trace . WriteLine ( "Test run starting..." ) ;
73+ _logger . LogInformation ( "Test run starting..." ) ;
7274 }
7375
7476 private async Task RedeployTestSite ( )
@@ -88,7 +90,7 @@ private async Task RedeployTestSite()
8890
8991 private async Task InitializeFunctionKeys ( )
9092 {
91- Trace . WriteLine ( "Initializing keys..." ) ;
93+ _logger . LogInformation ( "Initializing keys..." ) ;
9294
9395 using ( var keysUpdate = Telemetry . StartOperation < RequestTelemetry > ( "KeysUpdate" ) )
9496 {
@@ -111,10 +113,13 @@ private async Task InitializeFunctionKeys()
111113 }
112114 }
113115 }
114-
116+
115117 private async Task WaitForSite ( )
116118 {
117- Trace . WriteLine ( "Waiting for site..." ) ;
119+ _logger . LogInformation ( "Waiting for site..." ) ;
120+
121+ TimeSpan delay = TimeSpan . FromSeconds ( 3 ) ;
122+ int attemptsCount = 5 ;
118123
119124 using ( var client = new HttpClient ( ) )
120125 {
@@ -124,35 +129,42 @@ private async Task WaitForSite()
124129 {
125130 if ( attempts ++ > 0 )
126131 {
127- await Task . Delay ( TimeSpan . FromSeconds ( 3 ) ) ;
132+ await Task . Delay ( delay ) ;
128133 }
129134
130135 // Workaround for https://github.com/Azure/azure-functions-host/issues/2397 as the base URL
131136 // doesn't currently start the host.
132137 // var result = await client.GetAsync($"{Settings.SiteBaseAddress}");
133138 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 } ") ;
135140 statusCode = result . StatusCode ;
141+ if ( statusCode == HttpStatusCode . OK )
142+ {
143+ _logger . LogInformation ( "Site is up and running!" ) ;
144+ return ;
145+ }
136146 }
137- while ( statusCode != HttpStatusCode . OK && attempts < 5 ) ;
147+ while ( attempts < attemptsCount ) ;
148+
138149 }
139150
140- Trace . WriteLine ( "Site is up and running!" ) ;
151+ throw new InvalidOperationException ( "Wait for site timeout: {delay.TotalSeconds * 5} seconds." ) ;
152+
141153 }
142154
143155 private async Task AddSettings ( )
144156 {
145- Trace . WriteLine ( "Updating app settings..." ) ;
157+ _logger . LogInformation ( "Updating app settings..." ) ;
146158 Telemetry . TrackEvent ( "SettingsUpdate" ) ;
147159
148160 await AddAppSetting ( Constants . ServiceBusKey , Environment . GetEnvironmentVariable ( Constants . ServiceBusKey ) ) ;
149161
150- Trace . WriteLine ( "App settings updated" ) ;
162+ _logger . LogInformation ( "App settings updated" ) ;
151163 }
152164
153165 private async Task UpdateSiteContents ( )
154166 {
155- Trace . WriteLine ( "Updating site contents..." ) ;
167+ _logger . LogInformation ( "Updating site contents..." ) ;
156168 Telemetry . TrackEvent ( "ContentUpdate" ) ;
157169
158170 await _kuduClient . DeleteDirectory ( "site/wwwroot" , true ) ;
@@ -161,12 +173,12 @@ private async Task UpdateSiteContents()
161173
162174 await _kuduClient . UploadZip ( "site" , filePath ) ;
163175
164- Trace . WriteLine ( "Site contents updated" ) ;
176+ _logger . LogInformation ( "Site contents updated" ) ;
165177 }
166178
167179 private async Task UpdateRuntime ( )
168180 {
169- Trace . WriteLine ( $ "Updating runtime from: { Settings . RuntimeExtensionPackageUrl } ") ;
181+ _logger . LogInformation ( $ "Updating runtime from: { Settings . RuntimeExtensionPackageUrl } ") ;
170182 Telemetry . TrackEvent ( "RuntimeUpdate" , new Dictionary < string , string > { { "runtimeSource" , Settings . RuntimeExtensionPackageUrl } } ) ;
171183
172184 string extensionFilePath = Path . ChangeExtension ( Path . GetTempFileName ( ) , "zip" ) ;
@@ -192,34 +204,34 @@ private async Task UpdateRuntime()
192204 await _kuduClient . DeleteDirectory ( "SiteExtensions" , true ) ;
193205 await _kuduClient . UploadZip ( "/" , extensionFilePath ) ;
194206
195- Trace . WriteLine ( $ "Updated to function runtime version: { Settings . RuntimeVersion } ") ;
207+ _logger . LogInformation ( $ "Updated to function runtime version: { Settings . RuntimeVersion } ") ;
196208 }
197209
198210 public async Task RestartSite ( )
199211 {
200- Trace . WriteLine ( "Restarting site..." ) ;
212+ _logger . LogInformation ( "Restarting site..." ) ;
201213
202214 await IssueSiteCommand ( $ "/subscriptions/{ Settings . SiteSubscriptionId } /resourceGroups/{ Settings . SiteResourceGroup } /providers/Microsoft.Web/sites/{ Settings . SiteName } /restart?api-version=2015-08-01&softRestart=true&synchronous=true") ;
203215
204- Trace . WriteLine ( "Site restarted" ) ;
216+ _logger . LogInformation ( "Site restarted" ) ;
205217 }
206218
207219 public async Task StopSite ( )
208220 {
209- Trace . WriteLine ( "Stopping site..." ) ;
221+ _logger . LogInformation ( "Stopping site..." ) ;
210222
211223 await IssueSiteCommand ( $ "/subscriptions/{ Settings . SiteSubscriptionId } /resourceGroups/{ Settings . SiteResourceGroup } /providers/Microsoft.Web/sites/{ Settings . SiteName } /stop?api-version=2015-08-01") ;
212224
213- Trace . WriteLine ( "Site stopped" ) ;
225+ _logger . LogInformation ( "Site stopped" ) ;
214226 }
215227
216228 public async Task StartSite ( )
217229 {
218- Trace . WriteLine ( "Starting site..." ) ;
230+ _logger . LogInformation ( "Starting site..." ) ;
219231
220232 await IssueSiteCommand ( $ "/subscriptions/{ Settings . SiteSubscriptionId } /resourceGroups/{ Settings . SiteResourceGroup } /providers/Microsoft.Web/sites/{ Settings . SiteName } /start?api-version=2015-08-01") ;
221233
222- Trace . WriteLine ( "Site started" ) ;
234+ _logger . LogInformation ( "Site started" ) ;
223235 }
224236
225237 public async Task AddAppSetting ( string name , string value )
0 commit comments