In my asp.net core web application, I am logging the exceptions to the database.Till now everything works fine but problem is that when the DbConext.SaveChanges in try block throws an exception, cannot log the exception to the database as DbConext.SaveChanges in catch block also throws the same exception.
Here is the code I have tried so far :
try
{
_unitOfWork.Repository<Product>().InsertEntity(product);
await _unitOfWork.SaveChangesAsync();//This throws exception due the model validation failure
}
catch (Exception exception)
{
ExceptionModel exceptionModel = new ExceptionModel();
using (MyDbContext dbContext = new MyDbContext())
{
await dbContext.ExceptionModels.AddAsync(exceptionModel);
await dbContext.SaveChangesAsync(); // Throws the same exception that was thrown in try block due to entity framework transaction
}
}
Note: I guess the problem is causing due to the Entity Framework transaction.
Please help how can I overcome this situation to log the exception to the database. Thanks!