我的db.SaveChangesAsync()在一个try块中,如下所示,但是在查看我的日志时,我看到了一个DbUpdateConcurrencyException日志,但是DbUpdateConcurrencyException是从DbUpdateException派生的。
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateException)
{
}但我注意到以下日志:
Microsoft.EntityFrameworkCore.Update| An exception occurred in the
database while saving changes for context type 'xxx.MyContext'.
Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException: Database
operation expected to affect 1 row(s) but actually affected 0 row(s). Data
may have been modified or deleted since entities were loaded.我不是通过捕获DbUpdateConcurrencyException来捕获DbUpdateException,还是完全错过了异常或nlog的工作方式?
编辑:
我的nlog配置:
<targets>
<target xsi:type="File" name="all-log" layout="${longdate}|${logger}|${uppercase:${level}} ... />
<target xsi:type="File" name="own-log" ... />
</targets>
<rules>
<logger name="*" minlevel="Error" writeTo="all-log" />
<logger name="Microsoft.*" minlevel="Trace" finale="true" />
<logger name="*" minlevel="Trace" writeTo="own-log" />
</rules>发布于 2018-10-16 18:26:47
当您使用ASP.NET核心日志时,微软的所有组件都可以写入ASP.NET核心日志。NLog (NLog.Web.AspNetCore)集成也将获得这些日志,因此您可以将它们写入文件等。
因此,可能是EF组件为日志记录编写了该异常。您可以在配置中使用${stracktrace}或${logger}进行检查。
https://stackoverflow.com/questions/52833747
复制相似问题