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();
}
}
}
}
博客内容主要用于日常学习记录,内容比较随意,如有问题,还需谅解!!!

浙公网安备 33010602011771号