Esempio n. 1
0
        /// <summary>
        /// Writes a message into the log. This is the core method!
        /// </summary>
        /// <param name="msg">The message to print.</param>
        /// <param name="type">The <see cref="LogType"/> tag.</param>
        public virtual void WriteLog(string msg, LogType type)
        {
            if (DisableLogging || !isDebug && (type == LogType.DEBUG || type == LogType.VERBOSE))
            {
                return;
            }

            for (int i = 0; i < 3; i++)
            {
                try
                {
                    Directory.CreateDirectory(logFolderPath);

                    if (!File.Exists(filePath) && fileName != null)
                    {
                        string logStart = LogFileStart;
                        logStart = logStart.Replace("[DATE]", DateTimeManager.GetDate());
                        logStart = logStart.Replace("[TIME]", DateTimeManager.GetTime());
                        logStart = logStart.Replace("[NAME]", fileName);
                        File.WriteAllText(filePath, logStart);
                    }

                    StringBuilder logBuilder = new StringBuilder();
                    string        identifier = type.ToPrefix();

                    StringBuilder logFileBuilder = new StringBuilder(identifier + " ");
                    Console.Write(identifier);

                    //if (!string.IsNullOrWhiteSpace(prefix))
                    //{
                    //    prefix = string.Format(" [{0}]", prefix);
                    //    logFileBuilder.Append(prefix);
                    //    Console.Write(foregroundColor);
                    //}

                    if (sessionId > -1)
                    {
                        msg = string.Format("({0}) {1}: {2}", sessionId, DateTimeManager.GetTimestamp(), msg);
                    }
                    else
                    {
                        msg = string.Format("{0}: {1}", DateTimeManager.GetTimestamp(), msg);
                    }

                    logFileBuilder.Append(msg);
                    logBuilder.Append(msg);

                    if (fileName != null)
                    {
                        if (type != LogType.VERBOSE)
                        {
                            File.AppendAllText(filePath, logFileBuilder.ToString() + "\r");
                        }
                        else
                        {
                            File.AppendAllText(filePath + ".verbose", logFileBuilder.ToString() + "\r");
                        }
                    }

                    if (isDebug)
                    {
                        Console.WriteLine((AddIdentifierToConsole ? logFileBuilder : logBuilder).ToString());
                    }

                    if (AttachedConsole != null && LogToAttachedConsole)
                    {
                        if (fileName != null && SHOW_LOG_NAME)
                        {
                            AttachedConsole.WriteString(string.Format("({0}) {1}\n", fileName, AddIdentifierToConsole ? logFileBuilder : logBuilder), type);
                        }
                        else
                        {
                            AttachedConsole.WriteString(string.Format("{0}\n", AddIdentifierToConsole ? logFileBuilder : logBuilder), type);
                        }
                    }
                    break;
                }
                catch
                {
                }
            }
        }