Skip to content

Commit 1385c01

Browse files
committed
Fix #15.
1 parent dcc7b0d commit 1385c01

File tree

3 files changed

+50
-22
lines changed

3 files changed

+50
-22
lines changed

src/Serilog.Ui.MongoDbProvider/MongoDbDataProvider.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public MongoDbDataProvider(IMongoClient client, MongoDbOptions options)
1717
if (options is null) throw new ArgumentNullException(nameof(options));
1818

1919
_collection = client.GetDatabase(options.DatabaseName).GetCollection<MongoDbLogModel>(options.CollectionName);
20+
var s = _collection.CollectionNamespace;
2021
}
2122

2223
public async Task<(IEnumerable<LogModel>, int)> FetchDataAsync(
@@ -27,12 +28,12 @@ public MongoDbDataProvider(IMongoClient client, MongoDbOptions options)
2728
DateTime? startDate = null,
2829
DateTime? endDate = null)
2930
{
30-
var logsTask = GetLogsAsync(page - 1, count, level, searchCriteria, startDate, endDate);
31-
var logCountTask = CountLogsAsync(level, searchCriteria, startDate, endDate);
31+
var logsTask = await GetLogsAsync(page - 1, count, level, searchCriteria, startDate, endDate);
32+
var logCountTask = await CountLogsAsync(level, searchCriteria, startDate, endDate);
3233

33-
await Task.WhenAll(logsTask, logCountTask);
34+
//await Task.WhenAll(logsTask, logCountTask);
3435

35-
return (await logsTask, await logCountTask);
36+
return (logsTask, logCountTask);
3637
}
3738

3839
private async Task<IEnumerable<LogModel>> GetLogsAsync(
@@ -46,7 +47,8 @@ private async Task<IEnumerable<LogModel>> GetLogsAsync(
4647
var builder = Builders<MongoDbLogModel>.Filter.Empty;
4748
GenerateWhereClause(ref builder, level, searchCriteria, startDate, endDate);
4849

49-
var logs = await _collection.Find(builder)
50+
var logs = await _collection
51+
.Find(builder)
5052
.Skip(count * page)
5153
.Limit(count)
5254
.SortByDescending(entry => entry.Timestamp)

src/Serilog.Ui.MongoDbProvider/MongoDbLogModel.cs

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
using System;
2-
using MongoDB.Bson.Serialization.Attributes;
1+
using MongoDB.Bson.Serialization.Attributes;
2+
using Newtonsoft.Json;
33
using Serilog.Ui.Core;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Dynamic;
47

58
namespace Serilog.Ui.MongoDbProvider
69
{
@@ -15,9 +18,12 @@ public class MongoDbLogModel
1518
public string RenderedMessage { get; set; }
1619

1720
[BsonDateTimeOptions(Kind = DateTimeKind.Local)]
18-
public DateTime Timestamp { get; set; }
21+
public DateTime? Timestamp { get; set; }
1922

20-
public string Exception { get; set; }
23+
[BsonDateTimeOptions(Kind = DateTimeKind.Local)]
24+
public DateTime UtcTimeStamp { get; set; }
25+
26+
public dynamic Exception { get; set; }
2127

2228
public object Properties { get; set; }
2329

@@ -28,11 +34,30 @@ internal LogModel ToLogModel()
2834
RowNo = Id,
2935
Level = Level,
3036
Message = RenderedMessage,
31-
Timestamp = Timestamp,
32-
Exception = Exception,
37+
Timestamp = Timestamp ?? UtcTimeStamp,
38+
Exception = GetException(Exception),
3339
Properties = Newtonsoft.Json.JsonConvert.SerializeObject(Properties),
3440
PropertyType = "json"
3541
};
3642
}
43+
44+
private object GetException(dynamic exception)
45+
{
46+
if (exception == null || IsPropertyExist(Exception, "_csharpnull"))
47+
return null;
48+
49+
if (exception is string)
50+
return exception;
51+
52+
return Newtonsoft.Json.JsonConvert.SerializeObject(Exception, Formatting.Indented);
53+
}
54+
55+
private bool IsPropertyExist(dynamic obj, string name)
56+
{
57+
if (obj is ExpandoObject)
58+
return ((IDictionary<string, object>)obj).ContainsKey(name);
59+
60+
return obj?.GetType()?.GetProperty(name) != null;
61+
}
3762
}
3863
}
Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

3-
<PropertyGroup>
4-
<TargetFramework>netstandard2.0</TargetFramework>
5-
<Version>2.1.0</Version>
6-
</PropertyGroup>
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
<Version>2.1.0</Version>
6+
</PropertyGroup>
77

8-
<ItemGroup>
9-
<PackageReference Include="MongoDB.Driver" Version="2.11.5" />
10-
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
11-
</ItemGroup>
8+
<ItemGroup>
9+
<PackageReference Include="MongoDB.Driver" Version="2.11.5" />
10+
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
11+
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
12+
</ItemGroup>
1213

13-
<ItemGroup>
14-
<ProjectReference Include="..\Serilog.Ui.Core\Serilog.Ui.Core.csproj" />
15-
</ItemGroup>
14+
<ItemGroup>
15+
<ProjectReference Include="..\Serilog.Ui.Core\Serilog.Ui.Core.csproj" />
16+
</ItemGroup>
1617
</Project>

0 commit comments

Comments
 (0)