Created
December 15, 2011 14:09
-
-
Save taylonr/1481208 to your computer and use it in GitHub Desktop.
Profiler Interface
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DataProfiler : IProfiler | |
{ | |
public DataProfiler() | |
{ | |
Enabled = true; | |
} | |
public bool Enabled { get; set; } | |
public void Update(PetaPocoSqlInfo info) | |
{ | |
CurrentRequestInfo.Add(info); | |
} | |
public const string PetaKey = "__petaPocoSqlInfo"; | |
public static List<PetaPocoSqlInfo> CurrentRequestInfo | |
{ | |
get | |
{ | |
if (HttpContext.Current.Items[PetaKey] == null) | |
HttpContext.Current.Items[PetaKey] = new List<PetaPocoSqlInfo>(); | |
return (List<PetaPocoSqlInfo>)HttpContext.Current.Items[PetaKey]; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IProfiler | |
{ | |
bool Enabled { get; set; } | |
void Update(PetaPocoSqlInfo info); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[GlimpsePlugin] | |
public class PetaPocoGlimpsePlugin : IGlimpsePlugin | |
{ | |
public object GetData(HttpContextBase context) | |
{ | |
if (context.Items[DataProfiler.PetaKey] == null) | |
return new List<object[]> { new[] { "Log" }, new[] { "No database requests or database logging not switched on (Compilation debug='true' or ForceLogging='true' on | |
PetaPoco.DatabaseWithLogging)", "warn" } }; | |
var sqls = new List<object[]> {new[] {"#", "Time(ms)", "Sql", "Parameters"}}; | |
var i = 1; | |
foreach (var item in DataProfiler.CurrentRequestInfo) | |
{ | |
var parameterHeadings = new List<object[]> {new[] {"Name", "Type", "Value"}}; | |
parameterHeadings.AddRange(item.Parameters.Cast<IDataParameter>().Select(pm => new[] { pm.ParameterName, pm.Value.GetType().Name, pm.Value })); | |
object parameters = parameterHeadings.Count > 1 ? parameterHeadings : null; | |
var formattedTime = item.Time.ToString("0.#0"); | |
var time = item.Time > 100 ? "*"+formattedTime+"*" : formattedTime; | |
sqls.Add(new[] { i++, time, item.Sql, parameters }); | |
} | |
return sqls; | |
} | |
public void SetupInit() | |
{ | |
} | |
public string Name | |
{ | |
get { return "PetaPoco"; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class PetaPocoSqlInfo | |
{ | |
public double Time { get; set; } | |
public string FormattedSql { get; set; } | |
public string Sql { get; set; } | |
public IDataParameterCollection Parameters { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment