using System.Text;

namespace AUTS_Server.Service
{
    public class LogHelperForService : ILogHelperForService
    {
        string LogFile = "";
        public void Init()
        {
            string directory = AppDomain.CurrentDomain.BaseDirectory + "\\Logs";
            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }
            DateTime curTime = DateTime.Now;

            LogFile = directory + "\\" + curTime.ToString("yyyy-MM-dd") + ".txt";
            if (!File.Exists(LogFile))
            {
                FileStream fs = File.Create(LogFile);
                fs.Close();
            }
        }

        public void WriteLine(string text)
        {
            lock (LogFile)
            {
                Init();
                text += "\r\n";
                using (StreamWriter sw = new StreamWriter(LogFile, true, Encoding.UTF8))
                {
                    sw.Write(DateTime.Now.ToString("[yyyy-MM-dd HH:mm:ss] ") + text);
                }
            }
        }
    }
}
