public void Store(CommandLogEntry commandLogEntry) { using (var session = _store.LightweightSession()) { session.Store(commandLogEntry); session.SaveChanges(); } }
public async Task <TResult> HandleAsync <TCommand, TResult>(TCommand command) where TCommand : ICommand <TResult> { if (_serviceProvider.GetService(typeof(ICommandHandler <TCommand, TResult>)) is not ICommandHandler <TCommand, TResult> service) { throw new ApplicationException($"The Command Dispatcher cannot find command handler: {typeof(TCommand).Name} Notice that you can use the AddCQRS extension in OpenFTTH.CQRS to easily add command and query handlers."); } var cmdResult = await service.HandleAsync(command); if (command is BaseCommand baseCommand && cmdResult is Result result) { if (baseCommand.CorrelationId == Guid.Empty) { _logger.LogError($"{ typeof(TCommand).Name } command has empty correlation id. Please make sure all initated commands has a correlation id set."); } string atNode = ""; if (baseCommand.UserContext != null && baseCommand.UserContext.EditingRouteNodeId != Guid.Empty) { atNode = " from route node: " + baseCommand.UserContext.EditingRouteNodeId.ToString() + ","; } if (result.IsFailed && result.Errors != null) { foreach (var error in result.Errors) { _logger.LogWarning($"{ typeof(TCommand).Name } command with id {baseCommand.CmdId}, correlation id: {baseCommand.CorrelationId}, invoked by user: '******',{atNode} failed with message: {error.Message}"); } } else { _logger.LogInformation($"{ typeof(TCommand).Name } command with id {baseCommand.CmdId}, correlation id: {baseCommand.CorrelationId}, invoked by user: '******',{atNode} was successfully processed."); } // Store command in event store if (_eventStore != null) { var cmdLogEntry = new CommandLogEntry(baseCommand.CmdId, command, result); _eventStore.CommandLog.Store(cmdLogEntry); } } return(cmdResult); }
public void CommandLogTest() { if (_connectionString == null) { return; } var eventStore = new PostgresEventStore(null, _connectionString, "TestCommandLog", true) as IEventStore; var myCmd = new TestCommand() { CmdId = Guid.NewGuid(), Name = "Hans", Weight = 120, Gender = Gender.Male }; var failedResult = Result.Fail("Command failed for some reason. DOH!"); var myCmdLogEntry = new CommandLogEntry(myCmd.CmdId, myCmd, failedResult); eventStore.CommandLog.Store(myCmdLogEntry); var myCmdLogEntryFromLoad = eventStore.CommandLog.Load(myCmd.CmdId); // Assert myCmdLogEntryFromLoad.IsSuccess.Should().BeFalse(); myCmdLogEntryFromLoad.ErrorMessages.Should().Contain(failedResult.Errors.First().Message); }