public async Task <Possible <string, Failure> > CloseAsync(Guid activityId) { try { using (var counter = m_counters.CloseCounter()) { using (var eventing = new CloseActivity(BasicFilesystemCache.EventSource, activityId, this)) { eventing.Start(); m_closed = true; if (m_sessionLockFile != null) { try { // Write out our session data (strong fingerprints) if (m_fingerprints.Count > 0) { using (FileStream sessionFile = await m_cache.ContendedOpenStreamAsync(m_completedFilename, FileMode.Create, FileAccess.Write, FileShare.None)) { await BasicFilesystemCache.WriteSessionFingerprints(sessionFile, m_fingerprints.Keys); counter.SessionFingerprints(m_fingerprints.Count); } } } catch (Exception e) { return(eventing.StopFailure(new WriteCompletedSessionFailure(m_cache.CacheId, m_completedFilename, e))); } finally { Close(); } } return(eventing.Returns(m_sessionId)); } } } finally { // Update the cache disconnect count - only count the disconnects that happened after the session has started this.m_counters.SetCacheDisconnectedCounter(m_cache.DisconnectCount - m_cacheDisconnectCountAtSessionStart); // We need to do the collection of statistics and logging // at the last point after close has completed. using (var eventing = new CacheActivity(BasicFilesystemCache.EventSource, CacheActivity.StatisticOptions, activityId, "SessionStatistics", CacheId)) { eventing.Start(); eventing.WriteStatistics(m_finalStats.Value); eventing.Stop(); } } }