Exemplo n.º 1
0
    /// <summary>格式化log</summary>
    /// <param name="stringBuilder">格式化日志存储器</param>
    /// <param name="logType">日志类型</param>
    /// <param name="msg">日志</param>
    /// <param name="stackTrace">调用堆栈</param>
    /// <param name="time">产生时间</param>
    private static void LogFormat(ref StringBuilder stringBuilder, eLogLevel logType, string msg, string stackTrace = null, string time = null)
    {
        if (stringBuilder == null)
        {
            stringBuilder = GetStringBuilder();
        }

        switch (logType)
        {
        case eLogLevel.Log:
            stringBuilder.Append(TAG_LOG);
            break;

        case eLogLevel.Warning:
            stringBuilder.Append(TAG_WARNING);
            break;

        case eLogLevel.Error:
            stringBuilder.Append(TAG_ERROR);
            break;
        }

        stringBuilder.Append(TAB_LEFT_FLAG);
        stringBuilder.Append(!string.IsNullOrEmpty(time) ? time : DateTime.Now.ToString(TIME_FORMAT_HMSF));
        stringBuilder.Append(TAB_RIGHT_FLAG);

        stringBuilder.Append(TAB_SPACE);
        stringBuilder.AppendLine(msg.TrimStart());

        if (!string.IsNullOrEmpty(stackTrace))
        {
            stringBuilder.AppendLine(stackTrace);
        }
        stringBuilder.AppendLine("");
    }
Exemplo n.º 2
0
 /// <summary>
 /// 写日志
 /// </summary>
 /// <param name="log"></param>
 public void WriteLog(eLogLevel logLevel, string log)
 {
     if (logLevel == eLogLevel.ell_Debug)
     {
         Console.ForegroundColor = ConsoleColor.Green;
         Console.WriteLine(log);
     }
     else if (logLevel == eLogLevel.ell_Error)
     {
         Logger.Error(log);
     }
     else if (logLevel == eLogLevel.ell_Fatal)
     {
         Logger.Fatal(log);
     }
     else if (logLevel == eLogLevel.ell_Info)
     {
         Console.ForegroundColor = ConsoleColor.White;
         writeLog.WriteInLog(log);
     }
     else if (logLevel == eLogLevel.ell_Warning)
     {
         Logger.Warn(log);
     }
 }
Exemplo n.º 3
0
    public static void Debug(string log, eLogLevel logLevel, eLogOutputType outputType = eLogOutputType.All, string path = null, bool stackTrace = false)
    {
        switch (logLevel)
        {
        case eLogLevel.Error:
            Error(log, eLogColor.Red, outputType, path, stackTrace);
            break;

        case eLogLevel.Exception:
            Error(log, eLogColor.Red, outputType, path, stackTrace);
            break;

        case eLogLevel.Log:
            Debug(log, eLogColor.White, outputType, path, stackTrace);
            break;

        case eLogLevel.Warning:
            Warning(log, eLogColor.Yellow, outputType, path, stackTrace);
            break;

        case eLogLevel.Assert:
            Error(log, eLogColor.Red, outputType, path, stackTrace);
            break;
        }
    }
Exemplo n.º 4
0
        private bool tryGetSwitch(string i_Name, out eLogLevel i_Level)
        {
            bool foundSwitch = false;

            IConfigurationSection switches = r_Configuration.GetSection("LogLevel");

            if (switches == null)
            {
                i_Level     = eLogLevel.None;
                foundSwitch = false;
            }
            else
            {
                string value = switches[i_Name];
                if (string.IsNullOrEmpty(value))
                {
                    i_Level     = eLogLevel.None;
                    foundSwitch = false;
                }
                else if (Enum.TryParse <eLogLevel>(value, out i_Level))
                {
                    foundSwitch = true;
                }
                else
                {
                    string message = $"Configuration value '{value}' for category '{i_Name}' is not supported.";
                    throw new InvalidOperationException(message);
                }
            }

            return(foundSwitch);
        }
