我在我的 c# 代码中使用事务。在 TransactionScope 中,我记录了几乎所有内容,以确保所有内容都在测试中完成。但是,如果事务中发生了某些事情并且它去捕获我的日志类不会写任何东西。在 localhost 我将 TransactionScopeOption 更改为“ Suppress ”,因为数据库不在我的服务器中。当我调试一切正常时,我可以写入日志文件。但是当我更改为“必需”时,如果它给出错误,它会像往常一样取消所有内容,但它也无法写入第一个日志。我怎样才能解决这个问题?
我的代码示例:
TransactionOptions tr = new TransactionOptions();
tr.IsolationLevel = System.Transactions.IsolationLevel.Serializable;
using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, tr, System.Transactions.EnterpriseServicesInteropOption.Automatic))//System.TimeSpan.MaxValue))
{
bool status = false;
// this log must be written anyway but it won't when it gives an error.
LogProcess log = new LogProcess(Ssession["DBNAME"], new Guid(), "Transaction begun.");
try
{
// process 1
// process 2
.
.
.
if(status)
{
LogProcess log1 = new LogProcess(Ssession["DBNAME"], new Guid(), "Transaction will complete soon.");
scope.Complete();
LogProcess log1 = new LogProcess(Ssession["DBNAME"], new Guid(), "Transaction completed.");
}
else
{
LogProcess log2 = new LogProcess(Ssession["DBNAME"], new Guid(), "Transaction will dispose.");
scope.Dispose();
LogProcess log2 = new LogProcess(Ssession["DBNAME"], new Guid(), "Transaction disposed.");
}
}
catch(Exception ex)
{
LogProcess log1 = new LogProcess(Ssession["DBNAME"], new Guid(), "Transaction exception. Error: " + ex.Message);
scope.Dispose();
}
}