private IEnumerable <AuditLogCollection.LogInfo> FindLogFolders(Folder rootFolder, bool includeEmptyFolders)
 {
     if (rootFolder.HasSubfolders)
     {
         using (QueryResult queryResult = rootFolder.FolderQuery(FolderQueryFlags.None, null, AuditLogCollection.SortByDisplayName, AuditLogCollection.FolderProperties))
         {
             object[][] rows;
             do
             {
                 rows = queryResult.GetRows(100);
                 if (rows != null && rows.Length > 0)
                 {
                     foreach (object[] row in rows)
                     {
                         DateTime logRangeStart;
                         DateTime logRangeEnd;
                         bool     validLogSubfolder = AuditLogCollection.TryParseLogRange(row[0] as string, out logRangeStart, out logRangeEnd);
                         int      itemCount         = AuditLogCollection.GetItemCount(row[2]);
                         if (validLogSubfolder && (includeEmptyFolders || itemCount > 0))
                         {
                             yield return(new AuditLogCollection.LogInfo
                             {
                                 FolderId = (StoreId)row[1],
                                 RangeStart = logRangeStart,
                                 RangeEnd = logRangeEnd,
                                 ItemCount = itemCount
                             });
                         }
                     }
                 }
             }while (rows != null && rows.Length > 0);
         }
     }
     yield break;
 }
        public static string GetLogFolderNameAndRange(DateTime timestamp, out DateTime rangeStart, out DateTime rangeEnd)
        {
            string text = timestamp.ToString(AuditLogCollection.DailyLogFolderNameFormat);

            if (!AuditLogCollection.TryParseLogRange(text, out rangeStart, out rangeEnd))
            {
                throw new InvalidOperationException("Invalid log folder name");
            }
            return(text);
        }
        public bool FindLog(DateTime timestamp, bool createIfNotExists, out IAuditLog auditLog)
        {
            auditLog = null;
            using (Folder folder = Folder.Bind(this.storeSession, this.auditLogRootId))
            {
                AuditLogCollection.LogInfo?logInfo = null;
                foreach (AuditLogCollection.LogInfo value in this.FindLogFolders(folder, false))
                {
                    if (value.RangeStart <= timestamp && timestamp < value.RangeEnd)
                    {
                        logInfo = new AuditLogCollection.LogInfo?(value);
                        break;
                    }
                }
                if (logInfo == null)
                {
                    if (!createIfNotExists)
                    {
                        if (this.IsTraceEnabled)
                        {
                            this.tracer.TraceDebug <DateTime>((long)this.GetHashCode(), "No matching log subfolder found. Lookup time={0}", timestamp);
                        }
                        return(false);
                    }
                    AuditLogCollection.LogInfo value2 = default(AuditLogCollection.LogInfo);
                    string logFolderNameAndRange      = AuditLogCollection.GetLogFolderNameAndRange(timestamp, out value2.RangeStart, out value2.RangeEnd);
                    using (Folder folder2 = Folder.Create(this.storeSession, this.auditLogRootId, StoreObjectType.Folder, logFolderNameAndRange, CreateMode.OpenIfExists))
                    {
                        if (folder2.Id == null)
                        {
                            folder2.Save();
                            folder2.Load();
                        }
                        value2.FolderId  = folder2.Id;
                        value2.ItemCount = folder2.ItemCount;
                        logInfo          = new AuditLogCollection.LogInfo?(value2);
                        if (this.IsTraceEnabled)
                        {
                            this.tracer.TraceDebug <DateTime, DateTime, string>((long)this.GetHashCode(), "New log subfolder created. StartTime=[{0}],EndTime=[{1}],FolderId=[{2}]", value2.RangeStart, value2.RangeEnd, value2.FolderId.ToBase64String());
                        }
                        goto IL_1AA;
                    }
                }
                if (this.IsTraceEnabled)
                {
                    this.tracer.TraceDebug <DateTime, DateTime, string>((long)this.GetHashCode(), "Found matching log subfolder StartTime=[{0}],EndTime=[{1}],FolderId=[{2}]", logInfo.Value.RangeStart, logInfo.Value.RangeEnd, logInfo.Value.FolderId.ToBase64String());
                }
IL_1AA:
                auditLog = new AuditLog(this.storeSession, logInfo.Value.FolderId, logInfo.Value.RangeStart, logInfo.Value.RangeEnd, logInfo.Value.ItemCount, this.recordFormatter);
            }
            return(true);
        }