Exemplo n.º 5
0
        public void ToConsole(eLogLevel logLevel, string message)
        {
            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.Append("[").Append(logLevel).Append(" | ").Append(DateTime.Now.ToString("HH:mm:ss:fff_dd-MMM")).Append("] ").Append(message).Append(Environment.NewLine);

            switch (logLevel)
            {
            case eLogLevel.ERROR:
                Console.ForegroundColor = ConsoleColor.Red;
                break;

            case eLogLevel.FATAL:
                Console.ForegroundColor = ConsoleColor.DarkRed;
                break;

            case eLogLevel.DEBUG:
                Console.ForegroundColor = ConsoleColor.White;
                break;

            case eLogLevel.INFO:
                Console.ForegroundColor = ConsoleColor.Cyan;
                break;

            case eLogLevel.WARN:
                Console.ForegroundColor = ConsoleColor.DarkYellow;
                break;
            }
            Console.WriteLine(stringBuilder.ToString());
            Console.ResetColor();
        }
Exemplo n.º 6
0
        static void logMessage(eLogLevel level, eLogComponent component, string message, string source)
        {
            if (level == eLogLevel.Error)
            {
                Utils.NativeErrorMessages.setNativeErrorMessage(message);
            }

            ConsoleColor ccMessage       = s_colors[(byte)level];
            string       componentString = s_components[(byte)component];

            lock ( syncRoot )
            {
                ConsoleColor ccPrev = Console.ForegroundColor;
                Console.ForegroundColor = ccMessage;
                if (null == source)
                {
                    Console.WriteLine("{0}\t{1}", componentString, message);
                }
                else
                {
                    Console.WriteLine("{0}\t{1}\t{2}", componentString, message, source);
                }
                Console.ForegroundColor = ccPrev;
            }
        }
Exemplo n.º 7
0
 public static void SetEnableType(eLogLevel lv, bool b)
 {
     if (lv >= eLogLevel.LV_DEBUG && lv < eLogLevel.LV_MAX)
     {
         m_EnableType[(int)lv] = b;
     }
 }
Exemplo n.º 8
0
 public static void SetLogLv(eLogLevel lv)
 {
     if (lv >= eLogLevel.LV_DEBUG && lv < eLogLevel.LV_MAX)
     {
         m_LogLv = lv;
     }
 }
Exemplo n.º 9
0
 /// <summary>
 /// 写日志
 /// </summary>
 /// <param name="log"></param>
 public void Log(eLogLevel logLevel, string log)
 {
     if (logLevel == eLogLevel.ell_Debug)
     {
         Console.ForegroundColor = ConsoleColor.Green;
         Console.WriteLine("[" + DateTime.Now.ToString() + "]" + log);
     }
     else if (logLevel == eLogLevel.ell_Error)
     {
         WriteInLog(log);
     }
     else if (logLevel == eLogLevel.ell_Fatal)
     {
         WriteInLog(log);
     }
     else if (logLevel == eLogLevel.ell_Info)
     {
         Console.ForegroundColor = ConsoleColor.White;
         WriteInLog(log);
     }
     else if (logLevel == eLogLevel.ell_Warning)
     {
         WriteInLog(log);
     }
 }
Exemplo n.º 10
0
        void _LogFormat(string tag, eLogLevel logLevel, string fmt, params object[] args)
        {
            if (!IsValidLogLevel(logLevel))
            {
                return;
            }

            Exception exception = null;
            string    message;

            try
            {
                message = string.Format(fmt, args);
            }
            catch (Exception ex)
            {
                message   = INVALID_FORMAT;
                exception = ex;
            }
            __Log(new LogInfo
            {
                DateTime      = DateTime.Now,
                SequnceNumber = Interlocked.Increment(ref mSequenceNumber),
                Tag           = tag,
                ThreadID      = Thread.CurrentThread.ManagedThreadId,
                LogLevel      = eLogLevel.Info,
                Message       = message,
                Exception     = exception
            });
        }
Exemplo n.º 11
0
        public void ToConsole(eLogLevel logLevel, string message)
        {
            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.Append("[").Append(logLevel).Append("]").Append(message).Append(Environment.NewLine);

            switch (logLevel)
            {
            case eLogLevel.ERROR:
                Console.ForegroundColor = ConsoleColor.Red;
                break;

            case eLogLevel.FATAL:
                Console.ForegroundColor = ConsoleColor.DarkRed;
                break;

            case eLogLevel.DEBUG:
                Console.ForegroundColor = ConsoleColor.DarkGreen;
                break;

            case eLogLevel.INFO:
                Console.ForegroundColor = ConsoleColor.Green;
                break;

            case eLogLevel.WARN:
                Console.ForegroundColor = ConsoleColor.DarkYellow;
                break;
            }
            Console.WriteLine(stringBuilder.ToString());
        }
