Example #1
0
File: XTrace.cs Project: thxu/X
        public static void UseWinFormControl(this Control control, Boolean useFileLog = true, Int32 maxLines = 1000)
        {
            var clg = _Log as TextControlLog;
            var ftl = _Log as TextFileLog;
            if (_Log is CompositeLog cmp)
            {
                ftl = cmp.Get<TextFileLog>();
                clg = cmp.Get<TextControlLog>();
            }

            // 控制控制台日志
            if (clg == null) clg = new TextControlLog();
            clg.Control = control;
            clg.MaxLines = maxLines;

            if (!useFileLog)
            {
                Log = clg;
                if (ftl != null) ftl.Dispose();
            }
            else
            {
                if (ftl == null) ftl = TextFileLog.Create(null);
                Log = new CompositeLog(clg, ftl);
            }
        }
Example #2
0
        static Boolean InitLog()
        {
            /*
             * 日志初始化可能会触发配置模块,其内部又写日志导致死循环。
             * 1,外部写日志引发初始化
             * 2,标识日志初始化正在进行中
             * 3,初始化日志提供者
             * 4,此时如果再次引发写入日志,发现正在进行中,放弃写入的日志
             * 5,标识日志初始化已完成
             * 6,正常写入日志
             */

            if (_Log != null && _Log != Logger.Null)
            {
                return(true);
            }
            if (_initing > 0 && _initing == Thread.CurrentThread.ManagedThreadId)
            {
                return(false);
            }

            lock (_lock)
            {
                if (_Log != null && _Log != Logger.Null)
                {
                    return(true);
                }

                _initing = Thread.CurrentThread.ManagedThreadId;

                var set = Setting.Current;
                if (set.LogFileFormat.Contains("{1}"))
                {
                    _Log = new LevelLog(LogPath, set.LogFileFormat);
                }
                else
                {
                    _Log = TextFileLog.Create(LogPath);
                }

                if (!set.NetworkLog.IsNullOrEmpty())
                {
                    var nlog = new NetworkLog(set.NetworkLog);
                    _Log = new CompositeLog(_Log, nlog);
                }

                _initing = 0;
            }

            //WriteVersion();

            return(true);
        }
Example #3
0
 /// <summary>通过指定路径和文件格式来实例化等级日志,每个等级使用自己的日志输出</summary>
 /// <param name="logPath"></param>
 /// <param name="fileFormat"></param>
 public LevelLog(String logPath, String fileFormat)
 {
     foreach (LogLevel item in Enum.GetValues(typeof(LogLevel)))
     {
         if (item is > LogLevel.All and < LogLevel.Off)
         {
             _logs[item] = new TextFileLog(logPath, false, fileFormat)
             {
                 Level = item
             };
         }
     }
 }
Example #4
0
        static void InitLog()
        {
            if (hasInited > 0 || Interlocked.CompareExchange(ref hasInited, 1, 0) > 0)
            {
                return;
            }

            Log = TextFileLog.Create(Config.GetConfig <String>("NewLife.LogPath"));

            var asmx = AssemblyX.Create(Assembly.GetExecutingAssembly());

            WriteLine("{0} v{1} Build {2:yyyy-MM-dd HH:mm:ss}", asmx.Name, asmx.FileVersion, asmx.Compile);
        }
Example #5
0
        /// <summary>
        /// 2012.11.05 修正初次调用的时候,由于同步BUG,导致Log为空的问题。
        /// </summary>
        static Boolean InitLog()
        {
            /*
             * 日志初始化可能会除法配置模块,其内部又写日志导致死循环。
             * 1,外部写日志引发初始化
             * 2,标识日志初始化正在进行中
             * 3,初始化日志提供者
             * 4,此时如果再次引发写入日志,发现正在进行中,放弃写入的日志
             * 5,标识日志初始化已完成
             * 6,正常写入日志
             */

            if (_Log != null)
            {
                return(true);
            }
            if (_initing > 0 && _initing == Thread.CurrentThread.ManagedThreadId)
            {
                return(false);
            }

            lock (_lock)
            {
                if (_Log != null)
                {
                    return(true);
                }

                _initing = Thread.CurrentThread.ManagedThreadId;
#if !__MOBILE__
                _Log = TextFileLog.Create(LogPath);
#else
                _Log = new NetworkLog();
#endif

                var set = Setting.Current;
                if (!set.NetworkLog.IsNullOrEmpty())
                {
                    var nlog = new NetworkLog(NetHelper.ParseEndPoint(set.NetworkLog, 514));
                    _Log = new CompositeLog(_Log, nlog);
                }

                _initing = 0;
            }

            WriteVersion();

            return(true);
        }
