/// <summary>
        /// 异步写操作日志
        /// </summary>
        /// <param name="sn"></param>
        /// <param name="operation"></param>
        /// <param name="message"></param>
        public static void RecordOperationAsync(string sn, string operation, string message)
        {
            var ip        = HttpHelper.GetRealIP();
            var @operator = GetCurrentOperator();
            var record    = new OperateRecordDTO()
            {
                Sn           = sn,
                UserId       = @operator.UserId == Guid.Empty ? (Guid?)null : @operator.UserId,
                OperatorName = @operator.OperatorName,
                Operation    = operation,
                OperateTime  = DateTime.UtcNow,
                Message      = message,
                Ip           = ip
            };

            if (_eventTimer == null)
            {
                lock (Lock)
                {
                    _eventTimer = new System.Threading.Timer(
                        new System.Threading.TimerCallback(SendLogRecords),
                        State,
                        new TimeSpan(0, 0, 0),
                        new TimeSpan(0, 0, SendIntervalInSecond));
                }
            }

            LogQueue.Enqueue(record);
        }
 private static void SendLogRecords(object sender)
 {
     try
     {
         OperateRecordDTO current = default(OperateRecordDTO);
         while (LogQueue.TryDequeue(out current))
         {
             if (current == null)
             {
                 break;
             }
             try
             {
                 SaveOperateRecordAsync(current);
             }
             catch (Exception ex)
             {
                 Logger.Error("", ex);
                 //LogQueue.Enqueue(current);
             }
         }
     }
     catch (Exception ex)
     {
         Logger.Error("", ex);
     }
 }
        private static void SaveOperateRecordAsync(OperateRecordDTO record)
        {
            try
            {
                var parameters = ToPostDictionary(record);

                var json = HttpHelper.HttpPostRequest(OperateRecordSaveUrl, parameters);

                if (!json.StartsWith("{"))
                {
                    throw new Exception("invalid json string");
                }

                var result = JsonConvert.DeserializeObject <WebApiResponseDTO <string> >(json);

                if (!result.Succeeded)
                {
                    throw new Exception("not succeeded, json string: " + json);
                }
            }
            catch (Exception ex)
            {
                var parameters = ToPostDictionary(record);
                var sb         = new StringBuilder("");
                sb.AppendFormat("{0}:{1}, ", "url", OperateRecordSaveUrl);
                foreach (var key in parameters.Keys)
                {
                    sb.AppendFormat("{0}:{1}, ", key, parameters[key]);
                }

                Logger.Error(String.Format("SaveOperateRecord Failed {0}", sb.ToString()), ex);
            }
        }
        public OperateRecordDTO Add(OperateRecordDTO operateRecordDTO, bool commit = true)
        {
            var record = operateRecordDTO.ToModel();

            record.Id = IdentityGenerator.NewSequentialGuid();
            if (record.OperateTime == DateTime.MinValue)
            {
                record.OperateTime = DateTime.UtcNow;
            }

            if (record.Message != null && record.Message.Length > 150)
            {
                var message = record.Message;
                record.Message = record.Message.Substring(0, 147) + "...";
                // 把message添加到Extend表
                var extend = new OperateRecordExtend()
                {
                    Id          = IdentityGenerator.NewSequentialGuid(),
                    LongMessage = message
                };
                record.ExtendId = extend.Id;
                _Repository.AddExtend(extend);
            }

            _Repository.Add(record);

            if (commit)
            {
                //commit the unit of work
                _Repository.UnitOfWork.Commit();
            }

            return(record.ToDto());
        }
        protected static Dictionary <String, String> ToPostDictionary(OperateRecordDTO record)
        {
            var timestamp = SecurityHelper.GetTimestampAsPhp(DateTime.UtcNow);

            return(new Dictionary <String, String>
            {
                { "timestamp", Convert.ToString(timestamp) },
                { "signature", SecurityHelper.Encrypt(Convert.ToString(timestamp), WebApiSecret) },
                { "Sn", record.Sn },
                { "UserId", record.UserId.HasValue ? record.UserId.ToString() : string.Empty },
                { "OperatorName", record.OperatorName },
                { "Operation", record.Operation },
                { "Message", record.Message },
                { "OperateTime", record.OperateTime.ToString("yyyy-MM-dd HH:mm:ss.fff") },
                { "Ip", record.Ip }
            });
        }
        /// <summary>
        /// 同步写操作日志
        /// </summary>
        /// <param name="sn"></param>
        /// <param name="operation"></param>
        /// <param name="message"></param>
        /// <param name="user"></param>
        /// <param name="commit"></param>
        public static void RecordOperation(string sn, string operation, string message,
                                           UserDTO user = null, bool commit = false)
        {
            if (sn.IsNullOrBlank())
            {
                throw new Exception("sn can not be empty");
            }

            if (sn == Guid.Empty.ToString())
            {
                throw new Exception("sn can not be Guid.Empty");
            }

            var         ip        = HttpHelper.GetRealIP();
            OperatorDTO @operator = null;

            if (user == null)
            {
                @operator = GetCurrentOperator();
            }
            else
            {
                @operator = new OperatorDTO()
                {
                    UserId       = user.Id,
                    OperatorName = user.Name
                };
            }

            var record = new OperateRecordDTO()
            {
                Sn           = sn,
                UserId       = @operator.UserId == Guid.Empty ? (Guid?)null : @operator.UserId,
                OperatorName = @operator.OperatorName,
                Operation    = operation,
                Message      = message,
                Ip           = ip
            };

            SaveOperateRecord(record, commit);
        }
 private static void SaveOperateRecord(OperateRecordDTO record, bool commit = true)
 {
     OperateRecordService.Add(record, commit);
 }
 public static OperateRecord ToModel(this OperateRecordDTO dto)
 {
     return(Mapper.Map <OperateRecord>(dto));
 }