Exemplo n.º 12
0
        public void ConsoleWriteLine(eLogLevel logLevel, string message)
        {
            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.Append("[").Append(logLevel).Append("]").Append(message).Append(Environment.NewLine);
            Console.WriteLine(stringBuilder.ToString());
        }
Exemplo n.º 13
0
 public static void ConsoleMessageEvent(eLogLevel logLevel, string messageToConsole)
 {
     mConsoleMessages.Add(new ConsoleMessage()
     {
         LogLevel = logLevel, MessageToConsole = messageToConsole
     });
 }
Exemplo n.º 14
0
 public Flags(bool enableAutoUpload = false, bool resetSettings = false, bool enableAutoParse = false, eLogLevel logLevel = eLogLevel.WARN)
 {
     EnableAutoUpload = enableAutoUpload;
     ResetSettings    = resetSettings;
     EnableAutoParse  = enableAutoParse;
     LogLevel         = logLevel;
     OverrideLogLevel = false;
 }
 /// <summary>
 /// 设置日志等级
 /// </summary>
 private void OnCommand_LogLV(string[] param)
 {
     if (param.Length == 2 && StringUtils.IsInteger(param[1]))
     {
         eLogLevel lv = (eLogLevel)(int.Parse(param[1]));
         Log.SetLogLv(lv);
     }
 }
Exemplo n.º 16
0
 private void LogBase(eLogLevel logLevel, string logString)
 {
     if (logLevel >= _setLogLevel)
     {
         using (var sw = File.AppendText(_loggingPath))
         { sw.WriteLine($"{DateTime.UtcNow} - {logLevel} :: {logString}"); }
     }
 }
Exemplo n.º 17
0
        /// <summary>
        /// Overloaded constructor that requires the log path and application name.  These are used
        /// to build the file path for the log.  The log level is also required, which sets the
        /// default message visibility.
        /// </summary>
        /// <param name="inLogPath">The location where the log file should be created.</param>
        /// <param name="inAppName">The name of the application, used in the log file name.</param>
        /// <param name="logLevel">Error, Warning, Informational, Debug</param>
        public CentralLogger(string inLogPath, string inAppName, eLogLevel logLevel)
        {
            logPath  = inLogPath;
            appName  = inAppName;
            LogLevel = logLevel;

            Initialize();
        }
Exemplo n.º 18
0
        protected Log(eLogLevel level, string _path, int restrict)
        {
            Buffer = new StringBuilder();

            this.level    = level;
            path          = "Log.log";
            this.restrict = restrict;
        }
Exemplo n.º 19
0
 public LogUnit(eLogLevel logLevel, string log, string stackTrace = null, eLogOutputType outputType = eLogOutputType.All, string savePath = null, eLogColor color = eLogColor.Default)
 {
     LogLevel   = logLevel;
     Log        = log;
     StackTrace = stackTrace;
     OutputType = outputType;
     Timer      = DateTime.Now;
     SavePath   = savePath;
     Color      = color;
 }
Exemplo n.º 20
0
        public void Write(eLogLevel eLog, string msg, params object[] args)
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendFormat("{0} ", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff"));
            sb.AppendFormat("{0} ", eLog.ToString().PadRight(5, ' '));
            sb.AppendFormat("[{0}] ", System.Threading.Thread.CurrentThread.ManagedThreadId);
            sb.AppendFormat(msg, args);
            Write(sb.ToString());
            m_console.Write(eLog, sb.ToString());
        }
Exemplo n.º 21
0
        /// <summary>
        /// Minimum constructor which requires the log path and application name.  These are used
        /// to build the file path for the log.
        /// </summary>
        /// <param name="logPath">The location where the log file should be created.</param>
        /// <param name="appName">The name of the application, used in the log file name.</param>
        public CentralLogger(string inLogPath, string inAppName)
        {
            logPath = inLogPath;
            appName = inAppName;

            // set default log level to informational. this will log all messages flagged as
            // informational, warning, or error.  if debug messages are necessary the LogLevel
            // must be set to Debug.
            LogLevel = eLogLevel.Informational;

            Initialize();
        }
