/// <summary> /// Unused Currently. /// Extracts xml formatted properties from serilog mssql logs /// </summary> /// <param name="log"></param> /// <returns></returns> protected string ExtractPropertiesFromLog(DbLog log) { var settings = new XmlReaderSettings { Async = true }; var propertyDict = new Dictionary <string, string>(); try { propertyDict = RegexUtilities.DirtyXmlPropertyParser(log.Properties); if (propertyDict.Count == 0) { return(log.MessageTemplate); } } catch (Exception ex) { Console.WriteLine(ex); } var s = new StringBuilder(log.MessageTemplate); var match = Helpers.RegexUtilities.StringInterpolationHelper.Match(log.MessageTemplate); while (match.Success) { if (propertyDict.ContainsKey(match.Value)) { s.Replace($"{{{match.Value}}}", propertyDict[match.Value]); } match = match.NextMatch(); } return(s.ToString()); }
/// <summary> /// Asynchronously fetches <see cref="DbLog">log</see> entries from the database in a paginated manner. /// Supports filtering via the predicate parameter. /// </summary> /// <param name="pageSize"></param> /// <param name="page"></param> /// <param name="predicate"></param> /// <param name="cancellationToken"></param> public async Task <ApiResponse> GetAsync(int pageSize = 25, int page = 0, Expression <Func <DbLog, bool> > predicate = null, CancellationToken cancellationToken = default) { try { var r = await _dbContext.Logs.AsNoTracking().Where(predicate).OrderByDescending(x => x.TimeStamp).Skip(pageSize * page).Take(pageSize).ToArrayAsync(cancellationToken).ConfigureAwait(false); var count = await _dbContext.Logs.CountAsync(predicate, cancellationToken).ConfigureAwait(false); var syncPoint = await _dbContext.Logs.AsNoTracking().Where(predicate).OrderBy(x => x.TimeStamp).Select(x => x.Id).LastAsync(cancellationToken).ConfigureAwait(false); if (r.Length == 0) { return(new ApiResponse(Status204NoContent, $"No results for request; pageSize ={ pageSize}; page ={ page}")); } else { foreach (var item in r) { var propertyDict = RegexUtilities.DirtyXmlPropertyParser(item.Properties); item.LogProperties = propertyDict; } //return new ApiResponse(Status200OK, "", formattedMessageList); return(new ApiResponse( statusCode: Status200OK, message: $"Retrieved Type=DbLog;pageSize={pageSize};page={page}", result: r, paginationDetails: new PaginationDetails() { CollectionSize = count, PageIndex = page, PageSize = 25, SyncPointReference = syncPoint, })); } } catch (SqlException ex) { _logger.LogError(ex, "Error while retrieving Db Logs"); return(new ApiResponse(Status500InternalServerError, "Error while retrieving Db Logs", null)); } catch (InvalidOperationException ex) { _logger.LogError(ex, "Operation Error while retrieving Db Logs"); return(new ApiResponse(Status500InternalServerError, "Operation Error while retrieving Db Logs", null)); } }