捕获 DbUpdateException 不会捕获 DbUpdateConcurrencyException
catching DbUpdateException won't catch DbUpdateConcurrencyException
我的 db.SaveChangesAsync()
位于 try catch 块中,如下所示,但检查我的日志时,我看到 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.
我不是通过捕获 DbUpdateException
来捕获 DbUpdateConcurrencyException
还是我完全错过了异常的工作原理或 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>
当您使用 ASP.NET Core Logging 时,Microsoft 的所有组件都可以写入 ASP.NET Core Logging。 NLog (NLog.Web.AspNetCore) 集成也将获取这些日志,因此您可以将它们写入文件等。
所以 EF 组件可能将该异常写入了日志记录。您可以通过在配置
中使用 ${stracktrace}
或 ${logger}
来检查
我的 db.SaveChangesAsync()
位于 try catch 块中,如下所示,但检查我的日志时,我看到 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.
我不是通过捕获 DbUpdateException
来捕获 DbUpdateConcurrencyException
还是我完全错过了异常的工作原理或 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>
当您使用 ASP.NET Core Logging 时,Microsoft 的所有组件都可以写入 ASP.NET Core Logging。 NLog (NLog.Web.AspNetCore) 集成也将获取这些日志,因此您可以将它们写入文件等。
所以 EF 组件可能将该异常写入了日志记录。您可以通过在配置
中使用${stracktrace}
或 ${logger}
来检查