Exemplo n.º 22
0
 public static void ToLog(eLogLevel logLevel, string messageToLog, Exception exceptionToLog = null, bool writeAlsoToConsoleIfNeeded = true, bool writeOnlyInDebugMode = false)
 {
     if (writeOnlyInDebugMode && CurrentAppLogLevel != eAppReporterLoggingLevel.Debug)
     {
         return;
     }
     if (logLevel == eLogLevel.ERROR)
     {
         ReporterData.ErrorCounter++;
     }
     WorkSpaceReporter.ToLog(logLevel, messageToLog, exceptionToLog, writeAlsoToConsoleIfNeeded);
 }
Exemplo n.º 23
0
 public void Write(eLogLevel eLog, string msg)
 {
     if (null == m_dic)
     {
         return;
     }
     if (!m_dic.ContainsKey(eLog))
     {
         return;
     }
     m_dic[eLog](msg);
 }
Exemplo n.º 24
0
        private string TypeToString(eLogLevel Level)
        {
            switch (Level)
            {
            case eLogLevel.APPLICATION:
                return("APP");

            case eLogLevel.ERROR:
                return("ERROR");
            }

            return(String.Empty);
        }
Exemplo n.º 25
0
        public static void Log(eLogLevel level, string message)
        {
            if (level < LogLevel)
            {
                return;
            }

            lock (Instance.LogString)
            {
                Instance.LogString.AppendLine(message);
                Instance.LastTextUpdate = DateTime.Now;
            }
        }
Exemplo n.º 26
0
        /// <summary>
        /// Синглтон лога
        /// </summary>
        /// <param name="logLevel">Уровень ведения лога</param>
        /// <param name="logPath">Путь к файлу-логу</param>
        /// <param name="logRestrict">Ограничение по строкам. Число 0 для работы без ограничения.</param>
        public static Log Instance(eLogLevel logLevel, string logPath, int logRestrict)
        {
            // Use 'Lazy initialization'
            if (instance == null)
            {
                lock (syncLock)
                {
                    if (instance == null)
                    {
                        instance = new Log(logLevel, logPath, logRestrict);
                    }
                }
            }

            return(instance);
        }
Exemplo n.º 27
0
        public static void ToLog(eLogLevel logLevel, string messageToLog, Exception exceptionToLog = null, bool writeAlsoToConsoleIfNeeded = true, bool writeOnlyInDebugMode = false)
        {
            try
            {
                if (writeOnlyInDebugMode)
                {
                    if (CurrentAppLogLevel != eAppLogLevel.Debug)
                    {
                        return;
                    }
                }
                switch (logLevel)
                {
                case eLogLevel.DEBUG:
                    log.Debug(messageToLog, exceptionToLog);
                    break;

                case eLogLevel.ERROR:
                    log.Error(messageToLog, exceptionToLog);
                    break;

                case eLogLevel.FATAL:
                    log.Fatal(messageToLog, exceptionToLog);
                    break;

                case eLogLevel.INFO:
                    log.Info(messageToLog, exceptionToLog);
                    break;

                case eLogLevel.WARN:
                    log.Warn(messageToLog, exceptionToLog);
                    break;

                default:
                    log.Info(messageToLog, exceptionToLog);
                    break;
                }

                //if (writeAlsoToConsoleIfNeeded && AddAllReportingToConsole)
                ToConsole(logLevel.ToString() + ": " + messageToLog, exceptionToLog);
            }
            catch (Exception)
            {
                //failed to write to log
            }
        }
Exemplo n.º 28
0
        /// <summary>
        /// accepts writeToConsole, which will write the message to standard output regardless
        /// of the logging level.
        /// </summary>
        /// <param name="message"></param>
        /// <param name="msgLevel"></param>
        /// <param name="writeToConsole"></param>
        public void WriteLine(string message, eLogLevel msgLevel, bool writeToConsole)
        {
            if (msgLevel <= LogLevel)
            {
                WriteToFile(DateTime.Now.ToString() + " [" + msgLevel.GetStringValue() + "] " + message);
            }

            // by default, if an error message output to standard error as well as log file
            if (msgLevel == eLogLevel.Error)
            {
                WriteToStandardError(message);
            }

            if (writeToConsole)
            {
                WriteToStandardOutput(message);
            }
        }
