private static bool ArchiveTimeRange(ImageServerLogWriter <ApplicationLog> writer, DateTime cutOffTime) { ApplicationLogSelectCriteria criteria = new ApplicationLogSelectCriteria(); criteria.Timestamp.LessThan(cutOffTime); criteria.Timestamp.SortAsc(0); using (ServerExecutionContext context = new ServerExecutionContext()) { IApplicationLogEntityBroker broker = context.ReadContext.GetBroker <IApplicationLogEntityBroker>(); List <ServerEntityKey> keyList = new List <ServerEntityKey>(1000); try { broker.Find(criteria, delegate(ApplicationLog result) { keyList.Add(result.Key); if (writer.WriteLog(result, result.Timestamp)) { // The logs been flushed, delete the log entries cached. // Purposely use a read context here, even though we're doing // an update, so we don't use transaction wrappers, optimization // is more important at this point. using ( IReadContext update = PersistentStoreRegistry.GetDefaultStore().OpenReadContext()) { IApplicationLogEntityBroker updateBroker = update.GetBroker <IApplicationLogEntityBroker>(); foreach (ServerEntityKey key in keyList) { updateBroker.Delete(key); } } keyList = new List <ServerEntityKey>(1000); } }); if (keyList.Count > 0) { // Purposely use a read context here, even though we're doing an update, so we // don't have to do an explicit commit and don't use transaction wrappers. using ( IReadContext update = PersistentStoreRegistry.GetDefaultStore().OpenReadContext()) { IApplicationLogEntityBroker updateBroker = update.GetBroker <IApplicationLogEntityBroker>(); foreach (ServerEntityKey key in keyList) { updateBroker.Delete(key); } } } } catch (Exception e) { Platform.Log(LogLevel.Error, e, "Unexpected exception when purging log files."); return(false); } return(true); } }
private bool ArchiveLogs(ServerFilesystemInfo archiveFs) { string archivePath = Path.Combine(archiveFs.Filesystem.FilesystemPath, "AlertLog"); DateTime cutOffTime = Platform.Time.Date.AddDays(ServiceLockSettings.Default.AlertCachedDays * -1); AlertSelectCriteria criteria = new AlertSelectCriteria(); criteria.InsertTime.LessThan(cutOffTime); criteria.InsertTime.SortAsc(0); using (ServerExecutionContext context = new ServerExecutionContext()) { IAlertEntityBroker broker = context.ReadContext.GetBroker <IAlertEntityBroker>(); ImageServerLogWriter <Alert> writer = new ImageServerLogWriter <Alert>(archivePath, "Alert"); List <ServerEntityKey> keyList = new List <ServerEntityKey>(500); try { broker.Find(criteria, delegate(Alert result) { keyList.Add(result.Key); // If configured, don't flush to disk. We just delete the contents of keyList below. if (!ServiceLockSettings.Default.AlertDelete) { if (writer.WriteLog(result, result.InsertTime)) { // The logs been flushed, delete the log entries cached. using ( IUpdateContext update = PersistentStoreRegistry.GetDefaultStore().OpenUpdateContext(UpdateContextSyncMode.Flush) ) { IApplicationLogEntityBroker updateBroker = update.GetBroker <IApplicationLogEntityBroker>(); foreach (ServerEntityKey key in keyList) { updateBroker.Delete(key); } update.Commit(); } keyList = new List <ServerEntityKey>(); } } }); writer.FlushLog(); if (keyList.Count > 0) { using ( IUpdateContext update = PersistentStoreRegistry.GetDefaultStore().OpenUpdateContext(UpdateContextSyncMode.Flush)) { IAlertEntityBroker updateBroker = update.GetBroker <IAlertEntityBroker>(); foreach (ServerEntityKey key in keyList) { updateBroker.Delete(key); } update.Commit(); } } } catch (Exception e) { Platform.Log(LogLevel.Error, e, "Unexpected exception when purging Alert log files."); writer.Dispose(); return(false); } writer.Dispose(); return(true); } }
private bool ArchiveLogs(ServerFilesystemInfo archiveFs) { try { using (ServerExecutionContext context = new ServerExecutionContext()) { string archivePath = Path.Combine(archiveFs.Filesystem.FilesystemPath, "ApplicationLog"); ApplicationLogSelectCriteria criteria = new ApplicationLogSelectCriteria(); criteria.Timestamp.SortAsc(0); IApplicationLogEntityBroker broker = context.ReadContext.GetBroker <IApplicationLogEntityBroker>(); ApplicationLog firstLog = broker.FindOne(criteria); if (firstLog == null) { return(true); } DateTime currentCutOffTime = firstLog.Timestamp.AddMinutes(5); int cachedDays = ServiceLockSettings.Default.ApplicationLogCachedDays; if (cachedDays < 0) { cachedDays = 0; } DateTime cutOffTime = Platform.Time.Date.AddDays(cachedDays * -1); if (currentCutOffTime > cutOffTime) { return(true); } using ( ImageServerLogWriter <ApplicationLog> writer = new ImageServerLogWriter <ApplicationLog>(archivePath, "ApplicationLog")) { while (currentCutOffTime < cutOffTime) { if (!ArchiveTimeRange(writer, currentCutOffTime)) { writer.FlushLog(); return(false); } currentCutOffTime = currentCutOffTime.AddMinutes(5); } // Now flush the last potential 5 minutes. if (!ArchiveTimeRange(writer, cutOffTime)) { writer.FlushLog(); return(false); } writer.FlushLog(); } return(true); } } catch (Exception e) { Platform.Log(LogLevel.Error, e, "Unexpected exception when writing log file."); return(false); } }