Skip to content

Commit 688702a

Browse files
committed
Add log count select options.
1 parent 7e2cbfb commit 688702a

File tree

4 files changed

+62
-55
lines changed

4 files changed

+62
-55
lines changed

src/Serilog.Ui.Web/Controllers/LogsController.cs

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,35 @@
11
using Microsoft.AspNetCore.Mvc;
2+
using Microsoft.AspNetCore.Mvc.Rendering;
23
using Serilog.Ui.Core;
34
using Serilog.Ui.Web.ViewModel;
5+
using System.Collections.Generic;
46
using System.IO;
7+
using System.Linq;
58
using System.Text;
69
using System.Threading.Tasks;
710

811
namespace Serilog.Ui.Web.Controllers
912
{
1013
public class LogsController : Controller
1114
{
12-
private static string _scripts;
13-
private static string _styles;
15+
private static readonly string Scripts;
16+
private static readonly string Styles;
17+
private static readonly IEnumerable<SelectListItem> SelectListItems;
1418
private readonly IDataProvider _dataProvider;
1519

20+
static LogsController()
21+
{
22+
Styles = SetResource("Serilog.Ui.Web.wwwroot.css.main.css");
23+
Scripts = SetResource("Serilog.Ui.Web.wwwroot.js.main.js");
24+
SelectListItems = new List<SelectListItem>
25+
{
26+
new SelectListItem {Text = "10", Value = "10"},
27+
new SelectListItem {Text = "25", Value = "25"},
28+
new SelectListItem {Text = "50", Value = "50"},
29+
new SelectListItem {Text = "100", Value = "100"}
30+
};
31+
}
32+
1633
public LogsController(IDataProvider dataProvider)
1734
{
1835
_dataProvider = dataProvider;
@@ -26,39 +43,31 @@ public async Task<IActionResult> Index(int page = 1, int count = 10)
2643
if (count > 100)
2744
count = 100;
2845

46+
var x = SelectListItems.FirstOrDefault(i => i.Value == count.ToString());
47+
if (x != null)
48+
x.Selected = true;
49+
else
50+
SelectListItems.First().Selected = true;
51+
2952
var (logs, logCount) = await _dataProvider.FetchDataAsync(page, count);
3053
var viewModel = new LogViewModel
3154
{
3255
LogCount = logCount,
3356
Logs = logs,
3457
Page = page,
35-
Count = count
58+
Count = count,
59+
CountSelectListItems = SelectListItems
3660
};
3761

38-
GetResources();
62+
ViewData["Styles"] = Styles;
63+
ViewData["Scripts"] = Scripts;
3964

4065
return View(viewModel);
4166
}
4267

43-
private void GetResources()
44-
{
45-
if (_styles != null)
46-
{
47-
ViewData["Styles"] = _styles;
48-
ViewData["Scripts"] = _scripts;
49-
return;
50-
}
51-
52-
_styles = SetResource("Serilog.Ui.Web.wwwroot.css.main.css");
53-
ViewData["Styles"] = _styles;
54-
55-
_scripts = SetResource("Serilog.Ui.Web.wwwroot.js.main.js");
56-
ViewData["Scripts"] = _scripts;
57-
}
58-
59-
private string SetResource(string resourceName)
68+
private static string SetResource(string resourceName)
6069
{
61-
var resourceStream = GetType().Assembly.GetManifestResourceStream(resourceName);
70+
var resourceStream = typeof(LogsController).Assembly.GetManifestResourceStream(resourceName);
6271
var resource = string.Empty;
6372

6473
using var reader = new StreamReader(resourceStream, Encoding.UTF8);

src/Serilog.Ui.Web/ViewModel/LogViewModel.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Serilog.Ui.Core;
1+
using Microsoft.AspNetCore.Mvc.Rendering;
2+
using Serilog.Ui.Core;
23
using System.Collections.Generic;
34

45
namespace Serilog.Ui.Web.ViewModel
@@ -12,5 +13,7 @@ public class LogViewModel
1213
public int Page { get; set; }
1314

1415
public int Count { get; set; }
16+
17+
public IEnumerable<SelectListItem> CountSelectListItems { get; set; }
1518
}
1619
}

src/Serilog.Ui.Web/Views/Logs/Index.cshtml

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,7 @@
1111
<div class="table-select">
1212
<label>
1313
Show
14-
<select name="count" class="form-control form-control-sm">
15-
<option value="10">10</option>
16-
<option value="25">25</option>
17-
<option value="50">50</option>
18-
<option value="100">100</option>
14+
<select asp-items="@Model.CountSelectListItems" name="count" class="form-control form-control-sm">
1915
</select>
2016
entries
2117
</label>
@@ -42,33 +38,33 @@
4238
<tbody>
4339
@foreach (var log in Model.Logs)
4440
{
45-
switch (log.Level)
46-
{
47-
case "Verbose":
48-
case "Debug":
49-
levelClass = "bg-success";
50-
break;
51-
case "Information":
52-
levelClass = "bg-primary";
53-
break;
54-
case "Warning":
55-
levelClass = "bg-warning";
56-
break;
57-
case "Error":
58-
levelClass = "bg-danger";
59-
break;
60-
}
41+
switch (log.Level)
42+
{
43+
case "Verbose":
44+
case "Debug":
45+
levelClass = "bg-success";
46+
break;
47+
case "Information":
48+
levelClass = "bg-primary";
49+
break;
50+
case "Warning":
51+
levelClass = "bg-warning";
52+
break;
53+
case "Error":
54+
levelClass = "bg-danger";
55+
break;
56+
}
6157

62-
<tr>
63-
<th scope="row">@log.Id</th>
64-
<td class="text-center"><span style="padding: 5px;" class="@levelClass text-white">@log.Level</span></td>
65-
<td class="text-center">@log.Timestamp</td>
66-
<td>
67-
<span class="overflow-auto"> @log.Message</span>
68-
</td>
69-
<td class="text-center">@log.Exception</td>
70-
<td class="text-center"><a href="#" title="Click to view">View</a></td>
71-
</tr>
58+
<tr>
59+
<th scope="row">@log.Id</th>
60+
<td class="text-center"><span style="padding: 5px;" class="@levelClass text-white">@log.Level</span></td>
61+
<td class="text-center">@log.Timestamp</td>
62+
<td>
63+
<span class="overflow-auto"> @log.Message</span>
64+
</td>
65+
<td class="text-center">@log.Exception</td>
66+
<td class="text-center"><a href="#" title="Click to view">View</a></td>
67+
</tr>
7268
}
7369
</tbody>
7470
</table>

src/Serilog.Ui.Web/Views/Logs/_Paging.cshtml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
{
1212
hasMore = true;
1313
}
14-
1514
}
1615
}
1716

0 commit comments

Comments
 (0)