Skip to content

Commit b4038f4

Browse files
authored
Merge pull request #34 from followynne/fix/ui-date-management
Improve date management and manipulation
2 parents a41fe14 + b699014 commit b4038f4

File tree

5 files changed

+30
-11
lines changed

5 files changed

+30
-11
lines changed

src/Serilog.Ui.MongoDbProvider/MongoDbDataProvider.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,16 @@ private void GenerateWhereClause(
9898
builder &= Builders<MongoDbLogModel>.Filter.Text(searchCriteria);
9999

100100
if (startDate != null)
101-
builder &= Builders<MongoDbLogModel>.Filter.Gte(entry => entry.Timestamp, startDate);
101+
{
102+
var utcStart = startDate.Value.ToUniversalTime();
103+
builder &= Builders<MongoDbLogModel>.Filter.Gte(entry => entry.UtcTimeStamp, utcStart);
104+
}
102105

103106
if (endDate != null)
104-
builder &= Builders<MongoDbLogModel>.Filter.Lte(entry => entry.Timestamp, endDate);
107+
{
108+
var utcEnd = endDate.Value.ToUniversalTime();
109+
builder &= Builders<MongoDbLogModel>.Filter.Lte(entry => entry.UtcTimeStamp, utcEnd);
110+
}
105111
}
106112
}
107113
}

src/Serilog.Ui.Web/Serilog.Ui.Web.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,10 @@
2424
<ItemGroup>
2525
<ProjectReference Include="..\Serilog.Ui.Core\Serilog.Ui.Core.csproj" />
2626
</ItemGroup>
27+
28+
<ItemGroup>
29+
<EmbeddedResource Update="wwwroot\dist\index.html">
30+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
31+
</EmbeddedResource>
32+
</ItemGroup>
2733
</Project>

src/Serilog.Ui.Web/SerilogUiMiddleware.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
using Newtonsoft.Json.Serialization;
1212
using Serilog.Ui.Core;
1313
using System;
14-
using System.Diagnostics;
14+
using System.Globalization;
1515
using System.IO;
1616
using System.Linq;
1717
using System.Net;
@@ -154,19 +154,15 @@ private async Task<string> FetchLogsAsync(HttpContext httpContext)
154154
int.TryParse(pageStr, out var currentPage);
155155
int.TryParse(countStr, out var count);
156156

157-
DateTime.TryParse(startDateStar, out var startDate);
158-
DateTime.TryParse(endDateStar, out var endDate);
159-
160-
if (endDate != default)
161-
endDate = new DateTime(endDate.Year, endDate.Month, endDate.Day, 23, 59, 59);
157+
DateTime.TryParse(startDateStar, CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out var startDate);
158+
DateTime.TryParse(endDateStar, CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out var endDate);
162159

163160
currentPage = currentPage == default ? 1 : currentPage;
164161
count = count == default ? 10 : count;
165162

166163
var provider = httpContext.RequestServices.GetService<IDataProvider>();
167164
var (logs, total) = await provider.FetchDataAsync(currentPage, count, levelStr, searchStr,
168165
startDate == default ? (DateTime?)null : startDate, endDate == default ? (DateTime?)null : endDate);
169-
//var result = JsonSerializer.Serialize(logs, _jsonSerializerOptions);
170166
var result = JsonConvert.SerializeObject(new { logs, total, count, currentPage }, _jsonSerializerOptions);
171167
return result;
172168
}

tests/Serilog.Ui.Common.Tests/Serilog.Ui.Common.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.3" />
1818
<PackageReference Include="MySql.Data" Version="8.0.23" />
1919
<PackageReference Include="Npgsql" Version="6.0.9" />
20-
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
20+
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
2121
<PackageReference Include="Serilog" Version="2.12.0" />
2222
<PackageReference Include="Testcontainers" Version="3.0.0" />
2323
<PackageReference Include="xunit.extensibility.core" Version="2.4.2" />

tests/Serilog.Ui.MongoDbProvider.Tests/Util/Builders/MongoDbDataProviderBuilder.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using MongoDB.Driver;
1+
using MongoDB.Bson.Serialization;
2+
using MongoDB.Bson.Serialization.Serializers;
3+
using MongoDB.Driver;
24
using Serilog.Ui.Common.Tests.DataSamples;
35
using Serilog.Ui.MongoDbProvider;
46
using System.Threading.Tasks;
@@ -28,6 +30,15 @@ public static async Task<MongoDbDataProviderBuilder> Build(bool useLinq3)
2830
public static async Task<LogModelPropsCollector> Seed(IMongoCollection<MongoDbLogModel> collection)
2931
{
3032
var (array, collector) = MongoDbLogModelFaker.Logs(100);
33+
34+
// https://stackoverflow.com/a/75637412/15129749
35+
var objectSerializer = new ObjectSerializer(type => ObjectSerializer.DefaultAllowedTypes(type) ||
36+
type.FullName.StartsWith("Serilog.Ui.Common.Tests") ||
37+
type.FullName.StartsWith("MongoDB.Bson.BsonDocument")
38+
39+
);
40+
BsonSerializer.RegisterSerializer(objectSerializer);
41+
3142
await collection.InsertManyAsync(array);
3243
return collector;
3344
}

0 commit comments

Comments
 (0)