欢迎莅临 SUN WU GANG 的园子!!!

世上无难事,只畏有心人。有心之人,即立志之坚午也,志坚则不畏事之不成。

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

LvLogHelper.GetInstance(this.lbLog).PrintLog("初始化程序完成");
LvLogHelper.GetInstance().PrintLog("请连接综测仪表");
LvLogHelper.GetInstance().PrintLog("请加载测试用例");

using System;
using System.IO;
using System.Runtime.CompilerServices;
using System.Windows.Forms;
[assembly: SuppressIldasmAttribute()]

namespace LogHelper
{
    public class LvLogHelper
    {
        private ListBox LogBox;
        private LvLogHelper(ListBox logbox)
        {
            this.LogBox = logbox;
        }

        //定义一个用于保存静态变量的实例
        private static LvLogHelper instance = null;
        //定义一个保证线程同步的标识
        private static readonly object locker = new object();
        //构造函数为私有,使外界不能创建该类的实例
        private LvLogHelper() { }

        public static LvLogHelper GetInstance(ListBox lb)
        {
            if (instance == null)
            {
                lock (locker)
                {
                    if (instance == null) instance = new LvLogHelper(lb);
                }
            }
            return instance;
        }
        public static LvLogHelper GetInstance()
        {
            if (instance == null)
            {
                lock (locker)
                {
                    if (instance == null) instance = new LvLogHelper();
                }
            }
            return instance;
        }

        /// <summary>
        /// 打印日志
        /// </summary>
        /// <param name="log">日志信息</param>
        public void PrintLog(string log)
        {
            try
            {
                string txt = string.Format("--- {0}.{1}.{2}  {3}:{4}:{5}    :   ",
                    DateTime.Now.Year, DateTime.Now.Month,
                    DateTime.Now.Day, DateTime.Now.Hour,
                    DateTime.Now.Minute, DateTime.Now.Second);
                log = log.Trim();
                string txts = txt + log + "\r\n";

                LogBox.Items.Add(txts);
                LogBox.SelectedItem = txts;
            }
            catch { }
        }
        /// <summary>
        /// 打印日志(测试结果)
        /// </summary>
        /// <param name="log">日志</param>
        /// <param name="result">结果,例如pass或fail</param>
        public void PrintLog(string log, string result)
        {
            try
            {
                string txt = string.Format("--- {0}.{1}.{2}  {3}:{4}:{5}    :   ",
                    DateTime.Now.Year, DateTime.Now.Month,
                    DateTime.Now.Day, DateTime.Now.Hour,
                    DateTime.Now.Minute, DateTime.Now.Second);
                log = log.Trim();
                string txts = txt + log + "  -  " + result + "\r\n";

                LogBox.Items.Add(txts);
                LogBox.SelectedItem = txts;
            }
            catch { }
        }
        /// <summary>
        /// 打印日志(测试结果)
        /// </summary>
        /// <param name="log">日志</param>
        /// <param name="reason">原因</param>
        /// <param name="result">测试结果</param>
        public void PrintLog(string log, string reason, string result)
        {
            try
            {
                string txt = string.Format("--- {0}.{1}.{2}  {3}:{4}:{5}    :   ",
                    DateTime.Now.Year, DateTime.Now.Month,
                    DateTime.Now.Day, DateTime.Now.Hour,
                    DateTime.Now.Minute, DateTime.Now.Second);
                log = log.Trim();
                string txts = txt + log + "  -  " + reason + " > " + result + "\r\n";
                LogBox.Items.Add(txts);
                LogBox.SelectedItem = txts;
            }
            catch { }
        }

        public void SaveLogToFile(string path, ListBox lbbox)
        {
            if (File.Exists(path)) File.Delete(path);
            FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
            StreamWriter swLog = new StreamWriter(fs);
            swLog.Flush();
            swLog.BaseStream.Seek(0, SeekOrigin.End);// 使用StreamWriter来往文件中写入内容
            try
            {
                foreach (string log in lbbox.Items)
                {
                    swLog.Write(log);
                }
            }
            catch
            {
                swLog.Close();
            }
            finally
            {
                swLog.Flush();
                swLog.Close();
            }
        }

    }
}

  

posted on 2023-06-28 14:47  sunwugang  阅读(170)  评论(0编辑  收藏  举报