using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace MS.WebCore.Logger
{
    public static class Logger
    {
        public static void WriteLog(string msg)
        {
            string filePath = "";
            string logPath = "";

            try
            {
                string LogDir =DateTime.Now.ToString("yyyy-MM-dd");//文件名
                filePath = @"C:\Log\" + LogDir + @"\"; //路径
                logPath = filePath + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
                if (!Directory.Exists(filePath))
                {
                    Directory.CreateDirectory(filePath);
                }


                using (StreamWriter sw = File.AppendText(logPath))
                {
                    sw.WriteLine("消息：" + msg);
                    sw.WriteLine("时间：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    sw.WriteLine("**************************************************");
                    sw.WriteLine();
                    sw.Flush();
                    sw.Close();
                    sw.Dispose();
                }
            }
            catch (IOException e)
            {
                using (StreamWriter sw = File.AppendText(logPath))
                {
                    sw.WriteLine("异常：" + e.Message);
                    sw.WriteLine("时间：" + DateTime.Now.ToString("yyy-MM-dd HH:mm:ss"));
                    sw.WriteLine("**************************************************");
                    sw.WriteLine();
                    sw.Flush();
                    sw.Close();
                    sw.Dispose();
                }
            }
        }
    }
}
