using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace AutoService
{
class WriteLog
{
static String path = AppDomain.CurrentDomain.BaseDirectory;//当前服务所在路径
static String fileName = path+"ServiceLog.txt";
static StreamWriter sw = null;
static StreamReader sr = null;
static char[] ch;
private static bool isUse = true;
private static void init()
{
string a = Environment.CurrentDirectory;
if (File.Exists(fileName))
{
sr = new StreamReader(fileName);
int length = (int)sr.BaseStream.Length;
ch = new char[length];
sr.ReadBlock(ch, 0, length);
sr.Close();
sw = new StreamWriter(fileName);
sw.Write(new String(ch));
}
else
{
sw = new StreamWriter(fileName);
}
}
public static void toLog(String log)
{
if (isUse)
{
init();
isUse = false;
}
sw.WriteLine(DateTime.Now.ToString() + " " + log);
sw.Flush();
}
}
}