Esempio n. 1
0
        /// <summary>
        /// 初始化日志体信息
        /// </summary>
        /// <param name="msgHeader">日志信息头部</param>
        /// <param name="logMessage">日志体信息</param>
        /// <returns>初始完成的日志体信息结构</returns>
        /// <remarks>Version:1.1.3.0</remarks>
        public LogBody PrepareLogBody(String msgHeader, String logMessage)
        {
            var logBody = new LogBody();

            TXTLogFactory.SetLogBodyString(logBody, msgHeader, logMessage);
            return(logBody);
        }
Esempio n. 2
0
        /// <summary>
        /// 文本日志服务
        /// </summary>
        /// <param name="logconfig"></param>
        public TXTLogService(LogServiceConfig logconfig) : base(logconfig)
        {
            ProcessIsBegin = true;
            var lpinterval = TXTLogFactory.GetLogPersistInterval(logconfig);

            LogPersistInterval = lpinterval == 0 ? DefaultPersistInterval : lpinterval;
        }
Esempio n. 3
0
 /// <summary>
 /// 启动日志记录
 /// </summary>
 public override void Start()
 {
     //**********VERSION 1.1.2.0 START**********//
     if (_thread != null)
     {
         _thread.Start();
         return;
     }
     //**********VERSION 1.1.2.0 END**********//
     LogPersistenceService.SetPersistParam(LogFormatter.GetLogFileName(TXTLogFactory.GetLogFileName(LogConfig)));
     //**********VERSION 1.1.2.0 START**********//
     _thread = new Thread(Run)
     {
         IsBackground = true
     };
     _thread.Start();
     //new Thread(Run).Start();
     //**********VERSION 1.1.2.0 END**********//
 }
Esempio n. 4
0
        /// <summary>
        /// 日志写入
        /// </summary>
        /// <param name="theRecordGrade">日志级别</param>
        /// <param name="msgHeader">日志消息头信息</param>
        /// <param name="exception">异常消息信息</param>
        /// <remarks>Version:1.1.3.0</remarks>
        public override void WriteLog(Int32 theRecordGrade, String msgHeader, Exception exception)
        {
            if (theRecordGrade < TXTLogFactory.GetLogGrade(LogConfig))
            {
                return;
            }
            var        stackFrames = new StackTrace(exception, true).GetFrames();
            StackFrame stackFrame  = null;

            if (stackFrames != null)
            {
                stackFrame = stackFrames.Last();
            }
            var logrec = LogFormatter.FormatLogRecord(PrepareLogHeader(), PrepareLogBody(msgHeader, exception.Message), stackFrame);

            if (logrec != null)
            {
                LogRecordList.Add(logrec);
            }
        }
Esempio n. 5
0
        /// <summary>
        /// 日志记录
        /// </summary>
        /// <param name="theRecordGrade">日志级别</param>
        /// <param name="logMessage">日志信息</param>
        public override void WriteLog(Int32 theRecordGrade, string logMessage)
        {
            if (theRecordGrade < TXTLogFactory.GetLogGrade(LogConfig))
            {
                return;
            }
            //**********VERSION 1.1.1.0 START**********//
            var        stackFrames = new StackTrace(true).GetFrames();
            StackFrame stackFrame  = null;

            if (stackFrames != null)
            {
                stackFrame = stackFrames[1];
            }
            //**********VERSION 1.1.1.0 END**********//
            var logrec = LogFormatter.FormatLogRecord(PrepareLogHeader(), PrepareLogBody(logMessage), stackFrame);

            if (logrec != null)
            {
                LogRecordList.Add(logrec);
            }
        }
Esempio n. 6
0
        /// <summary>
        /// 日志记录
        /// </summary>
        /// <param name="exception">异常信息</param>
        public override void WriteLog(Exception exception)
        {
            if (DefaultLogGrade < TXTLogFactory.GetLogGrade(LogConfig))
            {
                return;
            }
            //**********VERSION 1.1.1.0 START**********//
            var        stackFrames = new StackTrace(exception, true).GetFrames();
            StackFrame stackFrame  = null;

            if (stackFrames != null)
            {
                stackFrame = stackFrames.Last();
            }
            //**********VERSION 1.1.1.0 END**********//
            var logrec = LogFormatter.FormatLogRecord(PrepareLogHeader(), PrepareLogBody(exception.Message), stackFrame);

            if (logrec != null)
            {
                LogRecordList.Add(logrec);
            }
        }
