Esempio n. 1
0
        /// <summary>
        /// write log
        /// </summary>
        /// <param name="level"></param>
        /// <param name="message"></param>
        private void WriteLog(LogLevel level, object message)
        {
            bool isAddHead = _isShowHeader;

            if (isAddHead)
            {
                isAddHead = !File.Exists(_logFile);
            }

            string head1 = "=============================================================";
            string head2 = "    Date       Time     MsgType            Message           ";
            string head3 = "=============================================================";

            lock (this)
            {
                FileStream fs = null;
                try
                {
                    fs = File.Open(_logFile, FileMode.Append, FileAccess.Write);
                }
                catch (Exception e)
                {
                    System.Diagnostics.Debug.WriteLine(e);
                    return;
                }

                LogSizeChecker logChecker = new LogSizeChecker(fs);
                if (logChecker.IsOver512kBytes())
                {
                    fs.Close();
                    FileInfo     file      = new FileInfo(_logFile);
                    LogFileMover fileMover = new LogFileMover(file);
                    fileMover.Move();

                    fs        = File.Open(_logFile, FileMode.Append);
                    isAddHead = true;
                }
                StreamWriter sw = new StreamWriter(fs);
                if (isAddHead)
                {
                    sw.WriteLine(head1);
                    sw.WriteLine(head2);
                    sw.WriteLine(head3);
                }
                string preText = DateFormat.LogDate.GetLogDateFormat(DateTime.Now)
                                 + " "
                                 + LevelParser.GetLogTypeName(level);

                if (!_isShowHeader)
                {
                    preText = string.Empty;
                }
                //begin to write log
                if (message is Exception)
                {
                    Exception e = message as Exception;
                    if (LogConfig.Single().LogLevel == LogLevel.Debug)
                    {
                        sw.WriteLine(preText + e);
                    }
                    else
                    {
                        sw.WriteLine(preText + e.Message);
                    }
                }
                else
                {
                    sw.WriteLine(preText + message);
                }
                sw.Close();
                fs.Close();
            }
        }
Esempio n. 2
0
 internal FileLogger()
 {
     _logLevel     = LogConfig.Single().LogLevel;
     _logFile      = LogConfig.Single().FileLogFullName;
     _isShowHeader = LogConfig.Single().IsShowHeader;
 }