Exemplo n.º 29
0
        void _Log(string tag, eLogLevel logLevel, string message, Exception exception)
        {
            if (!IsValidLogLevel(logLevel))
            {
                return;
            }

            __Log(new LogInfo
            {
                DateTime      = DateTime.Now,
                SequnceNumber = Interlocked.Increment(ref mSequenceNumber),
                Tag           = tag,
                ThreadID      = Thread.CurrentThread.ManagedThreadId,
                LogLevel      = logLevel,
                Message       = message,
                Exception     = exception
            });
        }
Exemplo n.º 30
0
        public static void ToLog(eLogLevel logLevel, string messageToLog, Exception exceptionToLog = null)
        {
            if (ReportAllAlsoToConsole)
            {
                ToConsole(logLevel, messageToLog, exceptionToLog);
            }

            if (logLevel == eLogLevel.DEBUG && AppLoggingLevel != eAppReporterLoggingLevel.Debug)
            {
                return;
            }

            if (logLevel == eLogLevel.ERROR || logLevel == eLogLevel.FATAL)
            {
                ReporterData.ErrorCounter++;
            }

            WorkSpaceReporter.ToLog(logLevel, messageToLog, exceptionToLog);
        }
Exemplo n.º 31
0
 public CategoryConfig(eLogLevel level, String file, bool useIndependant)
 {
     _level = level;
     _logFile = file;
     _useIndependantLog = useIndependant;
 }
        protected void Log(string logText, eLogLevel logLevel)
        {
            if (performLog)
            {
                switch (logLevel)
                {
                    case eLogLevel.eInfo:
                        log.Info(logText);
                        break;
                    case eLogLevel.eError:
                        log.Error(logText);
                        break;
                    case eLogLevel.eFatal:
                        log.Fatal(logText);
                        break;
                    default:
                        //do nothing
                        break;
                }

            }
        }
Exemplo n.º 33
0
 public static void SetLogLevel(eLogLevel logLevel)
 {
     Instance.m_currentLogLevel = logLevel;
 }
Exemplo n.º 34
0
        public static void LogMessage(eLogCategory category, eLogLevel level, String message)
        {
#if !FINAL
            if (null != ARKLogger.Singleton)
            {
                ARKLogger.Singleton.Write(category, level, message);
            }
            else if (eLogLevel.System == level)
            {
                // Fallback if the debugging system hasn't been initialized yet.
                UnityEngine.Debug.Log(message);
            }
#endif
        }
Exemplo n.º 35
0
        private void Write(eLogCategory category, eLogLevel level, String message)
        {
            // Ensure the level is within desired levels
            CategoryConfig conf = configuration[category];
            if (level > conf._level)
            {
                return;
            }

            // Get the desired Output Stream for the Category
            StreamWriter outputStream;
            if (conf._useIndependantLog)
            {
                if (logWriters.ContainsKey(conf._logFile))
                {
                    outputStream = logWriters[conf._logFile];
                }
                else
                {
                    outputStream = new StreamWriter(conf._logFile, false, System.Text.Encoding.UTF8);
                    logWriters.Add(conf._logFile, outputStream);
                }
            }
            else
            {
                // Default File Writer
                outputStream = logWriters[generalLog];
            }

            // Add timestamp to the message
            if (AddTimeStamp)
            {
                DateTime current = DateTime.Now;
                message = string.Format("<{0:HH:mm:ss:fff}> [{1}] : {2} - {3}",
                                        current, level.ToString("G").PadRight(8), 
                                        category.ToString("G").PadRight(8), 
                                        message);
            }

            // Commit the message to the file
            if (null != outputStream)
            {
                outputStream.WriteLine(message);
                outputStream.Flush();
            }

#if !FINAL
            // Echo to the Console if a debug
            if (EchoToConsole)
            {
                if ((eLogLevel.Trace == level) ||
                    (eLogLevel.Info == level))
                {
                    UnityEngine.Debug.Log(message);
                }
                else if (eLogLevel.Warning == level)
                {
                    UnityEngine.Debug.LogWarning(message);
                }
                else // Both Error and Assert
                {
                    UnityEngine.Debug.LogError(message);
                }
            }
#endif
        }