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);
            }
        }
예제 #2
0
        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);
            }
        }