diff --git a/src/admin/Bootstrap.Admin/Components/PageContent.cs b/src/admin/Bootstrap.Admin/Components/PageContent.cs
index bd179f71..31444856 100644
--- a/src/admin/Bootstrap.Admin/Components/PageContent.cs
+++ b/src/admin/Bootstrap.Admin/Components/PageContent.cs
@@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Rendering;
+using Microsoft.AspNetCore.Http;
using System;
namespace Bootstrap.Admin.Components
@@ -9,6 +10,12 @@ namespace Bootstrap.Admin.Components
///
public class PageContent : ComponentBase
{
+ ///
+ /// 获得/设置 组件名字
+ ///
+ [Inject]
+ protected IHttpContextAccessor? HttpContextAccessor { get; set; }
+
///
/// 获得/设置 组件名字
///
@@ -27,6 +34,9 @@ namespace Bootstrap.Admin.Components
var t = Type.GetType($"Bootstrap.Admin{name}");
if (t != null)
{
+ // 访问日志
+ if (HttpContextAccessor != null) HttpContextAccessor.HttpContext.SaveOnlineUser(Name);
+
builder.OpenComponent(0, t);
builder.CloseComponent();
}
diff --git a/src/admin/Bootstrap.Admin/Extensions/HttpContextExtensions.cs b/src/admin/Bootstrap.Admin/Extensions/HttpContextExtensions.cs
new file mode 100644
index 00000000..441757d9
--- /dev/null
+++ b/src/admin/Bootstrap.Admin/Extensions/HttpContextExtensions.cs
@@ -0,0 +1,52 @@
+using System;
+using System.Net;
+using Bootstrap.DataAccess;
+using Longbow.Cache;
+using Longbow.Web;
+using Microsoft.Extensions.DependencyInjection;
+
+namespace Microsoft.AspNetCore.Http
+{
+ ///
+ /// HttpContextExtensions 扩展方法
+ ///
+ public static class HttpContextExtensions
+ {
+ ///
+ /// 保存访问日志方法
+ ///
+ public static void SaveOnlineUser(this HttpContext context, string url)
+ {
+ var onlineUserSvr = context.RequestServices.GetRequiredService();
+ var locator = context.RequestServices.GetRequiredService();
+ var proxy = new Func, Action?, AutoExpireCacheEntry>((c, action) =>
+ {
+ var v = c.Value;
+ v.LastAccessTime = DateTime.Now;
+ v.Method = context.Request.Method;
+ v.RequestUrl = url;
+ v.AddRequestUrl(url);
+ action?.Invoke();
+ TraceHelper.Save(context, v);
+ return c;
+ });
+ onlineUserSvr.AddOrUpdate(context.Connection.Id ?? "", key =>
+ {
+ var agent = context.Request.Headers["User-Agent"];
+ var userAgent = string.IsNullOrEmpty(agent) ? null : new UserAgent(agent);
+ var v = new OnlineUser
+ {
+ UserAgent = agent,
+ ConnectionId = key,
+ Ip = context.Connection.RemoteIpAddress.ToIPv4String(),
+ Browser = userAgent == null ? "Unknown" : $"{userAgent.Browser?.Name} {userAgent.Browser?.Version}",
+ OS = userAgent == null ? "Unknown" : $"{userAgent.OS?.Name} {userAgent.OS?.Version}",
+ FirstAccessTime = DateTime.Now,
+ Referer = context.Request.Headers["Referer"]
+ };
+ v.Location = locator?.Locate(v.Ip) ?? "";
+ return proxy(new AutoExpireCacheEntry(v, 1000 * 60, __ => onlineUserSvr.TryRemove(key, out _)), null);
+ }, (key, v) => proxy(v, () => v.Reset()));
+ }
+ }
+}