serilog-contrib / serilog-ui

Simple Serilog log viewer UI for several sinks.
MIT License
226 stars 42 forks source link

UtcTimeStamp instead of TimeStamp in MongoDbDataProvider #122

Closed TVAps closed 3 months ago

TVAps commented 5 months ago

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 :

Additional context Following this: https://github.com/serilog-contrib/serilog-ui/pull/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.

followynne commented 4 months ago

Hi @TVAps

Thanks for opening the issue, I'll give it a look those days as soon as I can.

followynne commented 3 months ago

@TVAps

thanks again for opening the issue, you'll find it fixed from v2.3.2 onwards 🛩️