You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
2.7 KiB
C#

8 months ago
using System.Net;
using System.Net.Sockets;
using IPTools.Core;
using Microsoft.AspNetCore.Http;
using NPin.Framework.Core.Extensions;
using NPin.Framework.Upms.Domain.Entities;
using NPin.Framework.Upms.Domain.Shared.OperLog;
using SqlSugar;
using UAParser;
using Volo.Abp.Auditing;
using Volo.Abp.Domain.Entities;
namespace NPin.Framework.Upms.Domain.OperLog;
[SugarTable("OperationLog", "操作日志记录表")]
public class OperationLogEntity: Entity<Guid>, ICreationAuditedObject
{
[SugarColumn(IsPrimaryKey = true)]
public override Guid Id { get; protected set; }
[SugarColumn(ColumnDescription = "日志标题")]
public string? Title { get; set; }
[SugarColumn(ColumnDescription = "操作类型")]
public OperTypeEnum OperType { get; set; }
[SugarColumn(ColumnDescription = "请求方式")]
public string? RequestMethod { get; set; }
[SugarColumn(ColumnDescription = "操作者")]
public string? OperUser { get; set; }
[SugarColumn(ColumnDescription = "操作者Ipv4")]
public string? OperIPv4 { get; set; }
[SugarColumn(ColumnDescription = "操作者Ipv6")]
public string? OperIPv6 { get; set; }
[SugarColumn(ColumnDescription = "操作地点")]
public string? OperLocation { get; set; }
[SugarColumn(ColumnDescription = "操作方法")]
public string? Method { get; set; }
[SugarColumn(ColumnDescription = "请求参数")]
public string? RequestParam { get; set; }
[SugarColumn(ColumnDescription = "请求结果", Length = 9999)]
public string? RequestResult { get; set; }
public DateTime CreationTime { get; }
public Guid? CreatorId { get; }
public static OperationLogEntity GetInfoByHttpContext(HttpContext httpContext)
{
// var ipInfo = httpContext.GetRemoteIpInfo();
string ipv4AddrStr = null;
string ipv6AddrStr = null;
IpInfo info = new IpInfo { Province = "本地", City = "本机" };
var ipAddr = httpContext.GetClientIpAddress();
if (ipAddr != null)
{
switch (ipAddr.AddressFamily)
{
case AddressFamily.InterNetwork:
ipv4AddrStr = ipAddr.ToString();
break;
case AddressFamily.InterNetworkV6:
ipv6AddrStr = ipAddr.ToString();
break;
}
if (!IPAddress.IsLoopback(ipAddr))
{
info = IpTool.Search(ipAddr.ToString());
}
}
return new OperationLogEntity
{
OperIPv4 = ipv4AddrStr,
OperIPv6 = ipv6AddrStr,
OperLocation = $"{info.Country}-{info.Province}-{info.City}"
};
}
}