Skip to content

Commit 6a83edc

Browse files
committed
Add start and end date to SQL Server where clause.
1 parent 9b33ba5 commit 6a83edc

File tree

1 file changed

+56
-28
lines changed

1 file changed

+56
-28
lines changed

src/Serilog.Ui.MsSqlServerProvider/SqlServerDataProvider.cs

Lines changed: 56 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -27,37 +27,30 @@ public SqlServerDataProvider(RelationalDbOptions options)
2727
DateTime? endDate = null
2828
)
2929
{
30-
var logsTask = GetLogsAsync(page - 1, count, logLevel, searchCriteria);
31-
var logCountTask = CountLogsAsync(logLevel, searchCriteria);
30+
var logsTask = GetLogsAsync(page - 1, count, logLevel, searchCriteria, startDate, endDate);
31+
var logCountTask = CountLogsAsync(logLevel, searchCriteria, startDate, endDate);
3232

3333
await Task.WhenAll(logsTask, logCountTask);
3434

3535
return (await logsTask, await logCountTask);
3636
}
3737

38-
private async Task<IEnumerable<LogModel>> GetLogsAsync(int page, int count, string level, string searchCriteria)
38+
private async Task<IEnumerable<LogModel>> GetLogsAsync(
39+
int page,
40+
int count,
41+
string level,
42+
string searchCriteria,
43+
DateTime? startDate,
44+
DateTime? endDate)
3945
{
4046
var queryBuilder = new StringBuilder();
41-
queryBuilder.Append("SELECT [Id], [Message], [Level], [TimeStamp], [Exception], [Properties] FROM[");
47+
queryBuilder.Append("SELECT [Id], [Message], [Level], [TimeStamp], [Exception], [Properties] FROM [");
4248
queryBuilder.Append(_options.Schema);
4349
queryBuilder.Append("].[");
4450
queryBuilder.Append(_options.TableName);
4551
queryBuilder.Append("] ");
4652

47-
var whereIncluded = false;
48-
49-
if (!string.IsNullOrEmpty(level))
50-
{
51-
queryBuilder.Append("WHERE [LEVEL] = @Level ");
52-
whereIncluded = true;
53-
}
54-
55-
if (!string.IsNullOrEmpty(searchCriteria))
56-
{
57-
queryBuilder.Append(whereIncluded
58-
? "AND [Message] LIKE @Search OR [Exception] LIKE @Search "
59-
: "WHERE [Message] LIKE @Search OR [Exception] LIKE @Search ");
60-
}
53+
GenerateWhereClause(queryBuilder, level, searchCriteria, startDate, endDate);
6154

6255
queryBuilder.Append("ORDER BY Id DESC OFFSET @Offset ROWS FETCH NEXT @Count ROWS ONLY");
6356

@@ -69,7 +62,9 @@ private async Task<IEnumerable<LogModel>> GetLogsAsync(int page, int count, stri
6962
Offset = page * count,
7063
Count = count,
7164
Level = level,
72-
Search = searchCriteria != null ? "%" + searchCriteria + "%" : null
65+
Search = searchCriteria != null ? "%" + searchCriteria + "%" : null,
66+
StartDate = startDate,
67+
EndDate = endDate
7368
});
7469

7570
var index = 1;
@@ -80,15 +75,42 @@ private async Task<IEnumerable<LogModel>> GetLogsAsync(int page, int count, stri
8075
}
8176
}
8277

83-
public async Task<int> CountLogsAsync(string level, string searchCriteria)
78+
public async Task<int> CountLogsAsync(
79+
string level,
80+
string searchCriteria,
81+
DateTime? startDate = null,
82+
DateTime? endDate = null)
8483
{
8584
var queryBuilder = new StringBuilder();
86-
queryBuilder.Append("SELECT COUNT(Id) FROM[");
85+
queryBuilder.Append("SELECT COUNT(Id) FROM [");
8786
queryBuilder.Append(_options.Schema);
8887
queryBuilder.Append("].[");
8988
queryBuilder.Append(_options.TableName);
9089
queryBuilder.Append("] ");
9190

91+
GenerateWhereClause(queryBuilder, level, searchCriteria, startDate, endDate);
92+
93+
using (IDbConnection connection = new SqlConnection(_options.ConnectionString))
94+
{
95+
return await connection.ExecuteScalarAsync<int>(queryBuilder.ToString(),
96+
new
97+
{
98+
Level = level,
99+
Search = searchCriteria != null ? "%" + searchCriteria + "%" : null,
100+
StartDate = startDate,
101+
EndDate = endDate
102+
});
103+
}
104+
}
105+
106+
private void GenerateWhereClause(
107+
StringBuilder queryBuilder,
108+
string level,
109+
string searchCriteria,
110+
DateTime? startDate = null,
111+
DateTime? endDate = null
112+
)
113+
{
92114
var whereIncluded = false;
93115

94116
if (!string.IsNullOrEmpty(level))
@@ -102,16 +124,22 @@ public async Task<int> CountLogsAsync(string level, string searchCriteria)
102124
queryBuilder.Append(whereIncluded
103125
? "AND [Message] LIKE @Search OR [Exception] LIKE @Search "
104126
: "WHERE [Message] LIKE @Search OR [Exception] LIKE @Search ");
127+
whereIncluded = true;
105128
}
106129

107-
using (IDbConnection connection = new SqlConnection(_options.ConnectionString))
130+
if (startDate != null)
108131
{
109-
return await connection.ExecuteScalarAsync<int>(queryBuilder.ToString(),
110-
new
111-
{
112-
Level = level,
113-
Search = searchCriteria != null ? "%" + searchCriteria + "%" : null
114-
});
132+
queryBuilder.Append(whereIncluded
133+
? "AND [TimeStamp] >= @StartDate "
134+
: "WHERE [TimeStamp] >= @StartDate ");
135+
whereIncluded = true;
136+
}
137+
138+
if (endDate != null)
139+
{
140+
queryBuilder.Append(whereIncluded
141+
? "AND [TimeStamp] <= @EndDate "
142+
: "WHERE [TimeStamp] <= @EndDate ");
115143
}
116144
}
117145
}

0 commit comments

Comments
 (0)