我正在使用 Mvc-Mini-Profiler(多么棒的产品!)。使用我的普通网络浏览器,一切似乎都可以正常工作,但是一旦我使用我自己的 http 客户端(基本的 http 1.1 不支持 cookie),http 标头中X-MiniProfiler-Id的数量就会增加。这种情况发生得非常快,并且在很短的时间内变得非常多(11kB 及以上的数据)。
缺少 cookie 能否使 Mvc-Mini-Profiler 以这种方式工作,或者我的实现可能有什么问题?
我正在使用 Mvc-Mini-Profiler(多么棒的产品!)。使用我的普通网络浏览器,一切似乎都可以正常工作,但是一旦我使用我自己的 http 客户端(基本的 http 1.1 不支持 cookie),http 标头中X-MiniProfiler-Id的数量就会增加。这种情况发生得非常快,并且在很短的时间内变得非常多(11kB 及以上的数据)。
缺少 cookie 能否使 Mvc-Mini-Profiler 以这种方式工作,或者我的实现可能有什么问题?
我认为这是设计使然。虽然我们可以稍微改进一下实现。
X-MiniProfiler-Ids
需要“消费”,它们仅在启用分析时显示。它以这种方式工作的原因是您可以分析POST
和重定向。
我们可能应该在那里设置一些明确的上限(比如 20 左右) - 请为此发布错误。
但是,由于您从未真正计划为您的 HTTP 客户端使用任何分析块,因此如果用户代理是您的 HTTP 客户端,我建议您放弃分析。
您可以通过在之前添加条件来做到这一点:
// don't run if UserAgent is "my http client"
if(notMyUserAgent)
MvcMiniProfiler.MiniProfiler.Start();
另一种选择是覆盖 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;
}
}