もう 1 つのオプションは、SqlServerStorage クラスをオーバーライドし、UserHasViewed フィールドをデフォルトで true に設定することです。これにより、X-MiniProfiler-Id
文字列が最小限に抑えられます。
public class MvcMiniProfilerStorage : SqlServerStorage
{
public MvcMiniProfilerStorage(string connectionString) : base(connectionString)
{
}
/// <summary>
/// Stores to dbo.MiniProfilers under its ;
/// stores all child Timings and SqlTimings to their respective tables.
/// </summary>
public override void Save(MiniProfiler profiler)
{
const string sql =
@"insert into MiniProfilers
(Id,
Name,
Started,
MachineName,
[User],
Level,
RootTimingId,
DurationMilliseconds,
DurationMillisecondsInSql,
HasSqlTimings,
HasDuplicateSqlTimings,
HasTrivialTimings,
HasAllTrivialTimings,
TrivialDurationThresholdMilliseconds,
HasUserViewed)
select @Id,
@Name,
@Started,
@MachineName,
@User,
@Level,
@RootTimingId,
@DurationMilliseconds,
@DurationMillisecondsInSql,
@HasSqlTimings,
@HasDuplicateSqlTimings,
@HasTrivialTimings,
@HasAllTrivialTimings,
@TrivialDurationThresholdMilliseconds,
@HasUserViewed
where not exists (select 1 from MiniProfilers where Id = @Id)";
// this syntax works on both mssql and sqlite
using (DbConnection conn = GetOpenConnection())
{
int insertCount = conn.Execute(sql,
new
{
profiler.Id,
Name = Truncate(profiler.Name, 200),
profiler.Started,
MachineName = Truncate(profiler.MachineName, 100),
User = Truncate(profiler.User, 100),
profiler.Level,
RootTimingId = profiler.Root.Id,
profiler.DurationMilliseconds,
profiler.DurationMillisecondsInSql,
profiler.HasSqlTimings,
profiler.HasDuplicateSqlTimings,
profiler.HasTrivialTimings,
profiler.HasAllTrivialTimings,
profiler.TrivialDurationThresholdMilliseconds,
// BUG: Too many X-MiniProfiler-Id headers cause
// Firefox to stop all requests
//
// This hack marks all entries as read so that
// they do not end up part of that header.
HasUserViewed = true
});
if (insertCount > 0)
{
SaveTiming(conn, profiler, profiler.Root);
}
}
}
private static string Truncate(string s, int maxLength)
{
return s != null && s.Length >
maxLength ? s.Substring(0, maxLength) : s;
}
}