using System;
using System.IO;
using System.Windows.Forms;
namespace WCS_Client
{
    public class Log
    {
        /// 日志文件的路径
        private string fileLogPath;
        /// 日志文件的名称
        private string dataFileName;
        public Log(string FileName, string filepath)
        {
            this.fileLogPath = filepath;
            this.dataFileName = FileName + ".txt";
            //this.dataFileName = DateTime.Now.ToString("yyyyMMdd") + FileName + ".txt";
        }
        /// 
        /// 获取或设置日志文件的路径
        /// 
        public string FileLogPath
        {
            set
            {
                this.fileLogPath = value;
            }
            get
            {
                return this.fileLogPath;
            }
        }
        /// 
        /// 获取或设置日志文件的名称
        /// 
        public string DataFileName
        {
            set
            {
                this.dataFileName = value;
            }
            get
            {
                return this.dataFileName;
            }
        }
        /// 
        /// 向指定目录中的指定的文件中追加日志文件
        /// 
        /// 要写入的内容
        public void WriteLog(string Message)
        {
            this.WriteLog(this.dataFileName, Message);
        }
        /// 
        /// 向指定目录中的文件中追加日志文件,日志文件的名称将由传递的参数决定.
        /// 
        /// 日志文件的名称,如:mylog.txt ,如果没有自动创建,如果存在将追加写入日志
        /// 要写入的内容
        public void WriteLog(string LogFileName, string Message)
        {
            //如果日志文件目录不存在,则创建
            if (!Directory.Exists(this.fileLogPath))
            {
                Directory.CreateDirectory(this.fileLogPath);
            }
            FileInfo finfo = new FileInfo(this.fileLogPath + LogFileName);
            try
            {
                FileStream fs = new FileStream(this.fileLogPath + LogFileName, FileMode.Append);
                StreamWriter strwriter = new StreamWriter(fs);
                try
                {
                    strwriter.WriteLine(System.DateTime.Now.ToString() + " " + DateTime.Now.Millisecond.ToString().PadLeft(3, '0') + ": " + Message);
                    strwriter.Flush();
                }
                catch (Exception ee)
                {
                    Console.WriteLine("日志文件写入失败信息:" + ee.ToString());
                }
                finally
                {
                    strwriter.Close();
                    strwriter = null;
                    fs.Close();
                    fs = null;
                }
            }
            catch (Exception ee)
            {
                Console.WriteLine("日志文件没有打开,详细信息如下:" + ee.ToString());
            }
        }
    }
    public class LogHelper
    {
        //private static Log _sys_Log = new Log("系统", "C:\\webapi日志\\");//Application.StartupPath +
        //private static string mappedPath = System.Web.Hosting.HostingEnvironment.MapPath("~/");
        private static string mappedPath = Application.StartupPath;
        private static string dt = DateTime.Now.ToString("yyyyMMdd");
        private static string fileName = dt + " 系统";
        private static Log _sys_Log = new Log(fileName, mappedPath + "\\Logs\\");
        public static Log Sys_Log
        {
            get
            {
                if (dt != DateTime.Now.ToString("yyyyMMdd"))
                {
                    dt = DateTime.Now.ToString("yyyyMMdd");
                    _sys_Log = new Log(fileName, mappedPath + "\\Logs\\");
                }
                return _sys_Log;
            }
        }
    }
}