/// <summary>
        /// 将带单位的大小转换为字节大小
        /// </summary>
        /// <param name="size">带单位大小</param>
        /// <param name="unit">单位</param>
        /// <returns></returns>
        public long ConvertToByteCountFromSizeAndUnit(SizeWithUnitInfo swui)
        {
            switch (swui.Unit)
            {
            case ByteUnit.B:
            {
                return((long)swui.Size);
            }

            case ByteUnit.KB:
            {
                return((long)swui.Size * 1024);
            }

            case ByteUnit.MB:
            {
                return((long)swui.Size * 1048576);
            }

            case ByteUnit.GB:
            {
                return((long)swui.Size * 1073741824);
            }

            default:
            {
                throw new Exception("不支持的单位换算!");
            }
            }
        }
Example #2
0
        /// <summary>
        /// 转换为带单位大小实体
        /// </summary>
        /// <param name="byteCount">字节数</param>
        /// <returns></returns>
        public SizeWithUnitInfo ConvertToSizeWithUnitInfoFromByteCount(long byteCount)
        {
            SizeWithUnitInfo swui = new SizeWithUnitInfo();

            swui.Bytes = byteCount;

            if (byteCount >= 1073741824)
            {
                swui.Size = double.Parse(String.Format("{0:##.##}", byteCount / 1073741824));
                swui.Unit = ByteUnit.GB;
            }
            else if (byteCount >= 1048576)
            {
                swui.Size = double.Parse(String.Format("{0:##.##}", byteCount / 1048576));
                swui.Unit = ByteUnit.MB;
            }
            else if (byteCount >= 1024)
            {
                swui.Size = double.Parse(String.Format("{0:##.##}", byteCount / 1024));
                swui.Unit = ByteUnit.KB;
            }
            else if (byteCount > 0 && byteCount < 1024)
            {
                swui.Size = byteCount;
                swui.Unit = ByteUnit.B;
            }

            return(swui);
        }
Example #3
0
        /// <summary>
        /// 添加待保存日志信息
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <param name="logMsg">日志消息</param>
        /// <param name="su">日志大小</param>
        /// <returns></returns>
        public string AddLogMsg(string filePath, string logMsg, SizeWithUnitInfo su)
        {
            //先添加日志文件大小限制信息
            LogFileHelper.Instance.AddLogFile(filePath, su);

            //再添加日志信息
            return(AddLogMsg(filePath, logMsg));
        }
Example #4
0
        public LogFileHelper()
        {
            LogFileList = new Dictionary <string, SizeWithUnitInfo>();

            //默认日志文件为1M
            this.sizeUnit      = new SizeWithUnitInfo();
            this.sizeUnit.Size = 5;
            this.sizeUnit.Unit = ByteUnit.MB;
        }
Example #5
0
        /// <summary>
        /// 换算单位
        /// </summary>
        /// <param name="byteCount">大小</param>
        /// <param name="by">单位</param>
        /// <returns></returns>
        public SizeWithUnitInfo ConvertToSizeWithUnitInfoFromByteCountAndUnit(long byteCount, ByteUnit by)
        {
            SizeWithUnitInfo swui = new SizeWithUnitInfo();

            swui.Bytes = byteCount;

            switch (by)
            {
            case ByteUnit.B:
            {
                swui.Size = byteCount;
                swui.Unit = ByteUnit.B;
                break;
            }

            case ByteUnit.KB:
            {
                swui.Size = double.Parse(String.Format("{0:##.##}", byteCount / 1024));
                swui.Unit = ByteUnit.KB;
                break;
            }

            case ByteUnit.MB:
            {
                swui.Size = double.Parse(String.Format("{0:##.##}", byteCount / 1048576));
                swui.Unit = ByteUnit.MB;
                break;
            }

            case ByteUnit.GB:
            {
                swui.Size = double.Parse(String.Format("{0:##.##}", byteCount / 1073741824));
                swui.Unit = ByteUnit.GB;
                break;
            }

            default:
            {
                throw new Exception("不支持的单位换算!");
            }
            }


            return(swui);
        }
Example #6
0
 /// <summary>
 /// 添加日志文件信息
 /// </summary>
 /// <param name="filePath">文件路径</param>
 /// <param name="su">带单位的大小信息</param>
 public void AddLogFile(string filePath, SizeWithUnitInfo su)
 {
     if (LogFileList != null && LogFileList.Count > 0)
     {
         if (LogFileList.ContainsKey(filePath))
         {
             LogFileList[filePath] = su;
         }
         else
         {
             LogFileList.Add(filePath, su);
         }
     }
     else
     {
         LogFileList.Add(filePath, su);
     }
 }
Example #7
0
 /// <summary>
 /// 记录日志(带文件大小限制)
 /// </summary>
 /// <param name="fileName">文件名</param>
 /// <param name="su">文件大小限制</param>
 /// <returns></returns>
 public string WriteLog(string fileName, SizeWithUnitInfo su)
 {
     return(LogMsgHelper.Instance.AddLogMsg(BuildFilePath(fileName), string.Empty, su));
 }
Example #8
0
 /// <summary>
 /// 记录日志(带文件大小限制)
 /// </summary>
 /// <param name="fileName">文件名</param>
 /// <param name="ex">异常</param>
 /// <param name="su">文件大小限制</param>
 /// <returns></returns>
 public string WriteLog(string fileName, Exception ex, SizeWithUnitInfo su)
 {
     return(LogMsgHelper.Instance.AddLogMsg(BuildFilePath(fileName), BuildLogWithTime(ex.ToString()), su));
 }
Example #9
0
 /// <summary>
 /// 记录日志(带文件大小限制)
 /// </summary>
 /// <param name="fileName">文件名</param>
 /// <param name="msg">日志</param>
 /// <param name="su">文件大小限制</param>
 /// <returns></returns>
 public string WriteLog(string fileName, string msg, SizeWithUnitInfo su)
 {
     return(LogMsgHelper.Instance.AddLogMsg(BuildFilePath(fileName), BuildLogWithTime(msg), su));
 }
Example #10
0
 /// <summary>
 /// 记录日志(带文件大小限制)
 /// </summary>
 /// <param name="su">文件大小限制</param>
 /// <returns></returns>
 public string WriteLog <T>(SizeWithUnitInfo su) where T : class
 {
     return(LogMsgHelper.Instance.AddLogMsg(BuildFilePath(typeof(T).ToString()), string.Empty, su));
 }
Example #11
0
 /// <summary>
 /// 记录日志(带文件大小限制)
 /// </summary>
 /// <param name="ex">异常</param>
 /// <param name="su">文件大小限制</param>
 /// <returns></returns>
 public string WriteLog <T>(Exception ex, SizeWithUnitInfo su) where T : class
 {
     return(LogMsgHelper.Instance.AddLogMsg(BuildFilePath(typeof(T).ToString()), BuildLogWithTime(ex.ToString()), su));
 }
Example #12
0
        /// <summary>
        /// 转换为带单位大小的字符串(例如:42M、2.3G)
        /// </summary>
        /// <param name="byteCount">字节数</param>
        /// <returns></returns>
        public string ConvertToSizeUnitStringFromByteCount(long byteCount)
        {
            SizeWithUnitInfo swui = ConvertToSizeWithUnitInfoFromByteCount(byteCount);

            return(swui.Size + " " + GetUnitName(swui.Unit));
        }