feat: 增加访问日志功能

This commit is contained in:
Argo-Tianyi 2022-01-17 12:34:20 +08:00
parent 2892a2780f
commit 7fd4a83b52
1 changed files with 68 additions and 1 deletions

View File

@ -4,13 +4,14 @@ using BootstrapAdmin.Web.Core;
using BootstrapAdmin.Web.Extensions;
using BootstrapAdmin.Web.Services;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Components.Routing;
namespace BootstrapAdmin.Web.Shared
{
/// <summary>
/// MainLayout 布局类
/// </summary>
public partial class MainLayout
public partial class MainLayout : IDisposable
{
private IEnumerable<MenuItem>? MenuItems { get; set; }
@ -42,6 +43,25 @@ namespace BootstrapAdmin.Web.Shared
[NotNull]
private ToastService? ToastService { get; set; }
[Inject]
[NotNull]
private ITrace? TraceService { get; set; }
/// <summary>
///
/// </summary>
[Inject]
[NotNull]
private WebClientService? WebClientService { get; set; }
[Inject]
[NotNull]
private NavigationManager? NavigationManager { get; set; }
[Inject]
[NotNull]
private BootstrapAppContext? AppContext { get; set; }
private string? Title { get; set; }
private string? Footer { get; set; }
@ -57,6 +77,35 @@ namespace BootstrapAdmin.Web.Shared
[NotNull]
private string? Icon { get; set; }
/// <summary>
///
/// </summary>
protected override void OnInitialized()
{
base.OnInitialized();
NavigationManager.LocationChanged += NavigationManager_LocationChanged;
}
private void NavigationManager_LocationChanged(object? sender, LocationChangedEventArgs e)
{
_ = Task.Run(async () =>
{
var clientInfo = await WebClientService.GetClientInfo();
TraceService.Log(new Trace
{
Browser = clientInfo.Browser,
City = clientInfo.City,
Ip = clientInfo.Ip,
LogTime = DateTime.Now,
OS = clientInfo.OS,
UserAgent = clientInfo.UserAgent,
RequestUrl = e.Location,
UserName = AppContext.UserName
});
});
}
/// <summary>
/// OnInitializedAsync 方法
/// </summary>
@ -93,5 +142,23 @@ namespace BootstrapAdmin.Web.Shared
logger.LogError(ex, "ErrorLogger");
}
private void Dispose(bool disposing)
{
if (disposing)
{
NavigationManager.LocationChanged -= NavigationManager_LocationChanged;
}
}
/// <summary>
///
/// </summary>
/// <exception cref="NotImplementedException"></exception>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
}