protected virtual void Dispose(bool disposing) { if (!_disposed) { if (disposing) { if (FileConnection != null) { FileConnection.Dispose(); FileConnection = null; } } _disposed = true; } // Delete the file in any case try { File.Delete(LockFileName); } catch (Exception) { // Ignore } }
private void CloseLogFile(bool raiseOnResetEvent) { if (FileConnection != null) { FileConnection.Dispose(); FileConnection = null; } if (raiseOnResetEvent) { OnReset?.Invoke(); } }
private void ResetInitialization() { lock (_syncRoot) { if (FileConnection != null) { FileConnection.Dispose(); FileConnection = null; } OnReset?.Invoke(); EnsureInitialize(); } }
private void EnsureInitialize() { TouchLockFile(); RemoveOldLockFiles(); if (FileConnection != null) { return; } lock (_syncRoot) { if (FileConnection != null) { return; } DateTime creationDate = DateTime.Now; string fileNamePrefix = creationDate.ToString("yyyyMMdd"); string fileName; FileStream stream; Exception ex; for (int i = 0; i < 10; i++) { fileName = fileNamePrefix + (i > 0 ? "_" + i : string.Empty) + ".txt"; string filePath = Path.Combine(_logDirectoryPath, fileName); if (!File.Exists(filePath)) { stream = TryOpenFile(filePath, out ex); if (stream == null) { // Ignoring this exception if the file has already created if (File.Exists(filePath)) { continue; } throw new Exception("Failed to create file '{0}'".FormatWith(filePath), ex); } FileConnection = new LogFileInfo { CreationDate = creationDate.Date, StartupTime = creationDate, FileName = fileName, FilePath = filePath, FileStream = stream, OldEntries = new string[0] }; WriteUTF8EncodingHeader(stream); return; } string[] alreadyWritten; if (!TryReadAndOpen(filePath, out alreadyWritten, out stream, out ex)) { // Trying another file name, since the file may be in use by another process continue; } FileConnection = new LogFileInfo { CreationDate = creationDate.Date, StartupTime = creationDate, FileName = fileName, FilePath = filePath, FileStream = stream, OldEntries = alreadyWritten }; return; } throw new InvalidOperationException("Failed to open/create a log file"); } }
private void ResetInitialization() { lock (_syncRoot) { if (FileConnection != null) { FileConnection.Dispose(); FileConnection = null; } if (OnReset != null) { OnReset(); } EnsureInitialize(); } }
private void EnsureInitialize() { TouchLockFile(); RemoveOldLockFiles(); if (FileConnection != null) return; lock (_syncRoot) { if (FileConnection != null) return; DateTime creationDate = DateTime.Now; string fileNamePrefix = creationDate.ToString("yyyyMMdd"); string fileName; FileStream stream; Exception ex; for (int i = 0; i < 10; i++) { fileName = fileNamePrefix + (i > 0 ? "_" + i : string.Empty) + ".txt"; string filePath = Path.Combine(_logDirectoryPath, fileName); if (!File.Exists(filePath)) { stream = TryOpenFile(filePath, out ex); if (stream == null) { // Ignoring this exception if the file has already created if (File.Exists(filePath)) continue; throw new Exception("Failed to create file '{0}'".FormatWith(filePath), ex); } FileConnection = new LogFileInfo { CreationDate = creationDate.Date, StartupTime = creationDate, FileName = fileName, FilePath = filePath, FileStream = stream }; WriteUTF8EncodingHeader(stream); return; } string[] alreadyWritten; if (!TryReadAndOpen(filePath, out alreadyWritten, out stream, out ex)) { // Trying another file name, since the file may be in use by another process continue; } FileConnection = new LogFileInfo { CreationDate = creationDate.Date, StartupTime = creationDate, FileName = fileName, FilePath = filePath, FileStream = stream }; return; } throw new InvalidOperationException("Failed to open/create a log file"); } }
protected virtual void Dispose(bool disposing) { if (!_disposed) { if (disposing) { if (FileConnection != null) { FileConnection.Dispose(); FileConnection = null; } } _disposed = true; } // Delete the file in any case try { File.Delete(LockFileName); } catch (Exception) { // Ignore } }