Skip to content

UtcTimeStamp instead of TimeStamp in MongoDbDataProvider #122

Closed
@TVAps

Description

@TVAps

Describe the bug
Bug is that logs aren't ordered in serilog-ui with a Mongo database.

To Reproduce
Steps to reproduce the behavior:

  1. Use Serilog.UI and Serilog.UI.MongoDbProvider nuggets.
  2. Open the serilog-ui page
  3. You'll see your logs not ordered by date.

Expected behavior
Logs order by DESC date when using a mongo db.

Screenshots
image
The form is not filtered.
I've precise what are suppose to be the good order.
1 and 4 has to be 8 and 9 even if it's same date, i've check in database milliseconds.

Desktop :

  • OS: Windows 11
  • Browser Chrome and Firefox (last versions)
  • Version net6.0 and net8.0

Additional context
Following this: #34 where TimeStamp has been change to UtcTimeStamp.
I think there is still a problem in MongoDbDataProvider > GetLogsAsync() because code is still using TimeStamp to do the SortByDescending.

private async Task<IEnumerable<LogModel>> GetLogsAsync(
            int page,
            int count,
            string level,
            string searchCriteria,
            DateTime? startDate,
            DateTime? endDate)
        {
            try
            {
                var builder = Builders<MongoDbLogModel>.Filter.Empty;
                GenerateWhereClause(ref builder, level, searchCriteria, startDate, endDate);

                if (!string.IsNullOrWhiteSpace(searchCriteria))
                {
                    await _collection.Indexes.CreateOneAsync(
                        new CreateIndexModel<MongoDbLogModel>(Builders<MongoDbLogModel>.IndexKeys.Text(p => p.RenderedMessage)));
                }

                var logs = await _collection
                    .Find(builder)
                    .Skip(count * page)
                    .Limit(count)
                    .SortByDescending(entry => entry.Timestamp)
                    .ToListAsync();

                var index = 1;
                foreach (var log in logs)
                    log.Id = (page * count) + index++;

                return logs.Select(log => log.ToLogModel()).ToList();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                throw;
            }
        }

I've also check code in feature/3.0 branch, sort is also done with TimeStamp.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions