在生产中使用带有EF 6应用程序的C#ASP.NET MVC大约3个月之后,我们注意到对数据库的一次调用花费的时间比以前长得多。它开始的时间不到一秒钟。没有逐渐增加,它跳到15秒。
我已启用了对EF上下文的登录。这是代码。
public async Task<int> SetToProcessing(string[] documentIds, string userName)
{
if (documentIds.Count() == 0)
return 0;
var assigned = 0;
using (var db = new InvoiceDeliveryContext())
{
db.Database.Log = message => logger.Debug("EF6-" + message);
foreach (var id in ConvertToLong(documentIds))
{
if (id == 0)
continue;
var doc = db.Documents.Find(id);
if (doc.Status == DocumentStates.Processing)
{
logger.Debug($"Document Id {id} not being set to processed. Already set to Processing.");
continue;
}
assigned++;
doc.AssignedTo = userName;
doc.Status = DocumentStates.Processing;
doc.History.Add(new History
{
Audit = "Set back to Processing.",
CreatedBy = userName,
Document = doc,
});
logger.Debug($"Assigned Document with ID={id} for Processing to {userName}.");
}
try
{
await db.SaveChangesAsync();
}
catch (Exception e)
{
logger.Error(e, "An exception occurred trying to set documents to processing.");
}
}
return assigned;
}
以下是生成的日志。请注意,在打开连接后,会发生约15秒的延迟。
[5] 2017-07-27 06:47:32.4742 EF6-Opened connection at 7/27/2017 6:47:32 AM -05:00
[5] 2017-07-27 06:47:47.0677 EF6-SELECT TOP (2)
[Extent1].[DocumentId] AS [DocumentId],
[Extent1].[AssignedTo] AS [AssignedTo],
[Extent1].[ApprovedBy] AS [ApprovedBy],
[Extent1].[AttachmentId] AS [AttachmentId],
[Extent1].[FileLocation] AS [FileLocation],
[Extent1].[FileSize] AS [FileSize],
[Extent1].[Name] AS [Name],
[Extent1].[Pages] AS [Pages],
[Extent1].[Part] AS [Part],
[Extent1].[Parts] AS [Parts],
[Extent1].[IsMerged] AS [IsMerged],
[Extent1].[ReceivedOn] AS [ReceivedOn],
[Extent1].[Status] AS [Status],
FROM [InvoiceDelivery].[Documents] AS [Extent1]
WHERE [Extent1].[DocumentId] = @p0
[5] 2017-07-27 06:47:47.0980 EF6--- p0: '7149' (Type = Int64)
[5] 2017-07-27 06:47:47.0980 EF6--- Executing at 7/27/2017 6:47:47 AM -05:00
[5] 2017-07-27 06:47:47.1401 EF6--- Completed in 28 ms with result: SqlDataReader
我已多次捕获上述过程的日志,并且它们都显示大约15秒的延迟。其他查询不会遇到此问题。知道为什么我会看到这种延迟吗?
我无法使用新的控制台应用程序重现延迟
private static async Task GoAsync()
{
var repo = new InvoiceRepository();
await repo.SetToProcessing(new[] { "7149" }, "JD Montgomery");
}
答案 0 :(得分:0)
记录日志。除了几个服务(包括UI层日志)之外,我不会将所有内容记录到同一个数据库表中。使用DEBUG级别,此问题正在发生。一旦我降低级别或停止登录数据库,问题就会消失。
我猜测Log表上有锁。