RunLogUtil.cs 9.2 KB
 
    using log4net;
    using Newtonsoft.Json;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.Text;

    namespace OnlineStore.Common
    {
        public class RunLogUtil
        {
            public static readonly ILog RunLog = LogManager.GetLogger("RunLog");

            public static void ErrorLog(ErrorLog log)
            {
                string jsonStr = JsonHelper.SerializeObject(log);
                RunLog.Error(jsonStr);
            }
            public static void MoveLog(MoveLog log)
            {
                if (log == null || (!log.IsValid()))
                {
                    return;
                }
                string jsonStr = JsonHelper.SerializeObject(log);
                RunLog.Info(jsonStr);
            }
            public static void InoutEndLog(InoutEndLog log)
            {
                if (log == null || (!log.IsValid()))
                {
                    return;
                }
                string jsonStr = JsonHelper.SerializeObject(log);
                RunLog.Info(jsonStr);
            }
            public static void AxisLog(AxisMoveLog log)
            {
                string jsonStr = JsonHelper.SerializeObject(log);
                RunLog.Error(jsonStr);
            }
        }
        public class BaseLog
        {
            [JsonProperty(Order = 0)]
            public string Name { get; set; } = "三楼料仓";
            [JsonProperty(Order = 1)]
            public string DeviceName { get; set; } = "";
            [JsonProperty(Order = 2)]
            public string LogType { get; set; } = "";
            [JsonProperty(Order = 3)]
            public string StartTime { get; set; } = "";
            [JsonProperty(Order = 4)]
            public string EndTime { get; set; } = "";
            [JsonProperty(Order = 5)]
            public string timeSpan { get; set; } = "";
        }

        public class ErrorLog : BaseLog
        {
            public ErrorLog()
            {
                this.LogType = "Error";
            }
            public ErrorLog(string deviceName, string errType, string errMsg, DateTime startTime, DateTime endTime, string operType = "", string posid = "", string barcode = "")
            {
                if (errType == null) { errType = ""; }
                if (errMsg == null) { errMsg = ""; }
                if (operType == null) { operType = ""; }
                if (posid == null) { posid = ""; }
                if (barcode == null) { barcode = ""; }
                this.LogType = "Error";
                this.DeviceName = deviceName.Trim();
                this.ErrorType = errType.Trim();
                this.ErrorMsg = errMsg.Trim();
                this.StartTime = startTime.ToString("yyyy-MM-dd HH:mm:ss");
                this.EndTime = endTime.ToString("yyyy-MM-dd HH:mm:ss");
                this.OperateType = operType.Trim();
                this.PosId = posid.Trim();
                this.Barcode = barcode.Trim();

                TimeSpan span = endTime - startTime;
                this.timeSpan = (Math.Round(span.TotalMinutes, 2)).ToString();//两位小数的分钟
            }
            public bool IsValid()
            {
                if (string.IsNullOrEmpty(DeviceName) || String.IsNullOrEmpty(LogType) || string.IsNullOrEmpty(ErrorType) || string.IsNullOrEmpty(ErrorMsg))
                {
                    return false;
                }
                return true;
            }


            [JsonProperty(Order = 11)]
            public string ErrorType { get; set; } = "";

            [JsonProperty(Order = 12)]
            public string ErrorMsg { get; set; } = "";
            [JsonProperty(Order = 13)]
            public string OperateType { get; set; } = "";
            [JsonProperty(Order = 14)]
            public string PosId { get; set; } = "";
            [JsonProperty(Order = 15)]
            public string Barcode { get; set; } = "";

        }
        public class MoveLog : BaseLog
        {
            public MoveLog()
            {
                this.LogType = "Running";
            }
            public MoveLog(string deviceName, string moveType, string moveMsg, DateTime startTime, DateTime endTime, string posid = "", string barcode = "")
            {
                if (moveMsg == null) { moveMsg = ""; }
                if (posid == null) { posid = ""; }
                if (barcode == null) { barcode = ""; }
                this.LogType = "Running";
                this.DeviceName = deviceName.Replace("-Move", "").Replace("-SMove", "").Trim();
                this.MoveType = moveType.Trim();
                this.MoveMsg = moveMsg.Trim();
                this.StartTime = startTime.ToString("yyyy-MM-dd HH:mm:ss");
                this.EndTime = endTime.ToString("yyyy-MM-dd HH:mm:ss");
                this.PosId = posid.Trim();
                this.Barcode = barcode.Trim();

                TimeSpan span = endTime - startTime;
                this.timeSpan = (Math.Round(span.TotalMinutes, 2)).ToString();//两位小数的分钟
            }
            public bool IsValid()
            {
                if (string.IsNullOrEmpty(DeviceName) || String.IsNullOrEmpty(LogType) || string.IsNullOrEmpty(MoveType) || string.IsNullOrEmpty(MoveMsg))
                {
                    return false;
                }
                return true;
            }

            [JsonProperty(Order = 11)]
            public string MoveType { get; set; } = "";
            [JsonProperty(Order = 12)]
            public string MoveMsg { get; set; } = "";
            [JsonProperty(Order = 13)]

            public string PosId { get; set; } = "";
            [JsonProperty(Order = 14)]
            public string Barcode { get; set; } = "";

        }
        public class InoutEndLog : BaseLog
        {
            public InoutEndLog()
            {

                this.LogType = "InoutEnd";
            }
            public InoutEndLog(string deviceName, string moveType, DateTime startTime, DateTime endTime, string posid = "", string barcode = "")
            {
                if (posid == null) { posid = ""; }
                if (barcode == null) { barcode = ""; }
                this.LogType = "InoutEnd";
                this.DeviceName = deviceName.Replace("-Move", "").Replace("-SMove", "").Trim();
                this.MoveType = moveType.Trim();
                this.StartTime = startTime.ToString("yyyy-MM-dd HH:mm:ss");
                this.EndTime = endTime.ToString("yyyy-MM-dd HH:mm:ss");
                this.PosId = posid.Trim();
                this.Barcode = barcode.Trim();

                TimeSpan span = endTime - startTime;
                this.timeSpan = (Math.Round(span.TotalMinutes, 2)).ToString();//两位小数的分钟
            }
            public bool IsValid()
            {
                if (string.IsNullOrEmpty(DeviceName) || string.IsNullOrEmpty(MoveType))
                {
                    return false;
                }
                return true;
            }
            [JsonProperty(Order = 11)]
            public string MoveType { get; set; } = "";
            [JsonProperty(Order = 12)]
            public string PosId { get; set; } = "";
            [JsonProperty(Order = 13)]
            public string Barcode { get; set; } = "";


        }

        public class AxisMoveLog : BaseLog
        {

            public AxisMoveLog()
            {

                this.LogType = "Axis";
            }
            public AxisMoveLog(string deviceName, string axisName, string moveType, int targetP, int speed, DateTime startTime, DateTime endTime, string posid = "", string barcode = "")
            {
                if (moveType == null) { moveType = ""; }
                if (posid == null) { posid = ""; }
                if (barcode == null) { barcode = ""; }
                this.LogType = "Axis";
                this.DeviceName = deviceName.Replace("-Move", "").Replace("-SMove", "").Trim();
                this.MoveType = moveType.Trim();
                this.AxisName = axisName;
                this.TargetPos = targetP;
                this.StartTime = startTime.ToString("yyyy-MM-dd HH:mm:ss");
                this.EndTime = endTime.ToString("yyyy-MM-dd HH:mm:ss");
                this.PosId = posid.Trim();
                this.Barcode = barcode.Trim();

                TimeSpan span = endTime - startTime;
                this.timeSpan = (Math.Round(span.TotalMinutes, 2)).ToString();//两位小数的分钟
                this.Speed = speed;
            }
            public bool IsValid()
            {
                if (string.IsNullOrEmpty(DeviceName) || string.IsNullOrEmpty(MoveType))
                {
                    return false;
                }
                return true;
            }
            [JsonProperty(Order = 11)]

            public string AxisName { get; set; } = "";
            [JsonProperty(Order = 12)]
            public string MoveType { get; set; } = "";
            [JsonProperty(Order = 13)]
            public int TargetPos { get; set; } = 0;
            [JsonProperty(Order = 14)]
            public int Speed { get; set; }
            [JsonProperty(Order = 15)]
            public string PosId { get; set; } = "";
            [JsonProperty(Order = 16)]
            public string Barcode { get; set; } = "";
        }

    }