UnitOfWork:如何回滚和保留日志

UnitOfWork: How to rollback and keep the logs

我有如下方法:

using (var unitOfWork = _unitOfWorkManager.Begin())
{
    var result = await ExcuteOperator(input);
    if (!result.WorkflowStatus) 
    {
        unitOfWork.Dispose();
    }
    unitOfWork.Complete();
}


public async Task<ExecuteOperatorOutput> ExcuteOperator(ExecuteOperatorInput input)
{                  
    //Doing something
    DoSomething(input);

    //Insert log
    await _logRepository.InsertAsync(logInput);

    //Recursive
    return await ExcuteOperator(input);
}

我的问题是:如何在unitOfWork.Complete()之前手动回滚unitOfWork而不抛出异常并保留插入的日志?

无法手动回滚 unitOfWork

参见rationale/attempts:https://github.com/aspnetboilerplate/aspnetboilerplate/issues?q=rollback

也就是说,欢迎有兴趣的 reader 尝试解决问题。

How to keep the logs after the transaction rolled back?

开始新的 unitOfWork 完成:

public async Task<ExecuteOperatorOutput> ExcuteOperator(ExecuteOperatorInput input)
{                  
    //...

    using (var unitOfWork = _unitOfWorkManager.Begin(TransactionScopeOption.RequiresNew))
    {
        await _logRepository.InsertAsync(logInput);
        unitOfWork.Complete();
    }

    //...
}