@@ -54,17 +54,13 @@ public class PlayFabAllocator : IMatchmakerAllocator
5454 /// </summary>
5555 const string PlayFabTitleId = "TITLE_ID" ;
5656
57+ readonly ILogger _logger ;
5758 readonly IGameApiClient _gameApiClient ;
58- readonly Action < string , Exception ? > LogDebug ;
59- readonly Action < string , Exception ? > LogError ;
6059
6160 public PlayFabAllocator ( IGameApiClient gameApiClient , ILogger < PlayFabAllocator > logger )
6261 {
6362 _gameApiClient = gameApiClient ;
64- LogError = ( message , exception ) =>
65- LoggerMessage . Define < string > ( LogLevel . Error , new EventId ( ) , "{ErrorMessage}" ) ( logger , message , exception ) ;
66- LogDebug = ( message , exception ) =>
67- LoggerMessage . Define < string > ( LogLevel . Debug , new EventId ( ) , "{DebugMessage}" ) ( logger , message , exception ) ;
63+ _logger = logger ;
6864 }
6965
7066 [ CloudCodeFunction ( nameof ( Allocate ) ) ]
@@ -76,8 +72,7 @@ public async Task<AllocateResponse> Allocate(IExecutionContext context, Allocate
7672 }
7773 catch ( Exception e )
7874 {
79- const string error = $ "An error occured when retrieving secrets for key '{ DeveloperSecretKey } '.";
80- LogError ( error , e ) ;
75+ _logger . LogError ( e , $ "An error occured when retrieving secrets for key '{ DeveloperSecretKey } '.") ;
8176 return new AllocateResponse ( AllocateStatus . Error ) { Message = AllocationUserFriendlyError } ;
8277 }
8378
@@ -88,7 +83,7 @@ public async Task<AllocateResponse> Allocate(IExecutionContext context, Allocate
8883 catch ( Exception e )
8984 {
9085 const string error = $ "An error occured when retrieving secret for key '{ PlayFabTitleId } '.";
91- LogError ( error , e ) ;
86+ _logger . LogError ( e , error ) ;
9287 return new AllocateResponse ( AllocateStatus . Error ) { Message = AllocationUserFriendlyError } ;
9388 }
9489
@@ -102,7 +97,7 @@ public async Task<AllocateResponse> Allocate(IExecutionContext context, Allocate
10297 catch ( Exception e )
10398 {
10499 const string error = $ "An error occured when retrieving secret for key '{ PlayFabBuildId } '.";
105- LogError ( error , e ) ;
100+ _logger . LogError ( e , error ) ;
106101 return new AllocateResponse ( AllocateStatus . Error ) { Message = AllocationUserFriendlyError } ;
107102 }
108103
@@ -123,7 +118,7 @@ public async Task<AllocateResponse> Allocate(IExecutionContext context, Allocate
123118 catch ( Exception e )
124119 {
125120 const string error = "An error occured when retrieving the entity token." ;
126- LogError ( error , e ) ;
121+ _logger . LogError ( e , error ) ;
127122 return new AllocateResponse ( AllocateStatus . Error ) { Message = AllocationUserFriendlyError } ;
128123 }
129124
@@ -143,7 +138,7 @@ public async Task<AllocateResponse> Allocate(IExecutionContext context, Allocate
143138 {
144139 const string error =
145140 "An error occured when retrieving the region in matchmaking properties. The region field must be present, non-null and non-empty." ;
146- LogError ( error , null ) ;
141+ _logger . LogError ( error ) ;
147142 return new AllocateResponse ( AllocateStatus . Error ) { Message = AllocationUserFriendlyError } ;
148143 }
149144
@@ -154,7 +149,7 @@ public async Task<AllocateResponse> Allocate(IExecutionContext context, Allocate
154149 SessionId = request . MatchId
155150 } ;
156151
157- LogDebug ( $ "Requesting an allocation for session id: { multiplayerServerRequest . SessionId } ", null ) ;
152+ _logger . LogDebug ( $ "Requesting an allocation for session id: { multiplayerServerRequest . SessionId } ") ;
158153
159154 var allocationResult = await multiplayerInstanceApi . RequestMultiplayerServerAsync ( multiplayerServerRequest ) ;
160155
@@ -177,7 +172,7 @@ public async Task<AllocateResponse> Allocate(IExecutionContext context, Allocate
177172 catch ( Exception e )
178173 {
179174 const string error = "An error occured when allocating." ;
180- LogError ( error , e ) ;
175+ _logger . LogError ( e , error ) ;
181176 return new AllocateResponse ( AllocateStatus . Error ) { Message = AllocationUserFriendlyError } ;
182177 }
183178 }
@@ -192,7 +187,7 @@ public async Task<PollResponse> Poll(IExecutionContext context, PollRequest requ
192187 catch ( Exception e )
193188 {
194189 const string error = $ "An error occured when retrieving secret for key '{ DeveloperSecretKey } '.";
195- LogError ( error , e ) ;
190+ _logger . LogError ( e , error ) ;
196191 return new PollResponse ( PollStatus . Error ) { Message = PollUserFriendlyError } ;
197192 }
198193
@@ -203,7 +198,7 @@ public async Task<PollResponse> Poll(IExecutionContext context, PollRequest requ
203198 catch ( Exception e )
204199 {
205200 const string error = $ "An error occured when retrieving secret for key '{ PlayFabTitleId } '.";
206- LogError ( error , e ) ;
201+ _logger . LogError ( e , error ) ;
207202 return new PollResponse ( PollStatus . Error ) { Message = PollUserFriendlyError } ;
208203 }
209204
@@ -226,7 +221,7 @@ public async Task<PollResponse> Poll(IExecutionContext context, PollRequest requ
226221 catch ( Exception e )
227222 {
228223 const string error = "An error occured when retrieving the entity token." ;
229- LogError ( error , e ) ;
224+ _logger . LogError ( e , error ) ;
230225 return new PollResponse ( PollStatus . Error ) { Message = PollUserFriendlyError } ;
231226 }
232227
@@ -246,7 +241,7 @@ public async Task<PollResponse> Poll(IExecutionContext context, PollRequest requ
246241 SessionId = request . AllocationData [ "sessionId" ] . ToString ( )
247242 } ;
248243
249- LogDebug ( $ "Requesting details for session id: { multiplayerServerDetailsRequest . SessionId } ", null ) ;
244+ _logger . LogDebug ( $ "Requesting details for session id: { multiplayerServerDetailsRequest . SessionId } ") ;
250245
251246 var detailsResult =
252247 await multiplayerInstanceApi . GetMultiplayerServerDetailsAsync ( multiplayerServerDetailsRequest ) ;
@@ -269,16 +264,13 @@ public async Task<PollResponse> Poll(IExecutionContext context, PollRequest requ
269264 detailsResult . Result . Ports [ 0 ] . Num )
270265 } ;
271266 default :
272- var error =
273- $ "An error occured when polling the server status. Server state: { detailsResult . Result . State } ";
274- LogError ( error , null ) ;
267+ _logger . LogError ( $ "An error occured when polling the server status. Server state: { detailsResult . Result . State } ") ;
275268 return new PollResponse ( PollStatus . Error ) { Message = PollUserFriendlyError } ;
276269 }
277270 }
278271 catch ( Exception e )
279272 {
280- const string error = "An error occured when polling the server status." ;
281- LogError ( error , e ) ;
273+ _logger . LogError ( e , "An error occured when polling the server status." ) ;
282274 return new PollResponse ( PollStatus . Error ) { Message = PollUserFriendlyError } ;
283275 }
284276 }
@@ -288,10 +280,11 @@ bool IsValid(PlayFabResult<GetEntityTokenResponse> entityTokenRequestResult)
288280 switch ( entityTokenRequestResult )
289281 {
290282 case null :
291- LogError ( $ "An error occured when calling { nameof ( PlayFabAuthenticationAPI . GetEntityTokenAsync ) } . The result is null.", null ) ;
283+ _logger . LogError (
284+ $ "An error occured when calling { nameof ( PlayFabAuthenticationAPI . GetEntityTokenAsync ) } . The result is null.") ;
292285 return false ;
293286 case { Error : not null } :
294- LogError ( $ "An error occured when calling { nameof ( PlayFabAuthenticationAPI . GetEntityTokenAsync ) } . The result is null. Error: { SerializeToJson ( entityTokenRequestResult . Error ) } .", null ) ;
287+ _logger . LogError ( $ "An error occured when calling { nameof ( PlayFabAuthenticationAPI . GetEntityTokenAsync ) } . The result is null. Error: { SerializeToJson ( entityTokenRequestResult . Error ) } .") ;
295288 return false ;
296289 case
297290 {
@@ -307,7 +300,7 @@ bool IsValid(PlayFabResult<GetEntityTokenResponse> entityTokenRequestResult)
307300 } :
308301 return true ;
309302 default :
310- LogError ( $ "An error occured when calling { nameof ( PlayFabAuthenticationAPI . GetEntityTokenAsync ) } . The result is null. Token is malformed. Token: { SerializeToJson ( entityTokenRequestResult . Result ) } .", null ) ;
303+ _logger . LogError ( $ "An error occured when calling { nameof ( PlayFabAuthenticationAPI . GetEntityTokenAsync ) } . The result is null. Token is malformed. Token: { SerializeToJson ( entityTokenRequestResult . Result ) } .") ;
311304 return false ;
312305 }
313306 }
@@ -317,10 +310,10 @@ bool IsValid(PlayFabResult<RequestMultiplayerServerResponse> requestMultiplayerS
317310 switch ( requestMultiplayerServerResult )
318311 {
319312 case null :
320- LogError ( $ "An error occured when calling { nameof ( PlayFabMultiplayerInstanceAPI . RequestMultiplayerServerAsync ) } . The result is null.", null ) ;
313+ _logger . LogError ( $ "An error occured when calling { nameof ( PlayFabMultiplayerInstanceAPI . RequestMultiplayerServerAsync ) } . The result is null.") ;
321314 return false ;
322315 case { Error : not null } :
323- LogError ( $ "An error occured when calling { nameof ( PlayFabMultiplayerInstanceAPI . RequestMultiplayerServerAsync ) } . Error: { SerializeToJson ( requestMultiplayerServerResult . Error ) } .", null ) ;
316+ _logger . LogError ( $ "An error occured when calling { nameof ( PlayFabMultiplayerInstanceAPI . RequestMultiplayerServerAsync ) } . Error: { SerializeToJson ( requestMultiplayerServerResult . Error ) } .") ;
324317 return false ;
325318 default :
326319 return true ;
@@ -332,17 +325,17 @@ bool IsValid(PlayFabResult<GetMultiplayerServerDetailsResponse> getMultiplayerSe
332325 switch ( getMultiplayerServerDetailsResult )
333326 {
334327 case null :
335- LogError ( $ "An error occured when calling { nameof ( PlayFabMultiplayerInstanceAPI . GetMultiplayerServerDetailsAsync ) } . The result is null.", null ) ;
328+ _logger . LogError ( $ "An error occured when calling { nameof ( PlayFabMultiplayerInstanceAPI . GetMultiplayerServerDetailsAsync ) } . The result is null.") ;
336329 return false ;
337330 case { Error : not null } :
338- LogError ( $ "An error occured when calling { nameof ( PlayFabMultiplayerInstanceAPI . GetMultiplayerServerDetailsAsync ) } . Error: { SerializeToJson ( getMultiplayerServerDetailsResult . Error ) } .", null ) ;
331+ _logger . LogError ( $ "An error occured when calling { nameof ( PlayFabMultiplayerInstanceAPI . GetMultiplayerServerDetailsAsync ) } . Error: { SerializeToJson ( getMultiplayerServerDetailsResult . Error ) } .") ;
339332 return false ;
340333 case { Result : { State . Length : > 0 , IPV4Address . Length : > 0 , Ports . Count : > 0 } } :
341334 {
342335 return true ;
343336 }
344337 default :
345- LogError ( $ "An error occured when calling { nameof ( PlayFabMultiplayerInstanceAPI . GetMultiplayerServerDetailsAsync ) } . Details are malformed. Details: { SerializeToJson ( getMultiplayerServerDetailsResult . Result ) } .", null ) ;
338+ _logger . LogError ( $ "An error occured when calling { nameof ( PlayFabMultiplayerInstanceAPI . GetMultiplayerServerDetailsAsync ) } . Details are malformed. Details: { SerializeToJson ( getMultiplayerServerDetailsResult . Result ) } .") ;
346339 return false ;
347340 }
348341 }
0 commit comments