Advertisement
Guest User

Untitled

a guest
Dec 7th, 2013
762
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.63 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3.  
  4. namespace Demo1
  5. {
  6.     public class MyLogClass
  7.     {
  8.         public enum LogLevel
  9.         {
  10.             Info,
  11.             Warn,
  12.             Error,
  13.             Fatal
  14.         }
  15.  
  16.         const string LOG_FILE_NAME = "Demo1_Log.txt";
  17.         private readonly string _folderSave;
  18.         private readonly string _logName;
  19.  
  20.         public MyLogClass(string folderSave, string logName)
  21.         {
  22.             _folderSave = folderSave;
  23.             _logName = logName;
  24.  
  25.             if (!_folderSave.EndsWith(@"\"))
  26.                 _folderSave += @"\";
  27.            
  28.             if (!Directory.Exists(_folderSave))
  29.                 throw new DirectoryNotFoundException("Directory Not Found");
  30.         }
  31.  
  32.         public void Write(LogLevel logLevel, string dataToSave)
  33.         {
  34.             try
  35.             {
  36.                 using (FileStream fs = new FileStream(_folderSave + LOG_FILE_NAME, FileMode.Append))
  37.                 {
  38.                     using (StreamWriter sw = new StreamWriter(fs))
  39.                     {
  40.                         string tmpDataToSave = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " ";
  41.                         tmpDataToSave += logLevel.ToString().PadRight(6).ToUpper();
  42.                         tmpDataToSave += _logName.PadRight(15) + " - ";
  43.                         tmpDataToSave += dataToSave.Replace(Environment.NewLine, " ");
  44.                        
  45.                         sw.WriteLine(tmpDataToSave);
  46.                     }
  47.                 }
  48.             }
  49.             catch (Exception)
  50.             {
  51.                 // No hacer nada
  52.             }
  53.         }
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement