using System; using System.Collections.Generic; using System.Text; using CYQ.Data.Orm; namespace CYQ.Data { /// /// A class for you to Write log to database /// 日志记录到数据库(需要配置LogConn链接后方有效) /// public partial class SysLogs : SimpleOrmBase { public SysLogs() { if (!string.IsNullOrEmpty(AppConfig.Log.Conn)) { base.SetInit(this, AppConfig.Log.TableName, AppConfig.Log.Conn); } else { IsWriteToTxt = true; } } private int _ID; /// /// 标识主键 /// [Key(true, true, false)] public int ID { get { return _ID; } set { _ID = value; } } private string _LogType; /// /// 日志类型 /// [Length(50)] public string LogType { get { return _LogType ?? ""; } set { _LogType = value; } } private string _TraceID; /// /// 分布式追踪ID /// [Length(50)] public string TraceID { get { return _TraceID ?? ""; } set { _TraceID = value; } } private string _HostName; /// /// 请求的主机名称(系统默认读取主机名) /// [Length(100)] public string HostName { get { if (string.IsNullOrEmpty(_HostName)) { _HostName = LocalEnvironment.HostName; } return _HostName; } set { _HostName = value; } } private string _Host; /// /// 请求的主机IP(系统默认读取主机内网IP,若无则返回主机名) /// [Length(100)] public string Host { get { if (string.IsNullOrEmpty(_Host)) { _Host = LocalEnvironment.HostIP; } return _Host; } set { _Host = value; } } private string _HttpMethod; /// /// 请求方法 /// [Length(10)] public string HttpMethod { get { return _HttpMethod; } set { _HttpMethod = value; } } private string _ClientIP; /// /// 客户端请求IP /// [Length(50)] public string ClientIP { get { return _ClientIP; } set { _ClientIP = value == "::1" ? "127.0.0.1" : value; } } private string _RequestUrl; /// /// 请求的地址 /// [Length(500)] public string RequestUrl { get { return _RequestUrl; } set { _RequestUrl = value; } } private string _RefererUrl; /// /// 请求的地址 /// [Length(500)] public string RefererUrl { get { return _RefererUrl; } set { _RefererUrl = value; } } private string _Message; /// /// 日志内容 /// [Length(2000)] public string Message { get { return _Message ?? ""; } set { _Message = value; } } private DateTime _CreateTime; /// /// 创建时间 /// public DateTime CreateTime { get { if (_CreateTime == DateTime.MinValue) { _CreateTime = DateTime.Now; } return _CreateTime; } set { _CreateTime = value; } } } public partial class SysLogs { /// /// 将日志写入到队列中待线程执行。 /// public void Write() { if (!AppConfig.Log.IsEnable) { Error.Throw("Error : " + LogType + " : " + Message); } LogWorker.Add(this); } /// /// 是否写到文本中 /// internal bool IsWriteToTxt = false; /// /// 获取文本格式的日志内容 /// /// internal string GetFormatterText() { string title = string.Format("V{0} Record On : {1} : {2} {3}", AppConfig.Version, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), HttpMethod, RequestUrl ?? "");// + Log.Url; if (!string.IsNullOrEmpty(ClientIP)) { title += " - " + ClientIP; } if (!string.IsNullOrEmpty(TraceID)) { title += AppConst.NewLine + AppConst.NewLine + "TraceID : " + TraceID; } if (!string.IsNullOrEmpty(RefererUrl)) { if (string.IsNullOrEmpty(TraceID)) { title += AppConst.NewLine; } title += AppConst.NewLine + "Referer : " + RefererUrl; } string body = title + AppConst.NewLine + AppConst.NewLine + Message.Replace("
", AppConst.NewLine) + AppConst.NewLine + AppConst.NewLine; body += "---------------------------------------" + AppConst.NewLine + AppConst.NewLine; return body; } } }