Skip to content

Improve date management and manipulation #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/Serilog.Ui.MongoDbProvider/MongoDbDataProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,16 @@ private void GenerateWhereClause(
builder &= Builders<MongoDbLogModel>.Filter.Text(searchCriteria);

if (startDate != null)
builder &= Builders<MongoDbLogModel>.Filter.Gte(entry => entry.Timestamp, startDate);
{
var utcStart = startDate.Value.ToUniversalTime();
builder &= Builders<MongoDbLogModel>.Filter.Gte(entry => entry.UtcTimeStamp, utcStart);
}

if (endDate != null)
builder &= Builders<MongoDbLogModel>.Filter.Lte(entry => entry.Timestamp, endDate);
{
var utcEnd = endDate.Value.ToUniversalTime();
builder &= Builders<MongoDbLogModel>.Filter.Lte(entry => entry.UtcTimeStamp, utcEnd);
}
}
}
}
6 changes: 6 additions & 0 deletions src/Serilog.Ui.Web/Serilog.Ui.Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,10 @@
<ItemGroup>
<ProjectReference Include="..\Serilog.Ui.Core\Serilog.Ui.Core.csproj" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Update="wwwroot\dist\index.html">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</EmbeddedResource>
</ItemGroup>
</Project>
10 changes: 3 additions & 7 deletions src/Serilog.Ui.Web/SerilogUiMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
using Newtonsoft.Json.Serialization;
using Serilog.Ui.Core;
using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
Expand Down Expand Up @@ -154,19 +154,15 @@ private async Task<string> FetchLogsAsync(HttpContext httpContext)
int.TryParse(pageStr, out var currentPage);
int.TryParse(countStr, out var count);

DateTime.TryParse(startDateStar, out var startDate);
DateTime.TryParse(endDateStar, out var endDate);

if (endDate != default)
endDate = new DateTime(endDate.Year, endDate.Month, endDate.Day, 23, 59, 59);
DateTime.TryParse(startDateStar, CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out var startDate);
DateTime.TryParse(endDateStar, CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal, out var endDate);

currentPage = currentPage == default ? 1 : currentPage;
count = count == default ? 10 : count;

var provider = httpContext.RequestServices.GetService<IDataProvider>();
var (logs, total) = await provider.FetchDataAsync(currentPage, count, levelStr, searchStr,
startDate == default ? (DateTime?)null : startDate, endDate == default ? (DateTime?)null : endDate);
//var result = JsonSerializer.Serialize(logs, _jsonSerializerOptions);
var result = JsonConvert.SerializeObject(new { logs, total, count, currentPage }, _jsonSerializerOptions);
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.3" />
<PackageReference Include="MySql.Data" Version="8.0.23" />
<PackageReference Include="Npgsql" Version="6.0.9" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
<PackageReference Include="Serilog" Version="2.12.0" />
<PackageReference Include="Testcontainers" Version="3.0.0" />
<PackageReference Include="xunit.extensibility.core" Version="2.4.2" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using MongoDB.Driver;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Serializers;
using MongoDB.Driver;
using Serilog.Ui.Common.Tests.DataSamples;
using Serilog.Ui.MongoDbProvider;
using System.Threading.Tasks;
Expand Down Expand Up @@ -28,6 +30,15 @@ public static async Task<MongoDbDataProviderBuilder> Build(bool useLinq3)
public static async Task<LogModelPropsCollector> Seed(IMongoCollection<MongoDbLogModel> collection)
{
var (array, collector) = MongoDbLogModelFaker.Logs(100);

// https://stackoverflow.com/a/75637412/15129749
var objectSerializer = new ObjectSerializer(type => ObjectSerializer.DefaultAllowedTypes(type) ||
type.FullName.StartsWith("Serilog.Ui.Common.Tests") ||
type.FullName.StartsWith("MongoDB.Bson.BsonDocument")

);
BsonSerializer.RegisterSerializer(objectSerializer);

await collection.InsertManyAsync(array);
return collector;
}
Expand Down