Skip to content

Instantly share code, notes, and snippets.

@taylonr
Created December 15, 2011 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taylonr/1481208 to your computer and use it in GitHub Desktop.
Save taylonr/1481208 to your computer and use it in GitHub Desktop.
Profiler Interface
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];
}
}
}
public interface IProfiler
{
bool Enabled { get; set; }
void Update(PetaPocoSqlInfo info);
}
[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"; }
}
}
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