daily pastebin goal
38%
SHARE
TWEET

Untitled

a guest Feb 23rd, 2019 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Configuration;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7.  
  8. namespace ApiCore21.Utils
  9. {
  10.     public class Logs
  11.     {
  12.         public enum logs { DEBUG, INFO, WARN, ERROR, FATAL }
  13.  
  14.         public static void WriteToLogFile(logs typeLog, string messageErro, string stackTrace)
  15.         {
  16.  
  17.             #region Folder
  18.             string pathFolder = string.Format("{0}{1}", AppDomain.CurrentDomain.BaseDirectory, "logs");
  19.             if (!Directory.Exists(pathFolder))
  20.             {
  21.                 Directory.CreateDirectory(pathFolder);
  22.             }
  23.             #endregion
  24.  
  25.             string filepath = string.Format("{0}\\{1}.txt", pathFolder, "logs");
  26.  
  27.             if (!File.Exists(filepath))
  28.             {
  29.                 #region CreateFile
  30.                 CreateFileLog(typeLog, messageErro, filepath, stackTrace);
  31.                 #endregion
  32.             }
  33.             else
  34.             {
  35.                 #region Size and Append
  36.                 if (ConvertBytesToMegabytes(File.ReadAllBytes(filepath).Length) >= 20.0)
  37.                 {
  38.                     string[] txtList = Directory.GetFiles(pathFolder, "*.txt");
  39.                     foreach (string f in txtList)
  40.                     {
  41.                         File.Delete(f);
  42.                     }
  43.  
  44.                     CreateFileLog(typeLog, messageErro, filepath, stackTrace);
  45.                 }
  46.                 else
  47.                 {
  48.                     #region Append
  49.                     using (StreamWriter sw = File.AppendText(filepath))
  50.                     {
  51.                         sw.WriteLine(string.Format("{0} [{1}]: {2} {3}", DateTime.Now, typeLog, messageErro, stackTrace));
  52.                     }
  53.                     #endregion
  54.                 }
  55.                 #endregion
  56.             }
  57.         }
  58.  
  59.         private static void CreateFileLog(logs typeLog, string messageErro, string filepath, string stackTrace)
  60.         {
  61.             using (StreamWriter sw = File.CreateText(filepath))
  62.             {
  63.                 sw.WriteLine(string.Format("{0} [{1}]: {2} {3}", DateTime.Now, typeLog, messageErro, stackTrace));
  64.             }
  65.         }
  66.  
  67.         private static double ConvertBytesToMegabytes(long bytes)
  68.         {
  69.             return (bytes / 1024f) / 1024f;
  70.         }
  71.     }
  72. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top