Example #1
0
        public PagedList<Log> SearchLogs(LogSearchCriteria criteria)
        {
            if (criteria == null)
                throw new ApplicationException("Search criteria is null.");

            using (var context = GetContext())
            {
                IQueryable<Log> query = from log in context.Logs
                                        join cl in context.CategoryLogs on log.LogId equals cl.LogId
                                        join c in context.Categories on cl.CategoryId equals c.CategoryId
                                        where c.CategoryName == criteria.Category
                                        select log;

                if (criteria.DateFrom != null)
                    query = query.Where(l => l.TimestampUtc >= criteria.DateFrom);

                if (criteria.DateTo != null)
                    query = query.Where(l => l.TimestampUtc <= criteria.DateTo);

                if (!string.IsNullOrEmpty(criteria.Title))
                    query = query.Where(l => l.Title == criteria.Title);

                if (criteria.Severity > 0)
                {
                    List<string> severities = new List<string>();
                    if ((criteria.Severity & (int)TraceEventType.Critical) != 0)
                        severities.Add(TraceEventType.Critical.ToString());
                    if ((criteria.Severity & (int)TraceEventType.Error) != 0)
                        severities.Add(TraceEventType.Error.ToString());
                    if ((criteria.Severity & (int)TraceEventType.Warning) != 0)
                        severities.Add(TraceEventType.Warning.ToString());
                    if ((criteria.Severity & (int)TraceEventType.Information) != 0)
                        severities.Add(TraceEventType.Information.ToString());
                    query = query.Where(l => severities.Contains(l.SeverityName));
                }

                query = query.SortBy(criteria.SortBy, criteria.SortByDesc);
                PagedList<int> logViewIds = new PagedList<int>(query.Select(l => l.LogId), criteria.StartIndex, criteria.PageSize);
                IQueryable<Log> secondQuery = context.Logs.Where(l => logViewIds.Contains(l.LogId));

                Dictionary<int, Log> logs = (criteria.ShouldIncludeMessage
                    ? secondQuery.Select(l => new LogToView
                    {
                        LogId = l.LogId,
                        TimestampUtc = l.TimestampUtc,
                        Message = l.FormattedMessage
                    })
                    : secondQuery.Select(l => new LogToView
                    {
                        LogId = l.LogId,
                        SeverityName = l.SeverityName,
                        Title = l.Title,
                        TimestampUtc = l.TimestampUtc,
                        Message = l.Message.Substring(0, criteria.MessageLength)
                    })).ToDictionary(l => l.LogId, l => (Log)l);

                return logViewIds.Transform<Log>(id => logs[id]);
            }
        }
Example #2
0
        public int ExportLogs(string categoryName, DateTime? from, DateTime? to, bool shouldDeleteFromDb, string outputPath, CancellationTokenSource cancellationTokenSource)
        {
            const string exportFileNameFormat = "DIS_{0}_{1:yyyy-MM-dd}_{2:yyyy-MM-dd}.xml";
            LogSearchCriteria criteria = new LogSearchCriteria() {
                Category = categoryName,
                Severity = int.MaxValue,
                DateFrom = from,
                DateTo = to,
                PageSize = MessageLogger.LogsPerFile,
                SortBy = "TimestampUtc",
                SortByDesc = false,
                ShouldIncludeMessage = true
            };

            int total = 0;
            int currentPage = 1;
            PagedList<Log> logs = null;
            try {
                while (cancellationTokenSource == null || !cancellationTokenSource.IsCancellationRequested) {
                    criteria.PageNumber = currentPage++;
                    logs = SearchLogs(criteria);
                    if (logs.Count == 0)
                        break;

                    total += logs.Count;
                    SplitAndWriteLogs(categoryName, outputPath, exportFileNameFormat, logs);
                    if (shouldDeleteFromDb)
                        logRepository.DeleteLogs(logs);

                    if (currentPage > logs.PageCount)
                        break;
                }
            }
            catch (UnauthorizedAccessException) {
                throw new DisException("Exception_PermissionsMsg");
            }

            return total;
        }
Example #3
0
        public PagedList<Log> GetSystemLogs(int severity, DateTime? from, DateTime? to, int pageNumber, int pageSize, string sortBy, bool sortByDesc)
        {
            if (severity == 0)
                return new PagedList<Log>(new List<Log>().AsQueryable(), 0, pageSize);

            LogSearchCriteria criteria = new LogSearchCriteria() {
                Category = MessageLogger.SystemCategoryName,
                Severity = severity,
                DateFrom = from,
                DateTo = to,
                PageSize = pageSize,
                PageNumber = pageNumber,
                ShouldIncludeMessage = false
            };
            if (!string.IsNullOrEmpty(sortBy)) {
                criteria.SortBy = sortBy;
                criteria.SortByDesc = sortByDesc;
            }
            return SearchLogs(criteria);
        }
Example #4
0
        private PagedList<Log> SearchLogs(LogSearchCriteria criteria)
        {
            if (criteria == null)
                throw new ApplicationException("Search criteria is null.");

            if (criteria.DateFrom != null)
                criteria.DateFrom = criteria.DateFrom.Value.ToUniversalTime();
            if (criteria.DateTo != null)
                criteria.DateTo = criteria.DateTo.Value.Date.AddDays(1).ToUniversalTime();

            if (criteria.PageSize < 0)
                criteria.PageSize = LogSearchCriteria.DefaultPageSize;
            else if (criteria.PageSize == 0)
                criteria.PageSize = int.MaxValue;

            return logRepository.SearchLogs(criteria);
        }
Example #5
0
 public PagedList<Log> GetOperationLogs(string userName, DateTime? from, DateTime? to, int pageNumber, int pageSize, string sortBy, bool sortByDesc)
 {
     LogSearchCriteria criteria = new LogSearchCriteria() {
         Category = MessageLogger.OperationCategoryName,
         Severity = (int)TraceEventType.Information,
         UserName = userName,
         DateFrom = from,
         DateTo = to,
         PageSize = pageSize,
         PageNumber = pageNumber,
         ShouldIncludeMessage = false
     };
     if (!string.IsNullOrEmpty(sortBy)) {
         criteria.SortBy = sortBy;
         criteria.SortByDesc = sortByDesc;
     }
     return SearchLogs(criteria);
 }