commit
111000c55e
|
@ -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
|
|||
/// </summary>
|
||||
public class PageContent : ComponentBase
|
||||
{
|
||||
/// <summary>
|
||||
/// 获得/设置 组件名字
|
||||
/// </summary>
|
||||
[Inject]
|
||||
protected IHttpContextAccessor? HttpContextAccessor { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 获得/设置 组件名字
|
||||
/// </summary>
|
||||
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// HttpContextExtensions 扩展方法
|
||||
/// </summary>
|
||||
public static class HttpContextExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// 保存访问日志方法
|
||||
/// </summary>
|
||||
public static void SaveOnlineUser(this HttpContext context, string url)
|
||||
{
|
||||
var onlineUserSvr = context.RequestServices.GetRequiredService<IOnlineUsers>();
|
||||
var locator = context.RequestServices.GetRequiredService<IIPLocatorProvider>();
|
||||
var proxy = new Func<AutoExpireCacheEntry<OnlineUser>, Action?, AutoExpireCacheEntry<OnlineUser>>((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<OnlineUser>(v, 1000 * 60, __ => onlineUserSvr.TryRemove(key, out _)), null);
|
||||
}, (key, v) => proxy(v, () => v.Reset()));
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue