Skip to content

Commit d9050b9

Browse files
fix: Change string.Format style to string interpolation (#225)
1 parent 2213f6f commit d9050b9

File tree

10 files changed

+49
-50
lines changed

10 files changed

+49
-50
lines changed

OptimizelySDK/Bucketing/Bucketer.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,7 @@ private string FindBucket(string bucketingId, string userId, string parentId, IE
8686
string bucketingKey = bucketingId + parentId;
8787
int bucketingNumber = GenerateBucketValue(bucketingKey);
8888

89-
Logger.Log(LogLevel.DEBUG, string.Format("Assigned bucket [{0}] to user [{1}] with bucketing ID [{2}].",
90-
bucketingNumber, userId, bucketingId));
89+
Logger.Log(LogLevel.DEBUG, $"Assigned bucket [{bucketingNumber}] to user [{userId}] with bucketing ID [{bucketingId}].");
9190

9291
foreach (var ta in trafficAllocations)
9392
if (bucketingNumber < ta.EndOfRange)
@@ -122,36 +121,34 @@ public virtual Variation Bucket(ProjectConfig config, Experiment experiment, str
122121
string userExperimentId = FindBucket(bucketingId, userId, group.Id, group.TrafficAllocation);
123122
if (string.IsNullOrEmpty(userExperimentId))
124123
{
125-
message = string.Format("User [{0}] is in no experiment.", userId);
124+
message = $"User [{userId}] is in no experiment.";
126125
Logger.Log(LogLevel.INFO, message);
127126
return new Variation();
128127
}
129128

130129
if (userExperimentId != experiment.Id)
131130
{
132-
message = string.Format("User [{0}] is not in experiment [{1}] of group [{2}].",
133-
userId, experiment.Key, experiment.GroupId);
131+
message = $"User [{userId}] is not in experiment [{experiment.Key}] of group [{experiment.GroupId}].";
134132
Logger.Log(LogLevel.INFO, message);
135133
return new Variation();
136134
}
137135

138-
message = string.Format("User [{0}] is in experiment [{1}] of group [{2}].",
139-
userId, experiment.Key, experiment.GroupId);
136+
message = $"User [{userId}] is in experiment [{experiment.Key}] of group [{experiment.GroupId}].";
140137
Logger.Log(LogLevel.INFO, message);
141138
}
142139

143140
// Bucket user if not in whitelist and in group (if any).
144141
string variationId = FindBucket(bucketingId, userId, experiment.Id, experiment.TrafficAllocation);
145142
if (string.IsNullOrEmpty(variationId))
146143
{
147-
Logger.Log(LogLevel.INFO, string.Format("User [{0}] is in no variation.", userId));
144+
Logger.Log(LogLevel.INFO, $"User [{userId}] is in no variation.");
148145
return new Variation();
149146

150147
}
151148

152149
// success!
153150
variation = config.GetVariationFromId(experiment.Key, variationId);
154-
message = string.Format("User [{0}] is in variation [{1}] of experiment [{2}].", userId, variation.Key, experiment.Key);
151+
message = $"User [{userId}] is in variation [{variation.Key}] of experiment [{experiment.Key}].";
155152
Logger.Log(LogLevel.INFO, message);
156153
return variation;
157154
}

OptimizelySDK/Bucketing/DecisionService.cs

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,12 @@ public class DecisionService
4646
/// set by the user by calling setForcedVariation (it is not the same as the
4747
/// whitelisting forcedVariations data structure in the Experiments class).
4848
/// </summary>
49+
#if NET35
4950
private Dictionary<string, Dictionary<string, string>> ForcedVariationMap;
51+
#else
52+
private System.Collections.Concurrent.ConcurrentDictionary<string, Dictionary<string, string>> ForcedVariationMap;
53+
#endif
54+
5055

5156
/// <summary>
5257
/// Initialize a decision service for the Optimizely client.
@@ -61,7 +66,11 @@ public DecisionService(Bucketer bucketer, IErrorHandler errorHandler, UserProfil
6166
ErrorHandler = errorHandler;
6267
UserProfileService = userProfileService;
6368
Logger = logger;
69+
#if NET35
6470
ForcedVariationMap = new Dictionary<string, Dictionary<string, string>>();
71+
#else
72+
ForcedVariationMap = new System.Collections.Concurrent.ConcurrentDictionary<string, Dictionary<string, string>>();
73+
#endif
6574
}
6675

6776
/// <summary>
@@ -132,7 +141,7 @@ public virtual Variation GetVariation(Experiment experiment, string userId, Proj
132141

133142
return variation;
134143
}
135-
Logger.Log(LogLevel.INFO, string.Format("User \"{0}\" does not meet conditions to be in experiment \"{1}\".", userId, experiment.Key));
144+
Logger.Log(LogLevel.INFO, $"User \"{userId}\" does not meet conditions to be in experiment \"{experiment.Key}\".");
136145

137146
return null;
138147
}
@@ -148,7 +157,7 @@ public Variation GetForcedVariation(string experimentKey, string userId, Project
148157
{
149158
if (ForcedVariationMap.ContainsKey(userId) == false)
150159
{
151-
Logger.Log(LogLevel.DEBUG, string.Format(@"User ""{0}"" is not in the forced variation map.", userId));
160+
Logger.Log(LogLevel.DEBUG, $@"User ""{userId}"" is not in the forced variation map.");
152161
return null;
153162
}
154163

@@ -162,15 +171,15 @@ public Variation GetForcedVariation(string experimentKey, string userId, Project
162171

163172
if (experimentToVariationMap.ContainsKey(experimentId) == false)
164173
{
165-
Logger.Log(LogLevel.DEBUG, string.Format(@"No experiment ""{0}"" mapped to user ""{1}"" in the forced variation map.", experimentKey, userId));
174+
Logger.Log(LogLevel.DEBUG, $@"No experiment ""{experimentKey}"" mapped to user ""{userId}"" in the forced variation map.");
166175
return null;
167176
}
168177

169178
string variationId = experimentToVariationMap[experimentId];
170179

171180
if (string.IsNullOrEmpty(variationId))
172181
{
173-
Logger.Log(LogLevel.DEBUG, string.Format(@"No variation mapped to experiment ""{0}"" in the forced variation map.", experimentKey));
182+
Logger.Log(LogLevel.DEBUG, $@"No variation mapped to experiment ""{experimentKey}"" in the forced variation map.");
174183
return null;
175184
}
176185

@@ -180,7 +189,7 @@ public Variation GetForcedVariation(string experimentKey, string userId, Project
180189
if (string.IsNullOrEmpty(variationKey))
181190
return null;
182191

183-
Logger.Log(LogLevel.DEBUG, string.Format(@"Variation ""{0}"" is mapped to experiment ""{1}"" and user ""{2}"" in the forced variation map", variationKey, experimentKey, userId));
192+
Logger.Log(LogLevel.DEBUG, $@"Variation ""{variationKey}"" is mapped to experiment ""{experimentKey}"" and user ""{userId}"" in the forced variation map");
184193

185194
Variation variation = config.GetVariationFromKey(experimentKey, variationKey);
186195

@@ -216,7 +225,7 @@ public bool SetForcedVariation(string experimentKey, string userId, string varia
216225
if (ForcedVariationMap.ContainsKey(userId) && ForcedVariationMap[userId].ContainsKey(experimentId))
217226
ForcedVariationMap[userId].Remove(experimentId);
218227

219-
Logger.Log(LogLevel.DEBUG, string.Format(@"Variation mapped to experiment ""{0}"" has been removed for user ""{1}"".", experimentKey, userId));
228+
Logger.Log(LogLevel.DEBUG, $@"Variation mapped to experiment ""{experimentKey}"" has been removed for user ""{userId}"".");
220229
return true;
221230
}
222231

@@ -233,7 +242,7 @@ public bool SetForcedVariation(string experimentKey, string userId, string varia
233242
// Add/Replace Experiment to Variation ID map.
234243
ForcedVariationMap[userId][experimentId] = variationId;
235244

236-
Logger.Log(LogLevel.DEBUG, string.Format(@"Set variation ""{0}"" for experiment ""{1}"" and user ""{2}"" in the forced variation map.", variationId, experimentId, userId));
245+
Logger.Log(LogLevel.DEBUG, $@"Set variation ""{variationId}"" for experiment ""{experimentId}"" and user ""{userId}"" in the forced variation map.");
237246
return true;
238247
}
239248

@@ -259,9 +268,9 @@ public Variation GetWhitelistedVariation(Experiment experiment, string userId)
259268
: null;
260269

261270
if (forcedVariation != null)
262-
Logger.Log(LogLevel.INFO, string.Format("User \"{0}\" is forced in variation \"{1}\".", userId, forcedVariationKey));
271+
Logger.Log(LogLevel.INFO, $"User \"{userId}\" is forced in variation \"{forcedVariationKey}\".");
263272
else
264-
Logger.Log(LogLevel.ERROR, string.Format("Variation \"{0}\" is not in the datafile. Not activating user \"{1}\".", forcedVariationKey, userId));
273+
Logger.Log(LogLevel.ERROR, $"Variation \"{forcedVariationKey}\" is not in the datafile. Not activating user \"{userId}\".");
265274

266275
return forcedVariation;
267276
}
@@ -284,7 +293,7 @@ public Variation GetStoredVariation(Experiment experiment, UserProfile userProfi
284293

285294
if (decision == null)
286295
{
287-
Logger.Log(LogLevel.INFO, string.Format("No previously activated variation of experiment \"{0}\" for user \"{1}\" found in user profile.", experimentKey, userProfile.UserId));
296+
Logger.Log(LogLevel.INFO, $"No previously activated variation of experiment \"{experimentKey}\" for user \"{userProfile.UserId}\" found in user profile.");
288297
return null;
289298
}
290299

@@ -298,13 +307,11 @@ public Variation GetStoredVariation(Experiment experiment, UserProfile userProfi
298307

299308
if (savedVariation == null)
300309
{
301-
Logger.Log(LogLevel.INFO, string.Format("User \"{0}\" was previously bucketed into variation with ID \"{1}\" for experiment \"{2}\", but no matching variation was found for that user. We will re-bucket the user.",
302-
userProfile.UserId, variationId, experimentId));
310+
Logger.Log(LogLevel.INFO, $"User \"{userProfile.UserId}\" was previously bucketed into variation with ID \"{variationId}\" for experiment \"{experimentId}\", but no matching variation was found for that user. We will re-bucket the user.");
303311
return null;
304312
}
305313

306-
Logger.Log(LogLevel.INFO, string.Format("Returning previously activated variation \"{0}\" of experiment \"{1}\" for user \"{2}\" from user profile.",
307-
savedVariation.Key, experimentKey, userProfile.UserId));
314+
Logger.Log(LogLevel.INFO, $"Returning previously activated variation \"{savedVariation.Key}\" of experiment \"{experimentKey}\" for user \"{userProfile.UserId}\" from user profile.");
308315
return savedVariation;
309316
}
310317
catch (Exception)
@@ -341,13 +348,11 @@ public void SaveVariation(Experiment experiment, Variation variation, UserProfil
341348
try
342349
{
343350
UserProfileService.Save(userProfile.ToMap());
344-
Logger.Log(LogLevel.INFO, string.Format("Saved variation \"{0}\" of experiment \"{1}\" for user \"{2}\".",
345-
variation.Id, experiment.Id, userProfile.UserId));
351+
Logger.Log(LogLevel.INFO, $"Saved variation \"{variation.Id}\" of experiment \"{experiment.Id}\" for user \"{userProfile.UserId}\".");
346352
}
347353
catch (Exception exception)
348354
{
349-
Logger.Log(LogLevel.ERROR, string.Format("Failed to save variation \"{0}\" of experiment \"{1}\" for user \"{2}\".",
350-
variation.Id, experiment.Id, userProfile.UserId));
355+
Logger.Log(LogLevel.ERROR, $"Failed to save variation \"{variation.Id}\" of experiment \"{experiment.Id}\" for user \"{userProfile.UserId}\".");
351356
ErrorHandler.HandleError(new Exceptions.OptimizelyRuntimeException(exception.Message));
352357
}
353358
}

OptimizelySDK/Config/DatafileProjectConfig.cs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ private static DatafileProjectConfig GetConfig(string configData)
339339
var config = JsonConvert.DeserializeObject<DatafileProjectConfig>(configData);
340340

341341
if (SupportedVersions.TrueForAll((supportedVersion) => !(((int)supportedVersion).ToString() == config.Version)))
342-
throw new ConfigParseException(string.Format(@"This version of the C# SDK does not support the given datafile version: {0}", config.Version));
342+
throw new ConfigParseException($@"This version of the C# SDK does not support the given datafile version: {config.Version}");
343343

344344
return config;
345345
}
@@ -356,7 +356,7 @@ public Group GetGroup(string groupId)
356356
if (_GroupIdMap.ContainsKey(groupId))
357357
return _GroupIdMap[groupId];
358358

359-
string message = string.Format(@"Group ID ""{0}"" is not in datafile.", groupId);
359+
string message = $@"Group ID ""{groupId}"" is not in datafile.";
360360
Logger.Log(LogLevel.ERROR, message);
361361
ErrorHandler.HandleError(new Exceptions.InvalidGroupException("Provided group is not in datafile."));
362362
return new Group();
@@ -372,7 +372,7 @@ public Experiment GetExperimentFromKey(string experimentKey)
372372
if (_ExperimentKeyMap.ContainsKey(experimentKey))
373373
return _ExperimentKeyMap[experimentKey];
374374

375-
string message = string.Format(@"Experiment key ""{0}"" is not in datafile.", experimentKey);
375+
string message = $@"Experiment key ""{experimentKey}"" is not in datafile.";
376376
Logger.Log(LogLevel.ERROR, message);
377377
ErrorHandler.HandleError(new Exceptions.InvalidExperimentException("Provided experiment is not in datafile."));
378378
return new Experiment();
@@ -388,7 +388,7 @@ public Experiment GetExperimentFromId(string experimentId)
388388
if (_ExperimentIdMap.ContainsKey(experimentId))
389389
return _ExperimentIdMap[experimentId];
390390

391-
string message = string.Format(@"Experiment ID ""{0}"" is not in datafile.", experimentId);
391+
string message = $@"Experiment ID ""{experimentId}"" is not in datafile.";
392392
Logger.Log(LogLevel.ERROR, message);
393393
ErrorHandler.HandleError(new Exceptions.InvalidExperimentException("Provided experiment is not in datafile."));
394394
return new Experiment();
@@ -404,7 +404,7 @@ public Entity.Event GetEvent(string eventKey)
404404
if (_EventKeyMap.ContainsKey(eventKey))
405405
return _EventKeyMap[eventKey];
406406

407-
string message = string.Format(@"Event key ""{0}"" is not in datafile.", eventKey);
407+
string message = $@"Event key ""{eventKey}"" is not in datafile.";
408408
Logger.Log(LogLevel.ERROR, message);
409409
ErrorHandler.HandleError(new Exceptions.InvalidEventException("Provided event is not in datafile."));
410410
return new Entity.Event();
@@ -420,7 +420,7 @@ public Audience GetAudience(string audienceId)
420420
if (_AudienceIdMap.ContainsKey(audienceId))
421421
return _AudienceIdMap[audienceId];
422422

423-
string message = string.Format(@"Audience ID ""{0}"" is not in datafile.", audienceId);
423+
string message = $@"Audience ID ""{audienceId}"" is not in datafile.";
424424
Logger.Log(LogLevel.ERROR, message);
425425
ErrorHandler.HandleError(new Exceptions.InvalidAudienceException("Provided audience is not in datafile."));
426426
return new Audience();
@@ -436,7 +436,7 @@ public Attribute GetAttribute(string attributeKey)
436436
if (_AttributeKeyMap.ContainsKey(attributeKey))
437437
return _AttributeKeyMap[attributeKey];
438438

439-
string message = string.Format(@"Attribute key ""{0}"" is not in datafile.", attributeKey);
439+
string message = $@"Attribute key ""{attributeKey}"" is not in datafile.";
440440
Logger.Log(LogLevel.ERROR, message);
441441
ErrorHandler.HandleError(new Exceptions.InvalidAttributeException("Provided attribute is not in datafile."));
442442
return new Attribute();
@@ -455,8 +455,7 @@ public Variation GetVariationFromKey(string experimentKey, string variationKey)
455455
_VariationKeyMap[experimentKey].ContainsKey(variationKey))
456456
return _VariationKeyMap[experimentKey][variationKey];
457457

458-
string message = string.Format(@"No variation key ""{0}"" defined in datafile for experiment ""{1}"".",
459-
variationKey, experimentKey);
458+
string message = $@"No variation key ""{variationKey}"" defined in datafile for experiment ""{experimentKey}"".";
460459
Logger.Log(LogLevel.ERROR, message);
461460
ErrorHandler.HandleError(new Exceptions.InvalidVariationException("Provided variation is not in datafile."));
462461
return new Variation();
@@ -475,8 +474,7 @@ public Variation GetVariationFromId(string experimentKey, string variationId)
475474
_VariationIdMap[experimentKey].ContainsKey(variationId))
476475
return _VariationIdMap[experimentKey][variationId];
477476

478-
string message = string.Format(@"No variation ID ""{0}"" defined in datafile for experiment ""{1}"".",
479-
variationId, experimentKey);
477+
string message = $@"No variation ID ""{variationId}"" defined in datafile for experiment ""{experimentKey}"".";
480478
Logger.Log(LogLevel.ERROR, message);
481479
ErrorHandler.HandleError(new Exceptions.InvalidVariationException("Provided variation is not in datafile."));
482480
return new Variation();
@@ -492,7 +490,7 @@ public FeatureFlag GetFeatureFlagFromKey(string featureKey)
492490
if (_FeatureKeyMap.ContainsKey(featureKey))
493491
return _FeatureKeyMap[featureKey];
494492

495-
string message = string.Format(@"Feature key ""{0}"" is not in datafile.", featureKey);
493+
string message = $@"Feature key ""{featureKey}"" is not in datafile.";
496494
Logger.Log(LogLevel.ERROR, message);
497495
ErrorHandler.HandleError(new Exceptions.InvalidFeatureException("Provided feature is not in datafile."));
498496
return new FeatureFlag();
@@ -508,7 +506,7 @@ public Rollout GetRolloutFromId(string rolloutId)
508506
if (_RolloutIdMap.ContainsKey(rolloutId))
509507
return _RolloutIdMap[rolloutId];
510508

511-
string message = string.Format(@"Rollout ID ""{0}"" is not in datafile.", rolloutId);
509+
string message = $@"Rollout ID ""{rolloutId}"" is not in datafile.";
512510
Logger.Log(LogLevel.ERROR, message);
513511
ErrorHandler.HandleError(new Exceptions.InvalidRolloutException("Provided rollout is not in datafile."));
514512
return new Rollout();

OptimizelySDK/Entity/EventTags.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public EventTags FilterNullValues(ILogger logger) {
2626
if (pair.Value != null) {
2727
answer[pair.Key] = pair.Value;
2828
} else {
29-
logger.Log(LogLevel.ERROR, string.Format("[EventTags] Null value for key {0} removed and will not be sent to results.", pair.Key));
29+
logger.Log(LogLevel.ERROR, $"[EventTags] Null value for key {pair.Key} removed and will not be sent to results.");
3030
}
3131
}
3232
return answer;

OptimizelySDK/Event/Dispatcher/HttpClientEventDispatcher45.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ private async void DispatchEventAsync(LogEvent logEvent)
6464
}
6565
catch (Exception ex)
6666
{
67-
Logger.Log(LogLevel.ERROR, string.Format("Error Dispatching Event: {0}", ex.GetAllMessages()));
67+
Logger.Log(LogLevel.ERROR, $"Error Dispatching Event: {ex.GetAllMessages()}");
6868
}
6969
}
7070

OptimizelySDK/Logger/DefaultLogger.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class DefaultLogger : ILogger
2424
{
2525
public void Log(LogLevel level, string message)
2626
{
27-
string line = string.Format("[{0}] : {1}", level, message);
27+
string line = $"[{level}] : {message}";
2828
Debug.WriteLine(line);
2929
}
3030
}

OptimizelySDK/Optimizely.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,7 @@ private void SendImpressionEvent(Experiment experiment, Variation variation, str
690690
{
691691
var userEvent = UserEventFactory.CreateImpressionEvent(config, experiment, variation.Id, userId, userAttributes);
692692
EventProcessor.Process(userEvent);
693-
Logger.Log(LogLevel.INFO, string.Format("Activating user {0} in experiment {1}.", userId, experiment.Key));
693+
Logger.Log(LogLevel.INFO, $"Activating user {userId} in experiment {experiment.Key}.");
694694

695695
// Kept For backwards compatibility.
696696
// This notification is deprecated and the new DecisionNotifications

0 commit comments

Comments
 (0)