Don't like ads? PRO users don't see any ads ;-)
Guest

logger.cs

By: a guest on Aug 9th, 2012  |  syntax: None  |  size: 2.86 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6.  
  7. namespace Arma2NETMySQLPlugin
  8. {
  9.     class Logger
  10.     {
  11.         public enum loggerState
  12.         {
  13.             Started,
  14.             Stopped
  15.         }
  16.  
  17.         public enum LogType
  18.         {
  19.             Info,
  20.             Warning,
  21.             Error
  22.         }
  23.  
  24.         private static loggerState state = loggerState.Stopped;
  25.         public static loggerState State { get { return state; } }
  26.  
  27.         private static FileStream fs = null;
  28.         private static StreamWriter sw = null;
  29.  
  30.         public Logger()
  31.         {
  32.             //Constructor
  33.             if (State == loggerState.Stopped)
  34.             {
  35.                 //check to see if the logs folder exists, if not create it
  36.                 if (!System.IO.Directory.Exists("logs"))
  37.                 {
  38.                     System.IO.Directory.CreateDirectory("logs");
  39.                 }
  40.  
  41.                 //Setup file streams
  42.                 DateTime dateValue = new DateTime();
  43.                 dateValue = DateTime.Now;
  44.                 string relativepath = Path.Combine("logs", dateValue.ToString("MM-dd-yyyy_HH-mm-ss") + ".log");
  45.                 fs = new FileStream(relativepath, FileMode.Append);
  46.                 sw = new StreamWriter(fs);
  47.  
  48.                 state = loggerState.Started;
  49.             }
  50.         }
  51.  
  52.         public static void addMessage(LogType type, string message)
  53.         {
  54.             if (State == loggerState.Started)
  55.             {
  56.                 DateTime time = new DateTime();
  57.                 time = DateTime.Now;
  58.                 string towrite = type.ToString() + ": " + time.ToString("HH:mm:ss - ") + message;
  59.  
  60.                 //This locks file writing for a second, this prevents multiple external threads to be writing to the file
  61.                 //using this method at exactly the same time.
  62.                 //http://msdn.microsoft.com/en-us/library/c5kehkcz.aspx
  63.                 //http://www.dotnetperls.com/lock
  64.                 lock (sw)
  65.                 {
  66.                     sw.WriteLine(towrite);
  67.                     sw.Flush();
  68.                 }
  69.             }
  70.             else
  71.             {
  72.                 Console.WriteLine("ERROR: Tried to add message when logger is down.\n**\t{0}", message);
  73.             }
  74.         }
  75.  
  76.         public static void Stop()
  77.         {
  78.             if (State == loggerState.Started)
  79.             {
  80.                 try
  81.                 {
  82.                     sw.Flush();
  83.                     sw.Close();
  84.                     fs.Close();
  85.                     state = loggerState.Stopped;
  86.                 }
  87.                 catch (Exception ex)
  88.                 {
  89.                     Console.WriteLine("EXCEPTION: An exception occured while stopping the logger.\n**\t{0}", ex.ToString());
  90.                 }
  91.             }
  92.         }
  93.     }
  94. }