Skip to content

Instantly share code, notes, and snippets.

@scionwest
Last active November 2, 2016 22:33
Show Gist options
  • Save scionwest/fedfaf9fe7f0f8eb6108b209fcf22a11 to your computer and use it in GitHub Desktop.
Save scionwest/fedfaf9fe7f0f8eb6108b209fcf22a11 to your computer and use it in GitHub Desktop.
{
"EnableProfiling": "false",
"IsProfilerWhitelisting": "true",
"ProfilerProvider": "ADOT.Core.Profiling.SingletonProfilerProvider",
"IncludedTypes": [
"ADOT.Core.Profiling.StopwatchFactory",
"ADOT.Core.Profiling.SingletoneProfilerProvider"
],
"IncludedNamespaces": [
"ADOT.DataAccess",
"ADOT.Logging"
]
}
public class ProfilerFactoryConverter : CustomCreationConverter<IAppProfilerFactory>
{
public override IAppProfilerFactory Create(Type objectType)
{
// We don't use this - ReadJson below is used instead.
// Inheriting from CustomCreationConverter requires that we expose this method though.
throw new NotSupportedException();
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
JToken jsonObject = JObject.ReadFrom(reader);
string providerTypeToActivate = jsonObject.Value<string>();
Type typeToActivate = Type.GetType(providerTypeToActivate);
object instancedObject = Activator.CreateInstance(typeToActivate);
return instancedObject as IAppProfilerFactory;
}
}
public partial class ProfileRuntimeSettings
{
public static ProfileRuntimeSettings Load()
{
string json = File.ReadAllText("AppProfiling.json");
var settings = JsonConvert.DeserializeObject<ProfileRuntimeSettings>(json);
return settings;
}
[JsonConverter(typeof(ProfilerFactoryConverter))]
public IAppProfilerFactory ProfilerProvider { get; set; }
public bool EnableProfiling { get; set; }
public bool IsProfilerWhitelisting { get; set; } = true;
public string[] IncludedTypes { get; set; }
public string[] IncludedNamespaces { get; set; }
}
[TestMethod]
public void ProfilerSettings_IsLoadedFromJson()
{
// Arrange
AppProfiler.LoadSettings();
// Act
IAppProfiler profiler = AppProfiler.GlobalSettings.ProfilerProvider.Create();
// Act
Assert.IsNotNull(profiler);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment