写入登录日志

This commit is contained in:
大石头 2020-01-11 15:36:04 +08:00
parent a1a489aefa
commit de8a4a03bd
5 changed files with 115 additions and 78 deletions

View File

@ -1,25 +1,11 @@
using System;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Xml.Serialization;
using NewLife;
using NewLife.Data;
using NewLife.Log;
using NewLife.Model;
using NewLife.Reflection;
using NewLife.Threading;
using NewLife.Web;
using XCode;
using XCode.Cache;
using XCode.Configuration;
using XCode.DataAccessLayer;
using XCode.Membership;
namespace AntJob.Data.Entity
@ -50,60 +36,20 @@ namespace AntJob.Data.Entity
//if (isNew && !Dirtys[nameof(CreateTime)]) CreateTime = DateTime.Now;
//if (isNew && !Dirtys[nameof(CreateIP)]) CreateIP = ManageProvider.UserHost;
}
///// <summary>首次连接数据库时初始化数据,仅用于实体类重载,用户不应该调用该方法</summary>
//[EditorBrowsable(EditorBrowsableState.Never)]
//protected override void InitData()
//{
// // InitData一般用于当数据表没有数据时添加一些默认数据该实体类的任何第一次数据库操作都会触发该方法默认异步调用
// if (Meta.Session.Count > 0) return;
// if (XTrace.Debug) XTrace.WriteLine("开始初始化AppHistory[应用历史]数据……");
// var entity = new AppHistory();
// entity.ID = 0;
// entity.AppID = 0;
// entity.Name = "abc";
// entity.Action = "abc";
// entity.Success = true;
// entity.Version = "abc";
// entity.CompileTime = DateTime.Now;
// entity.Creator = "abc";
// entity.CreateTime = DateTime.Now;
// entity.CreateIP = "abc";
// entity.Remark = "abc";
// entity.Insert();
// if (XTrace.Debug) XTrace.WriteLine("完成初始化AppHistory[应用历史]数据!");
//}
///// <summary>已重载。基类先调用Valid(true)验证数据然后在事务保护内调用OnInsert</summary>
///// <returns></returns>
//public override Int32 Insert()
//{
// return base.Insert();
//}
///// <summary>已重载。在事务保护范围内处理业务位于Valid之后</summary>
///// <returns></returns>
//protected override Int32 OnDelete()
//{
// return base.OnDelete();
//}
#endregion
#region
/// <summary>应用</summary>
[XmlIgnore, IgnoreDataMember]
//[ScriptIgnore]
public App App { get { return Extends.Get(nameof(App), k => App.FindByID(AppID)); } }
public App App => Extends.Get(nameof(App), k => App.FindByID(AppID));
/// <summary>应用</summary>
[XmlIgnore, IgnoreDataMember]
//[ScriptIgnore]
[DisplayName("应用")]
[Map(__.AppID, typeof(App), "ID")]
public String AppName { get { return App?.Name; } }
public String AppName => App?.Name;
#endregion
#region
@ -139,7 +85,78 @@ namespace AntJob.Data.Entity
#region
#endregion
#region
#region
/// <summary>高级搜索</summary>
/// <param name="appid"></param>
/// <param name="action"></param>
/// <param name="success"></param>
/// <param name="start"></param>
/// <param name="end"></param>
/// <param name="key"></param>
/// <param name="page"></param>
/// <returns></returns>
public static IList<AppHistory> Search(Int32 appid, String action, Boolean? success, DateTime start, DateTime end, String key, PageParameter page)
{
var exp = new WhereExpression();
if (appid >= 0) exp &= _.AppID == appid;
if (!action.IsNullOrEmpty()) exp &= _.Action == action;
if (success != null) exp &= _.Success == success;
exp &= _.CreateTime.Between(start, end);
if (!key.IsNullOrEmpty())
{
exp &= (_.Name.Contains(key) | _.Remark.Contains(key) | _.CreateIP.Contains(key));
}
return FindAll(exp, page);
}
#endregion
#region
/// <summary>类别名实体缓存异步缓存10分钟</summary>
static readonly FieldCache<AppHistory> ActionCache = new FieldCache<AppHistory>(_.Action);
/// <summary>获取所有类别名称</summary>
/// <returns></returns>
public static IDictionary<String, String> FindAllActionName() => ActionCache.FindAllName();
#endregion
#region
/// <summary>创建日志</summary>
/// <param name="app"></param>
/// <param name="action"></param>
/// <param name="success"></param>
/// <param name="remark"></param>
/// <param name="creator"></param>
/// <param name="ip"></param>
/// <returns></returns>
public static AppHistory Create(IApp app, String action, Boolean success, String remark, String creator, String ip)
{
if (app == null) app = new App();
var hi = new AppHistory
{
AppID = app.ID,
Name = app.Name,
Action = action,
Success = success,
Server = creator,
Version = app.Version,
CompileTime = app.CompileTime,
Remark = remark,
CreateTime = DateTime.Now,
CreateIP = ip,
};
hi.SaveAsync();
return hi;
}
#endregion
}
}

