Ejemplo n.º 1
0
 private static void writeToJson(string filePath, MessageJson msgJson, PLoggerElement target)
 {
     if (File.Exists(filePath))
     {
         List <string> lines = File.ReadAllLines(filePath).ToList();
         File.WriteAllLines(filePath, lines.GetRange(0, lines.Count - 3).ToArray());
         using (StreamWriter sw = File.AppendText(filePath))
         {
             sw.WriteLine(",");
             sw.WriteLine(JsonConvert.SerializeObject(msgJson));
             sw.WriteLine("]");
             sw.WriteLine("}");
             sw.WriteLine("}");
         }
     }
     else
     {
         using (StreamWriter sw = File.CreateText(filePath))
         {
             sw.WriteLine("{ " + "\"" + target.FileName + "\" : ");
             sw.WriteLine("{ " + "\"Logs\" : [ ");
             sw.WriteLine(JsonConvert.SerializeObject(msgJson));
             sw.WriteLine("]");
             sw.WriteLine("}");
             sw.WriteLine("}");
         }
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Check if .config `saveType` = "file" to save it into a file
        /// </summary>
        /// <param></param>
        private static void whichMethodToLog()
        {
            try
            {
                foreach (PLoggerElement target in GetConfig().PLoggerInstances)
                {
                    if (CheckMessageLevel(target) <= _level)
                    {
                        switch (target.SaveType)
                        {
                        case "mysql":
                        {
                            string connection = String.Format($"SERVER={target.DbHost};DATABASE={target.DbName};UID={target.DbUser};PASSWORD={target.DbPassword};");
                            writeToMySQL(new MySqlConnection(connection), target.DetailMode, target.ActivityId);
                            break;
                        }

                        case "sql":
                        {
                            string connection = String.Format($"Data Source={target.DbHost};Initial Catalog={target.DbName};User ID={target.DbUser};Password={target.DbPassword};");
                            writeToSQL(new SqlConnection(connection), target.DetailMode, target.ActivityId);
                            break;
                        }

                        case "json":
                        {
                            MessageJson message = new MessageJson();
                            message.unique_id      = (target.ActivityId) ? getActivityId() : null;
                            message.type           = _type.Substring(3).Replace(" ", String.Empty);
                            message.username       = Environment.UserName;
                            message.message        = _msg;
                            message.date           = CurrentDate();
                            message.created_at     = CurrentTimestamp();
                            message.passed_through = (target.DetailMode) ? _functionPassThrough : null;

                            if (String.IsNullOrEmpty(target.FilePath))
                            {
                                writeToJson(string.Format(Directory.GetCurrentDirectory() + "\\" + target.FileName + $"_{ CurrentDate().Replace('/', '-') }") + ".json", message, target);
                            }
                            else
                            {
                                writeToJson(string.Format(target.FilePath + "\\" + target.FileName + $"_{ CurrentDate().Replace('/', '-') }") + ".json", message, target);
                            }

                            break;
                        }

                        case "file":
                        {
                            if (String.IsNullOrEmpty(target.FilePath))
                            {
                                writeToFile(string.Format(Directory.GetCurrentDirectory() + "\\" + target.FileName + $"_{ CurrentDate().Replace('/', '-') }") + ".log", CreateMessage(target));
                            }
                            else
                            {
                                writeToFile(string.Format(target.FilePath + "\\" + target.FileName + $"_{ CurrentDate().Replace('/', '-') }") + ".log", CreateMessage(target));
                            }
                            break;
                            //we call the function named writeToFile with those parameters { FilePath, TheMessage }
                        }

                        default:
                        {
                            writeToFile(string.Format(Directory.GetCurrentDirectory() + "\\" + target.FileName + $"_{ CurrentDate().Replace('/', '-') }") + ".log", InternalError("Verify your saveType in the app.config"));
                            break;
                        }
                        }
                    }
                }
            }
            catch (Exception e) //if target.FileName doesn't exist
            {
                writeToFile(string.Format(Directory.GetCurrentDirectory() + "\\" + "ExceptionErrorPLogger" + $"_{ CurrentDate().Replace('/', '-') }") + ".log", InternalError(e));
            }
        }