1、创建日志
public static void WriteLog(string msg)
{
//该日志文件会存在windows服务程序目录下
//string path = AppDomain.CurrentDomain.BaseDirectory + "\\log.txt";
//该日志文件会存在最后一个硬盘下目录下
System.IO.DriveInfo[] allDrives = System.IO.DriveInfo.GetDrives();
string path = allDrives.Last().Name + "LYSpecServiceLog" ;
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
path = path + "\\" + DateTime.Now.ToString("yyyyMMdd") + ".txt";
using (FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
{
msg = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ffff") + "\t" + msg + "\r\n";
msg += "----------------------------------------------------------------------\r\n";
byte[] bs = Encoding.UTF8.GetBytes(msg);
fs.Write(bs, 0, bs.Length);
fs.Close();
}
}
方法2
public static void Save(string gasName, double[] value)
{
string configName = Directory.GetCurrentDirectory() + "\\Config\\";
if (!Directory.Exists(configName))
{
Directory.CreateDirectory(configName);
}
StringBuilder sb = new StringBuilder(5120);
for (int i = 0; i < value.Length; i++)
{
sb.Append(i + 1);
sb.Append('\t');
sb.Append(value[i]);
sb.Append(Environment.NewLine);
}
File.WriteAllText(configName + $"{gasName}.txt", sb.ToString());
}