Esempio n. 7
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="logHeader"></param>
        /// <param name="logBody"></param>
        /// <param name="stackfram"></param>
        /// <returns></returns>
        public override LogRecord FormatLogRecord(LogHeader logHeader, LogBody logBody, StackFrame stackfram)
        {
            var lr        = new LogRecord();
            var sb        = new StringBuilder();
            var seperator = TXTLogFactory.GetLogSeperator(logHeader);

            sb.Append(FormatTime(TXTLogFactory.GetLogGenTime(logHeader)));
            sb.Append(seperator);
            sb.Append(logHeader.LogName);
            sb.Append(seperator);
            sb.Append(logHeader.LogInfo);
            sb.Append(seperator);
            sb.Append(logHeader.LogType);
            sb.Append(seperator);
            sb.Append(logHeader.LogVersion);
            //2014-12-09 20:33:36:495|AdminService|WAP.WCF.HELPER|TEXT|1.0||||2014-12-09 20:33:36:495	999920
            sb.Append(seperator);
            if (logHeader.LogTraceEnable)
            {
                var className   = Path.GetFileName(LogManager.GetClassName(stackfram)); //VERSION:1.1.1.0
                var methodName  = LogManager.GetMethodName(stackfram);
                var lineNoumber = LogManager.GetLineNumber(stackfram);
                var colNumber   = LogManager.GetColumnNumber(stackfram);

                sb.Append(className);
                sb.Append(seperator);
                sb.Append(methodName);
                sb.Append(seperator);
                sb.Append(lineNoumber);
                sb.Append(seperator);
                sb.Append(colNumber);
                sb.Append(seperator);
            }
            sb.Append(TXTLogFactory.GetLogBodyString(logBody));
            lr.LogMessage = sb.ToString();
            return(lr);
        }
Esempio n. 8
0
 /// <summary>
 /// 获取日志工厂对象
 /// </summary>
 /// <returns></returns>
 public static LogFactory GetInstance()
 {
     return(Instance ?? (Instance = new TXTLogFactory()));
 }
Esempio n. 9
0
        public void Save(List <LogRecord> logRecordList)
        {
            lock (logRecordList)
            {
                var filePath = TXTLogFactory.GetLogFilePath(LogConfig) + TXTLogFactory.GetLogSubFolder(LogConfig);
                if (!Directory.Exists(filePath))
                {
                    Directory.CreateDirectory(filePath);
                }

                var file = filePath + "\\" + _fileName;

                if (_theStreamFileSplitManager.IsFristRun)
                {
                    bool rlt;
                    do
                    {
                        file = filePath + "\\" + _fileName;
                        rlt  = File.Exists(file);
                        if (!rlt)
                        {
                            continue;
                        }
                        if (_theStreamFileSplitManager.IsAppend)
                        {
                            var fi = new FileInfo(file);
                            if ((UInt64)fi.Length >= _theStreamFileSplitManager.FileMaxSizeBytes)
                            {
                                _theStreamFileSplitManager.AddSeqNo(1);
                                _fileName = _theStreamFileSplitManager.GetNewFileName(1);
                            }
                            else
                            {
                                break;
                            }
                        }
                        else
                        {
                            _theStreamFileSplitManager.AddSeqNo(1);
                            _fileName = _theStreamFileSplitManager.GetNewFileName(1);
                        }
                    } while (rlt);
                    _theStreamFileSplitManager.IsFristRun = false;
                }
                var streamWrite = new StreamWriter(file, true);

                try
                {
                    //多线程可能导致集合不同步
                    //foreach (var record in logRecordList
                    //{
                    //    var recByteSize = (UInt16)(Encoding.Default.GetByteCount(record.LogMessage) + 2);
                    //    var tempfileName = _theStreamFileSplitManager.GetNewFileName(recByteSize);

                    //    if (tempfileName != _fileName)
                    //    {
                    //        streamWrite.Flush();
                    //        streamWrite.Close();
                    //        _fileName = tempfileName;
                    //        file = filePath + "\\" + _fileName;
                    //        streamWrite = new StreamWriter(file, true);
                    //    }
                    //    streamWrite.WriteLine(record.LogMessage);
                    //}

                    do
                    {
                        var record = logRecordList[0];
                        if (record == null || record.LogMessage == null)
                        {
                            logRecordList.Remove(record);
                            continue;
                        }
                        var recByteSize  = (UInt16)(Encoding.Default.GetByteCount(record.LogMessage) + 2);
                        var tempfileName = _theStreamFileSplitManager.GetNewFileName(recByteSize);

                        if (tempfileName != _fileName)
                        {
                            streamWrite.Flush();
                            streamWrite.Close();
                            _fileName   = tempfileName;
                            file        = filePath + "\\" + _fileName;
                            streamWrite = new StreamWriter(file, true);
                        }
                        streamWrite.WriteLine(record.LogMessage);
                        logRecordList.Remove(record);
                    } while (logRecordList.Count > 0);

                    //logRecordList.Clear();

                    streamWrite.Flush();
                    streamWrite.Close();
                }
                catch (Exception ex)
                {
                    throw ExceptionHelper.GetException(20002001, "TXTLogService_Save", ex);
                }
                finally
                {
                    streamWrite.Close();
                }
            }
        }
Esempio n. 10
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="logcfg"></param>
 public StreamFileLPS(LogServiceConfig logcfg)
 {
     LogConfig = logcfg;
     _theStreamFileSplitManager = new StreamFileSplitManager(TXTLogFactory.GetLogFileName(logcfg));
     _fileName = _theStreamFileSplitManager.GetNewFileName(1);
 }
Esempio n. 11
0
 /// <summary>
 /// 获取日志工厂对象
 /// </summary>
 /// <returns></returns>
 public static LogFactory GetInstance()
 {
     return Instance ?? (Instance = new TXTLogFactory());
 }