From 45e4d734fbe7b631ae5aa5d19855a75ba863ebb6 Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Tue, 28 Jan 2020 10:14:28 +0800 Subject: [PATCH] =?UTF-8?q?feat(#I18PRL):=20Blazor=20=E6=A8=A1=E5=BC=8F?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=AE=BF=E9=97=AE=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #Issue link https://gitee.com/LongbowEnterprise/dashboard/issues?id=I18PRL --- .../Bootstrap.Admin/Components/PageContent.cs | 10 ++++ .../Extensions/HttpContextExtensions.cs | 52 +++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 src/admin/Bootstrap.Admin/Extensions/HttpContextExtensions.cs 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())); + } + } +}