View File

@ -73,13 +73,13 @@ namespace AntJob.Data.Entity
[BindColumn("CompileTime", "编译时间", "")]
public DateTime CompileTime { get { return _CompileTime; } set { if (OnPropertyChanging(__.CompileTime, value)) { _CompileTime = value; OnPropertyChanged(__.CompileTime); } } }
private String _Creator;
/// <summary>创建者。服务端节点</summary>
[DisplayName("创建者")]
[Description("创建者。服务端节点")]
private String _Server;
/// <summary>服务端。客户端登录到哪个服务端IP加端口</summary>
[DisplayName("服务端")]
[Description("服务端。客户端登录到哪个服务端IP加端口")]
[DataObjectField(false, false, true, 50)]
[BindColumn("Creator", "创建者。服务端节点", "")]
public String Creator { get { return _Creator; } set { if (OnPropertyChanging(__.Creator, value)) { _Creator = value; OnPropertyChanged(__.Creator); } } }
[BindColumn("Server", "服务端。客户端登录到哪个服务端IP加端口", "")]
public String Server { get { return _Server; } set { if (OnPropertyChanging(__.Server, value)) { _Server = value; OnPropertyChanged(__.Server); } } }
private DateTime _CreateTime;
/// <summary>创建时间</summary>
@ -123,7 +123,7 @@ namespace AntJob.Data.Entity
case __.Success : return _Success;
case __.Version : return _Version;
case __.CompileTime : return _CompileTime;
case __.Creator : return _Creator;
case __.Server : return _Server;
case __.CreateTime : return _CreateTime;
case __.CreateIP : return _CreateIP;
case __.Remark : return _Remark;
@ -141,7 +141,7 @@ namespace AntJob.Data.Entity
case __.Success : _Success = value.ToBoolean(); break;
case __.Version : _Version = Convert.ToString(value); break;
case __.CompileTime : _CompileTime = value.ToDateTime(); break;
case __.Creator : _Creator = Convert.ToString(value); break;
case __.Server : _Server = Convert.ToString(value); break;
case __.CreateTime : _CreateTime = value.ToDateTime(); break;
case __.CreateIP : _CreateIP = Convert.ToString(value); break;
case __.Remark : _Remark = Convert.ToString(value); break;
@ -176,8 +176,8 @@ namespace AntJob.Data.Entity
/// <summary>编译时间</summary>
public static readonly Field CompileTime = FindByName(__.CompileTime);
/// <summary>创建者。服务端节点</summary>
public static readonly Field Creator = FindByName(__.Creator);
/// <summary>服务端。客户端登录到哪个服务端IP加端口</summary>
public static readonly Field Server = FindByName(__.Server);
/// <summary>创建时间</summary>
public static readonly Field CreateTime = FindByName(__.CreateTime);
@ -215,8 +215,8 @@ namespace AntJob.Data.Entity
/// <summary>编译时间</summary>
public const String CompileTime = "CompileTime";
/// <summary>创建者。服务端节点</summary>
public const String Creator = "Creator";
/// <summary>服务端。客户端登录到哪个服务端IP加端口</summary>
public const String Server = "Server";
/// <summary>创建时间</summary>
public const String CreateTime = "CreateTime";
@ -255,8 +255,8 @@ namespace AntJob.Data.Entity
/// <summary>编译时间</summary>
DateTime CompileTime { get; set; }
/// <summary>创建者。服务端节点</summary>
String Creator { get; set; }
/// <summary>服务端。客户端登录到哪个服务端IP加端口</summary>
String Server { get; set; }
/// <summary>创建时间</summary>
DateTime CreateTime { get; set; }

