示例#1
0
        public Logger()
        {
            var time     = DateTime.Now;
            var filetime = time.ToString("yyyyMMdd_HHmmss");

            outfilepath  = "log/log_" + filetime + ".log";
            otherLogger  = null;
            outtag_info  = OUTPosition.Console;
            outtag_warn  = OUTPosition.Console | OUTPosition.Trace | OUTPosition.File;
            outtag_error = OUTPosition.Console | OUTPosition.Trace | OUTPosition.File;
        }
示例#2
0
        void WriteLine(LogType type, OUTPosition outtag, string str)
        {
            string tag = null;

            if ((outtag & OUTPosition.Console) > 0)
            {
                if (tag == null)
                {
                    if (type == LogType.Info)
                    {
                        Console.ForegroundColor = ConsoleColor.Gray;
                    }
                    else if (type == LogType.Warn)
                    {
                        Console.ForegroundColor = ConsoleColor.Yellow;
                    }
                    else if (type == LogType.Error)
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                    }
                }
                //Console.Write(tag);
                Console.WriteLine(str);

                Console.ForegroundColor = ConsoleColor.Gray;
            }
            if ((outtag & OUTPosition.Trace) > 0)
            {
                if (tag == null)
                {
                    if (type == LogType.Info)
                    {
                        tag = "<I>";
                    }
                    else if (type == LogType.Warn)
                    {
                        tag = "<W>";
                    }
                    else if (type == LogType.Error)
                    {
                        tag = "<E>";
                    }
                }
                System.Diagnostics.Trace.Write(tag);
                System.Diagnostics.Trace.WriteLine(str);
            }
            if ((outtag & OUTPosition.Debug) > 0)
            {
                if (tag == null)
                {
                    if (type == LogType.Info)
                    {
                        tag = "<I>";
                    }
                    else if (type == LogType.Warn)
                    {
                        tag = "<W>";
                    }
                    else if (type == LogType.Error)
                    {
                        tag = "<E>";
                    }
                }
                System.Diagnostics.Debug.Write(tag);
                System.Diagnostics.Debug.WriteLine(str);
            }
            lock (this)
            {
                if ((outtag & OUTPosition.File) > 0)
                {
                    try
                    {
                        System.IO.File.AppendAllText(outfilepath, tag + str + "\n", System.Text.Encoding.UTF8);
                    }
                    catch
                    {
                        Console.Write("<LOG ERROR>cant write to file:" + outfilepath + "=" + str);
                    }
                }
            }
            if (otherLogger != null && (outtag_info & OUTPosition.Other) > 0)
            {
                try
                {
                    if (type == LogType.Info)
                    {
                        otherLogger.Info(str);
                    }
                    else if (type == LogType.Warn)
                    {
                        otherLogger.Warn(str);
                    }
                    else if (type == LogType.Error)
                    {
                        otherLogger.Error(str);
                    }
                }
                catch
                {
                    Console.Write("<LOG ERROR>cant write to otherLogger:" + str);
                }
            }
        }