private void Flush(FlushMode flushMode) { // If this is the forced flush on Dispose, wait until we acquire the lock. Otherwise, // just check to see if the lock is available, and if not, we are still processing the // last tranche of appends, so bail out and wait for the next flush interval. var lockTimeout = (flushMode == FlushMode.IfIdle ? TimeSpan.Zero : Timeout.InfiniteTimeSpan); bool acquiredLock = false; Monitor.TryEnter(_lock, lockTimeout, ref acquiredLock); if (!acquiredLock) { return; } try { var file = new FileInfo(_filePath); if (!file.Exists) { return; } var uploadPointer = file.Length; if (uploadPointer <= _flushPointer) { return; } using (var stm = new FileStream(_filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { stm.Seek(_flushPointer, SeekOrigin.Begin); _blob.AppendFromStream(stm, uploadPointer - _flushPointer); _flushPointer = uploadPointer; } } catch (Exception ex) { if (flushMode == FlushMode.IfIdle) { OnFlushError(ex); } else { throw; } } finally { Monitor.Exit(_lock); } }
private void SaveToAzureStorage(IEnumerable <EventData> messages) { string content = ""; foreach (EventData eventData in messages) { content += Encoding.UTF8.GetString(eventData.GetBytes()); } var stream = new MemoryStream(Encoding.UTF8.GetBytes(content)); appBlob.AppendFromStream(stream); }