View File

@ -63,7 +63,7 @@
<Column Name="Success" DataType="Boolean" Description="成功" />
<Column Name="Version" DataType="String" Description="版本" />
<Column Name="CompileTime" DataType="DateTime" Description="编译时间" />
<Column Name="Creator" DataType="String" Description="创建者。服务端节点" />
<Column Name="Server" DataType="String" Description="服务端。客户端登录到哪个服务端IP加端口" />
<Column Name="CreateTime" DataType="DateTime" Description="创建时间" />
<Column Name="CreateIP" DataType="String" Description="创建地址" />
<Column Name="Remark" ColumnName="Content" DataType="String" Length="2000" Description="内容" />

View File

@ -28,7 +28,7 @@ namespace AntJob.Server
public IApiSession Session { get; set; }
/// <summary>
/// 传入应用名和密钥登
/// 传入应用名和密钥登
/// 返回应用名和应用显示名
/// </summary>
/// <param name="user">应用名</param>
@ -44,6 +44,7 @@ namespace AntJob.Server
var machine = ps["machine"] + "";
var pid = ps["processid"].ToInt();
var ver = ps["version"] + "";
var compile = ps["Compile"].ToDateTime();
var ns = Session as INetSession;
var ip = ns.Remote.Host;
@ -70,15 +71,21 @@ namespace AntJob.Server
// 版本和编译时间
if (app.Version.IsNullOrEmpty() || app.Version.CompareTo(ver) < 0) app.Version = ver;
if (app.CompileTime < compile) app.CompileTime = compile;
app.Save();
// 应用上线
CreateOnline(app, ns, machine, pid, ver);
var online = CreateOnline(app, ns, machine, pid);
online.Version = ver;
online.CompileTime = compile;
online.Save();
// 记录当前用户
Session["App"] = app;
WriteHistory("登录", true, $"[{user}/{pass}]登录[{app}]成功");
return new
{
app.Name,
@ -625,7 +632,7 @@ namespace AntJob.Server
return olts.Select(e => e.ToModel()).ToArray();
}
AppOnline CreateOnline(IApp app, INetSession ns, String machine, Int32 pid, String version)
AppOnline CreateOnline(IApp app, INetSession ns, String machine, Int32 pid)
{
var ip = ns.Remote.Host;
@ -633,10 +640,10 @@ namespace AntJob.Server
online.Client = $"{(ip.IsNullOrEmpty() ? machine : ip)}@{pid}";
online.Name = machine;
online.UpdateIP = ip;
online.Version = version;
//online.Version = version;
online.Server = Local + "";
online.Save();
//online.Save();
// 真正的用户
Session["AppOnline"] = online;
@ -673,6 +680,17 @@ namespace AntJob.Server
}
#endregion
#region
void WriteHistory(String action, Boolean success, String remark)
{
var app = Session["App"] as App;
var ns = Session as INetSession;
var ip = ns.Remote.Host;
AppHistory.Create(app, action, success, remark, Local + "", ip);
}
#endregion
#region
/// <summary>日志</summary>
public static ILog Log { get; set; }

View File

@ -4,6 +4,7 @@ using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using AntJob.Data;
using NewLife;
using NewLife.Log;
using NewLife.Net;
using NewLife.Reflection;
@ -79,6 +80,7 @@ namespace AntJob.Providers
machine = Environment.MachineName,
processid = Process.GetCurrentProcess().Id,
version = asmx?.Version,
asmx?.Compile,
};
var rs = await base.InvokeWithClientAsync<Object>(client, "Login", arg);