Example #6
0
        /// <summary>
        /// 2012.11.05 修正初次调用的时候,由于同步BUG,导致Log为空的问题。
        /// </summary>
        static Boolean InitLog()
        {
            /*
             * 日志初始化可能会除法配置模块,其内部又写日志导致死循环。
             * 1,外部写日志引发初始化
             * 2,标识日志初始化正在进行中
             * 3,初始化日志提供者
             * 4,此时如果再次引发写入日志,发现正在进行中,放弃写入的日志
             * 5,标识日志初始化已完成
             * 6,正常写入日志
             */

            if (_Log != null)
            {
                return(true);
            }
            if (_initing > 0 && _initing == Thread.CurrentThread.ManagedThreadId)
            {
                return(false);
            }

            lock (_lock)
            {
                if (_Log != null)
                {
                    return(true);
                }

                _initing = Thread.CurrentThread.ManagedThreadId;
#if !Android
                _Log = TextFileLog.Create(LogPath);
#else
                _Log = new NetworkLog();
#endif
                _initing = 0;
            }

            WriteVersion();

            return(true);
        }
Example #7
0
        static void InitLog()
        {
            if (hasInited > 0 || Interlocked.CompareExchange(ref hasInited, 1, 0) > 0) return;

            Log = TextFileLog.Create(Config.GetConfig<String>("NewLife.LogPath"));

            var asmx = AssemblyX.Create(Assembly.GetExecutingAssembly());
            WriteLine("{0} v{1} Build {2:yyyy-MM-dd HH:mm:ss}", asmx.Name, asmx.FileVersion, asmx.Compile);
        }
Example #8
0
        /// <summary>使用控制台输出日志,只能调用一次</summary>
        /// <param name="useColor">是否使用颜色,默认使用</param>
        /// <param name="useFileLog">是否同时使用文件日志,默认使用</param>
        public static void UseConsole(Boolean useColor = true, Boolean useFileLog = true)
        {
            if (!Runtime.IsConsole)
            {
                return;
            }

            // 适当加大控制台窗口
            try
            {
                if (Console.WindowWidth <= 80)
                {
                    Console.WindowWidth = Console.WindowWidth * 3 / 2;
                }
                if (Console.WindowHeight <= 25)
                {
                    Console.WindowHeight = Console.WindowHeight * 3 / 2;
                }
            }
            catch { }

            var clg = _Log as ConsoleLog;
            var ftl = _Log as TextFileLog;
            var cmp = _Log as CompositeLog;

            if (cmp != null)
            {
                ftl = cmp.Get <TextFileLog>();
                clg = cmp.Get <ConsoleLog>();
            }

            // 控制控制台日志
            if (clg == null)
            {
                clg = new ConsoleLog {
                    UseColor = useColor
                }
            }
            ;
            else
            {
                clg.UseColor = useColor;
            }

            if (!useFileLog)
            {
                // 如果原有提供者是文本日志,则直接替换
                if (ftl != null)
                {
                    Log = clg;
                    ftl.Dispose();
                }
                // 否则组件复合日志
                else
                {
                    if (cmp != null)
                    {
                        cmp.Remove(clg);
                        if (cmp.Logs.Count == 0)
                        {
                            _Log = null;
                        }
                    }

                    cmp = new CompositeLog();
                    cmp.Add(clg);
                    if (_Log != null)
                    {
                        cmp.Add(_Log);
                    }
                    Log = cmp;
                }
            }
            else
            {
                cmp = new CompositeLog();
                cmp.Add(clg);
                if (ftl == null)
                {
                    //if (_Log != null) cmp.Add(_Log);
                    ftl = TextFileLog.Create(null);
                }
                cmp.Add(ftl);
                Log = cmp;
            }

            //WriteVersion();
        }