public async Task CreateAuditAsync(DeploymentAudit deploymentAudit)
 {
     var updateDeploymentTask = deploymentRepository.UpdateMostRecentAuditAsync(deploymentAudit.DeploymentId, deploymentAudit.DeploymentAuditId);
     var insertAuditTask = collection.InsertOneAsync(deploymentAudit);
     await Task.WhenAll(updateDeploymentTask, insertAuditTask);
 }
        private async Task<DeploymentAudit> HashDeployment(Deployment deployment, IList<Regex> fileExclusionExpressions, bool hashHiddenFiles)
        {
            DeploymentAudit audit;
            try
            {
                var hashResults = new List<FileHash>();
                IList<FileHashMismatch> hashDifferences = new List<FileHashMismatch>();
                var sw = Stopwatch.StartNew();
                await HashDirectory(deployment, fileExclusionExpressions, hashHiddenFiles, hashResults);
                var hash = HashTheHashResults(hashResults);
                
                if (deployment.Hash == Deployment.EmptyHash)
                {
                    deployment.Hash = hash;
                    deployment.FileHashes = hashResults;
                }
                bool validHash = deployment.Hash == hash;
                if (!validHash)
                {
                    hashDifferences = DetermineHashDifferences(deployment.FileHashes, hashResults);
                }
                sw.Stop();
                audit = new DeploymentAudit
                {
                    DeploymentId = deployment.DeploymentId,
                    Hash = hash,
                    ValidHash = validHash,
                    FileHashMismatches = hashDifferences
                };
                if (deployment.MostRecentAudit == Guid.Empty)
                {
                    deployment.MostRecentAudit = audit.DeploymentAuditId;
                }

                if (log.IsDebugEnabled)
                {
                    log.Debug($"Completed audit for application {deployment.ApplicationName} on server {deployment.ServerName} with hash {hash} in {sw.Elapsed.TotalSeconds} seconds. \r\n Results: {audit.ValidHash} \r\n List of files included in hash: \r\n {string.Join("\r\n", hashResults)}");
                }
                else
                {
                    log.Info($"Completed audit for application {deployment.ApplicationName} on server {deployment.ServerName} with hash {hash} in {sw.Elapsed.TotalSeconds} seconds. \r\n Results: {audit.ValidHash}");
                }
            }
            catch (Exception ex)
            {
                log.Error("Error while running audit for application {deployment.ApplicationName} on server {deployment.ServerName}.", ex);
                audit = new DeploymentAudit
                {
                    DeploymentId = deployment.DeploymentId,
                    Error = ex.Message
                };
            }
            return audit;
        }