コード例 #1
0
        /// <summary>
        /// 为跨 Dll 调用的程序提供 Log
        /// </summary>
        /// <param name="handler">传入调用 DLL 方的处理函数</param>
        /// <param name="logMode">选择 LogMode,默认为 Trace 模式</param>
        /// <param name="newLine">换行字符</param>
        /// <param name="newSection">换段字符</param>
        public Logger_ForDll(HandlerLogCompleted handler, LogMode logMode = LogMode.Trace)
        {
            LogCompletedEvent += handler;

            loggerBase = Logger_Base.Create(logMode);

            newLine    = _newLine;
            newSection = _newSection;
        }
コード例 #2
0
        /// <summary>
        /// Log 到控制台输出
        /// </summary>
        /// <param name="outMode">选择 OutputMode,默认为输出到控制台模式</param>
        /// <param name="logMode">选择 LogMode,默认为 Trace 模式</param>
        public Logger_ForLocal(OutputMode outMode = OutputMode.Console, LogMode logMode = LogMode.Trace)
        {
            loggerBase = Logger_Base.Create(logMode);

            newLine    = _newLine;
            newSection = _newSection;

            switch (outMode)
            {
            case OutputMode.Console:
                HandlerWrite = Console.Write;
                break;

            case OutputMode.Standard:
                HandlerWrite = (string str) => { Trace.Write(str); };
                break;

            default:
                break;
            }

            Task.Factory.StartNew(() =>
            {
                while (true)
                {
                    while (_queue.Count > 0)
                    {
                        if (_queue.TryDequeue(out string str))
                        {
                            HandlerWrite(str);
                        }
                    }

                    Thread.Sleep(1);
                }
            });
        }
コード例 #3
0
        /// <summary>
        /// Log 到 txt 文件中
        /// </summary>
        /// <param name="path">路径 默认为程序运行路径下的 Log 文件夹</param>
        /// <param name="logMode">选择 LogMode,默认为 Trace 模式</param>
        /// <param name="segmentMode">选择 Log 的记录分割模式。默认为按天分割</param>
        /// <param name="flushInterval">将缓存写入文件的周期,单位为毫秒。默认 10000ms</param>
        public Logger_ForFile(string path = "Log\\", LogMode logMode = LogMode.Trace, SegmentMode segmentMode = SegmentMode.Day, int flushInterval = 10000)
        {
            _TimeStampStart = DateTime.Now;

            loggerBase = Logger_Base.Create(logMode);

            newLine    = _newLine;
            newSection = _newSection;

            // 校验 flushInterval 取值范围
            // 不能为 0 以避免自旋消耗 CPU 性能
            flushInterval = flushInterval > 0 ? flushInterval : 1;


            _QueueLog = new ConcurrentQueue <string>();
            Path      = path;

            switch (segmentMode)
            {
            case SegmentMode.Never:
                GetFileNameCheck = (DateTime dateTime) => { return(false); };
                break;

            case SegmentMode.Month:
                GetFileNameCheck = (DateTime dateTime) => { return(dateTime.Month != _TimeStampStart.Month); };
                break;

            case SegmentMode.Week:
                GetFileNameCheck = (DateTime dateTime) => { return(dateTime.GetWeek() != _TimeStampStart.GetWeek()); };
                break;

            case SegmentMode.Day:
                GetFileNameCheck = (DateTime dateTime) => { return(dateTime.Day != _TimeStampStart.Day); };
                break;

            case SegmentMode.Hour:
                GetFileNameCheck = (DateTime dateTime) => { return(dateTime.Hour != _TimeStampStart.Hour); };
                break;

            case SegmentMode.Minute:
                GetFileNameCheck = (DateTime dateTime) => { return(dateTime.Minute != _TimeStampStart.Minute); };
                break;

            default:
                break;
            }

            Task.Factory.StartNew(() =>
            {
                while (true)
                {
                    if (_QueueLog.Count != 0)
                    {
                        string logBuff;
                        string header   = "============" + DateTime.Now.ToString(_TimeFormat) + "============" + newLine;
                        string log      = "";
                        string fileName = _Path + "Log_" + GetFileName(DateTime.Now);
                        while (_QueueLog.Count != 0)
                        {
                            if (_QueueLog.TryDequeue(out logBuff))
                            {
                                log += logBuff;
                            }
                        }
                        if (log != string.Empty)
                        {
                            try
                            {
                                Directory.CreateDirectory(_Path);
                                File.AppendAllText(fileName, header + log, Encoding.UTF8);
                            }
                            catch (Exception e)
                            {
                                throw e;
                            }
                        }
                    }

                    System.Threading.Thread.Sleep(flushInterval);
                }
            });
        }
コード例 #4
0
 /// <summary>
 /// Log 到控制台输出
 /// </summary>
 /// <param name="outMode">选择 OutputMode,默认为输出到控制台模式</param>
 /// <param name="logMode">选择 LogMode,默认为 Trace 模式</param>
 public Logger_ForProcess(LogMode logMode = LogMode.Trace)
 {
     loggerBase = Logger_Base.Create(logMode);
 }