using System;
using System.IO;
using System.Text;
using System.Threading;

namespace RGD.Common
{
    public class LogUtil
    {
        private static object obje = new object();

        public static string LogPath
        {
            get
            {
                //return HttpContext.Current.Server.MapPath("/") + "/Log";// ConfigurationManager.AppSettings["LogPath"];
                return System.Environment.CurrentDirectory + "/Log";
            }
        }

        public static void WriteLog(string dirName, string contentText)
        {
            LogUtil.WriteLog(LogPath, dirName, contentText);
        }

        public static void WriteLog(string logPath, string dirName, string contentText)
        {
            LogUtil.WriteLog(dirName, contentText, logPath, FileMode.OpenOrCreate);
        }

        public static void WriteLogNoTime(string logPath, string dirName, string contentText)
        {
            LogUtil.WriteLogNoTopTime(dirName, contentText, logPath, FileMode.OpenOrCreate);
        }

        public static void WriteLog(string dirName, string contentText, string logPath, FileMode fm)
        {
            string logDirectory = logPath + "\\\\" + dirName + "\\";
            object obj;
            Monitor.Enter(obj = LogUtil.obje);
            try
            {
                if (!Directory.Exists(logDirectory))
                {
                    Directory.CreateDirectory(logDirectory);
                }
                string logName = logDirectory + DateTime.Now.ToString("yyyyMMdd") + ".log";
                FileStream logFile = null;
                StreamWriter streamWriter = null;
                try
                {
                    logFile = new FileStream(logName, fm);
                    logFile.Seek(0L, SeekOrigin.End);
                    streamWriter = new StreamWriter(logFile, Encoding.GetEncoding("gb2312"));
                    streamWriter.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + "|" + contentText);
                    //streamWriter.WriteLine(contentText);
                }
                catch (Exception)
                {
                }
                finally
                {
                    if (streamWriter != null)
                    {
                        streamWriter.Close();
                    }
                    if (logFile != null)
                    {
                        logFile.Close();
                    }
                }
            }
            finally
            {
                Monitor.Exit(obj);
            }
        }

        public static void WriteLogNoTopTime(string dirName, string contentText, string logPath, FileMode fm)
        {
            string logDirectory = logPath + "\\\\" + dirName + "\\";
            object obj;
            Monitor.Enter(obj = LogUtil.obje);
            try
            {
                if (!Directory.Exists(logDirectory))
                {
                    Directory.CreateDirectory(logDirectory);
                }
                string logName = logDirectory + DateTime.Now.ToString("yyyyMMdd") + ".log";
                FileStream logFile = null;
                StreamWriter streamWriter = null;
                try
                {
                    logFile = new FileStream(logName, fm);
                    logFile.Seek(0L, SeekOrigin.End);
                    streamWriter = new StreamWriter(logFile, Encoding.GetEncoding("gb2312"));
                    //streamWriter.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + "|" + contentText);
                    streamWriter.WriteLine(contentText);
                }
                catch (Exception)
                {
                }
                finally
                {
                    if (streamWriter != null)
                    {
                        streamWriter.Close();
                    }
                    if (logFile != null)
                    {
                        logFile.Close();
                    }
                }
            }
            finally
            {
                Monitor.Exit(obj);
            }
        }

        public static void WriteLogFile(string dirName, string contentText, string logPath, FileMode fm)
        {
            string logDirectory = logPath + "\\\\" + dirName + "\\";
            object obj;
            Monitor.Enter(obj = LogUtil.obje);
            try
            {
                if (!Directory.Exists(logDirectory))
                {
                    Directory.CreateDirectory(logDirectory);
                }
                string logName = logDirectory + DateTime.Now.ToString("yyyyMMdd") + ".log";
                FileStream logFile = null;
                StreamWriter streamWriter = null;
                try
                {
                    logFile = new FileStream(logName, fm);
                    logFile.Seek(0L, SeekOrigin.End);
                    streamWriter = new StreamWriter(logFile, Encoding.GetEncoding("gb2312"));
                    streamWriter.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + "|" + contentText);
                    //streamWriter.WriteLine(contentText);
                }
                catch (Exception)
                {
                }
                finally
                {
                    if (streamWriter != null)
                    {
                        streamWriter.Close();
                    }
                    if (logFile != null)
                    {
                        logFile.Close();
                    }
                }
            }
            finally
            {
                Monitor.Exit(obj);
            }
        }
    }
}