private void HandleFailedSend(FileLock markLock, string bufferName, ref LineReader streamReader, FileLock readerLock, List <string> memoryBuffer, int failLimit, Action <List <string> > alternativeWrite) { try { markLock.Lock(); var buffers = ReadMark(markLock).Buffers; var buffer = GetBufferIndex(bufferName, buffers, out var index); if (buffer.FailedSendCount >= failLimit) { alternativeWrite(memoryBuffer); HandleSuccessSend(markLock, bufferName, ref streamReader, readerLock, memoryBuffer); } else { var modifiedBuffer = new BufferInfo(buffer.Name, buffer.Written, buffer.Read, buffer.WriteActive, buffer.LastFlushTime, buffer.FailedSendCount + 1, DateTime.Now); buffers[index] = modifiedBuffer; WriteMark(markLock, buffers); streamReader.Close(); streamReader = null; } } finally { markLock.Unlock(); } }
private LineReader OpenReadBufferStream(BufferInfo bufferInfo, out FileLock streamLock) { streamLock = null; if (bufferInfo.Read >= bufferInfo.Written) { return(null); } var bufferPath = GetBufferFilePath(bufferInfo); if (_buffer.StreamService.IsStreamExist(bufferPath) == false) { return(null); } streamLock = new FileLock(bufferPath); if (streamLock.Lock(TimeSpan.FromMilliseconds(OpenReadStreamTimeout)) == false) { streamLock = null; return(null); } try { var streamReader = _buffer.StreamService.OpenReadStream(streamLock.Path, FileMode.Open, FileShare.Write); var lineReader = new LineReader(streamReader); for (var i = 0; i < bufferInfo.Read; i++) { if (lineReader.ReadLine(out var line) == false) { if (line != null) { } else { break; } } } return(lineReader); } catch (IOException e) { _logger.LogError(e, "OpenReadBufferStream."); } finally { streamLock.Unlock(); } streamLock = null; return(null); }
[System.Security.SecurityCritical] // auto-generated #endif public void Remove() { #if !FEATURE_LEGACYNETCF bool removedAll = true; FileLock groupLock = FileLock.GetFileLock(Path.Combine(IsolatedStorageFile.IsolatedStorageRoot, IsolatedStorageFile.s_LockPathPrefix), IsolatedStorageFile.s_GroupPathPrefix + "-" + m_ObfuscatedId); try { groupLock.Lock(); foreach (string storeDir in Directory.UnsafeGetDirectories(Path.Combine(IsolatedStorageFile.IsolatedStorageRoot, IsolatedStorageFile.s_StorePathPrefix), "*", SearchOption.TopDirectoryOnly)) { string groupFile = Path.Combine(storeDir, IsolatedStorageFile.s_GroupFileName); if (m_ObfuscatedId.Equals(File.UnsafeReadAllText(groupFile))) { IsolatedStorageFile f = IsolatedStorageFile.GetUserStoreFromGroupAndStorePath(Group, storeDir); removedAll = removedAll & f.TryRemove(); } } IsolatedStorageFile.TouchFile(Path.Combine(m_GroupPath, IsolatedStorageFile.s_CleanupFileName)); if (removedAll) { IsolatedStorageAccountingInfo.RemoveAccountingInfo(m_GroupPath); File.UnsafeDelete(Path.Combine(m_GroupPath, IsolatedStorageFile.s_IdFileName)); File.UnsafeDelete(Path.Combine(m_GroupPath, IsolatedStorageFile.s_CleanupFileName)); Directory.UnsafeDelete(m_GroupPath, false); } } catch (IOException) { // There isn't anything we can really do about this. Ignoring these sorts of issues shouldn't lead to corruption. } catch (UnauthorizedAccessException) { // There isn't anything we can really do about this. Ignoring these sorts of issues shouldn't lead to corruption. } finally { groupLock.Unlock(); } #else // !FEATURE_LEGACYNETCF try { using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication()) { isf.Remove(); } } catch (IOException) { // There isn't anything we can really do about this. Ignoring these sorts of issues shouldn't lead to corruption. } catch (UnauthorizedAccessException) { // There isn't anything we can really do about this. Ignoring these sorts of issues shouldn't lead to corruption. } #endif // !FEATURE_LEGACYNETCF }
private void CleanStalledBuffers(FileLock markLock) { try { markLock.Lock(); var oldBuffers = ReadMark(markLock).Buffers; var newBuffers = new List <BufferInfo>(); foreach (var bufferInfo in oldBuffers) { // TODO Investigate case when read is greater than written if (bufferInfo.WriteActive == false && bufferInfo.Read >= bufferInfo.Written) { var bufferFilePath = GetBufferFilePath(bufferInfo); if (_buffer.StreamService.IsStreamExist(bufferFilePath)) { try { DeleteBuffer(bufferFilePath); } catch (IOException) { newBuffers.Add(bufferInfo); } } } else { newBuffers.Add(bufferInfo); } } if (newBuffers.Count != oldBuffers.Count) { WriteMark(markLock, newBuffers); } } finally { markLock.Unlock(); } }
private BufferInfo ModifyBuffer(FileLock markLock, string bufferName, Func <BufferInfo, BufferInfo> mutator) { try { markLock.Lock(); var buffers = ReadMark(markLock).Buffers; var buffer = GetBufferIndex(bufferName, buffers, out var index); var modifiedBuffer = mutator(buffer); buffers[index] = modifiedBuffer; WriteMark(markLock, buffers); return(modifiedBuffer); } finally { markLock.Unlock(); } }
private bool IsReadBufferEmpty(FileLock markLock) { try { markLock.Lock(); var mark = ReadMark(markLock); return(mark.Read >= mark.Written); } catch (IOException e) { _logger.LogError(e, "IsReadBufferEmpty"); return(true); } finally { markLock.Unlock(); } }
[System.Security.SecurityCritical] // auto-generated #endif public static IEnumerable <IsolatedStorageGroup> GetGroups() { List <IsolatedStorageGroup> groups = new List <IsolatedStorageGroup>(); #if !FEATURE_LEGACYNETCF try { foreach (string groupDir in Directory.UnsafeGetDirectories(Path.Combine(IsolatedStorageFile.IsolatedStorageRoot, IsolatedStorageFile.s_GroupPathPrefix), "*", SearchOption.TopDirectoryOnly)) { string idFile = Path.Combine(groupDir, IsolatedStorageFile.s_IdFileName); string id; FileLock groupLock = FileLock.GetFileLock(Path.Combine(IsolatedStorageFile.IsolatedStorageRoot, IsolatedStorageFile.s_LockPathPrefix), IsolatedStorageFile.s_GroupPathPrefix + "-" + DirectoryInfo.UnsafeCreateDirectoryInfo(groupDir).Name); try { groupLock.Lock(); if (!File.UnsafeExists(Path.Combine(groupDir, IsolatedStorageFile.s_CleanupFileName))) { id = File.UnsafeReadAllText(idFile); if (IsolatedStorageAccountingInfo.IsAccountingInfoValid(groupDir)) { using (IsolatedStorageAccountingInfo accountingInfo = new IsolatedStorageAccountingInfo(groupDir)) { groups.Add(new IsolatedStorageGroup(id, accountingInfo.Quota, accountingInfo.UsedSize, groupDir)); } } else { // In this case we've tried to deseriaize a group that doesn't have valid data. We'll try to remove it. try { new IsolatedStorageGroup(id, 0, 0, groupDir).Remove(); } catch (Exception) { // We couldn't remove the group for some reason. Ignore it and move on. } } } } catch (IOException) { // There isn't anything we can really do about this. Ignoring these sorts of issues shouldn't lead to corruption. } catch (UnauthorizedAccessException) { // There isn't anything we can really do about this. Ignoring these sorts of issues shouldn't lead to corruption. } finally { groupLock.Unlock(); } } } catch (IOException) { // There isn't anything we can really do about this. Ignoring these sorts of issues shouldn't lead to corruption. } catch (UnauthorizedAccessException) { // There isn't anything we can really do about this. Ignoring these sorts of issues shouldn't lead to corruption. } #else try { using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication()) { groups.Add(new IsolatedStorageGroup(isf.GroupName, Int64.MaxValue, 0, IsolatedStorageFile.IsolatedStorageRoot)); } } catch (IOException) { // There isn't anything we can really do about this. Ignoring these sorts of issues shouldn't lead to corruption. } catch (UnauthorizedAccessException) { // There isn't anything we can really do about this. Ignoring these sorts of issues shouldn't lead to corruption. } catch (IsolatedStorageException) { // There isn't anything we can really do about this. Ignoring these sorts of issues shouldn't lead to corruption. } #endif return(groups); }
private LineReader OpenReadBufferStream(FileLock markLock, out string bufferName, out FileLock streamLock) { try { markLock.Lock(); bufferName = null; var buffers = ReadMark(markLock).Buffers; foreach (var buffer in buffers) { if (HasPauseTimeoutExpired(buffer) == false) { continue; } if (buffer.WriteActive && buffer.Written - buffer.Read < _readLimit && buffer.LastFlushTime + _flushTimeout > DateTime.Now) { continue; } var lineReader = OpenReadBufferStream(buffer, out streamLock); if (lineReader == null) { continue; } if (lineReader.ReadFinished) { try { if (buffer.WriteActive == false) { lineReader.Close(); DeleteBuffer(streamLock.Path); } } catch (Exception e) { _logger.LogError(e, $"Delete buffer '{streamLock.Path}' error."); } continue; } bufferName = buffer.Name; return(lineReader); } streamLock = null; return(null); } finally { markLock.Unlock(); } }
private void Flush(bool force, bool push = true) { try { List <TItem> flushBuffer; lock (_memoryBuffer) { flushBuffer = _memoryBuffer; var bufferCount = flushBuffer.Count; if (bufferCount == 0) { return; } if (force == false && bufferCount < _memoryBufferLimit) { return; } _memoryBuffer = RentMemoryBuffer(); } lock (_syncFlush) { try { if (_streamWriter == null) { _streamWriter = OpenWriteBufferStream(out _writeBuffer, out _writerLock); } var count = 0; try { _writerLock.Lock(); _streamWriter.BaseStream.Seek(0, SeekOrigin.End); foreach (var logEvent in flushBuffer) { WriteStartLine(_streamWriter); _buffer.FormatItem(logEvent, _streamWriter); WriteEndLine(_streamWriter); count++; } _streamWriter.Flush(); } finally { _writerLock.Unlock(); } ReleaseMemoryBuffer(flushBuffer); if (MarkWrite(_markLock, _writeBuffer, count).WriteActive == false) { _streamWriter.Close(); _streamWriter = null; _writerLock = null; } } catch (Exception e) { _logger.LogError(e, "Flush."); } } } finally { if (push) { PushBlock(); } } }