Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Windows.Forms;
  7. using System.Data.SqlClient;
  8.  
  9. namespace Onesource
  10. {
  11.  
  12.     public class log
  13.     {
  14.  
  15.         /// <summary>
  16.         /// Find the application data path
  17.         /// </summary>
  18.         /// <returns></returns>
  19.         public static string LogDataPath()
  20.         {
  21.             string path = Application.CommonAppDataPath;            
  22.             return path;
  23.         }
  24.  
  25.         /// <summary>
  26.         /// Returns the name of the log file for today
  27.         /// </summary>
  28.         /// <returns></returns>
  29.         public static string LogDataFile()
  30.         {
  31.             string path = LogDataPath();
  32.             string filename = string.Format(@"{0}\{1}.{2}", path, DateTime.Today.ToString("yyyy.MM.dd"), "log");
  33.             return filename;
  34.         }
  35.  
  36.         /// <summary>
  37.         /// Appends the message to the current text log.
  38.         /// </summary>
  39.         /// <param name="Message">the messaget to be logged.</param>
  40.         public static void writeMessage(string Message)
  41.         {
  42.             string filename = LogDataFile();
  43.             StreamWriter w = File.AppendText(filename);
  44.             w.WriteLine(string.Format("{0}: {1}", DateTime.Now.ToShortTimeString(), Message));
  45.             w.Flush();
  46.             w.Close();
  47.             w.Dispose();
  48.         }
  49.  
  50.         /// <summary>
  51.         /// Writes the message to the database AND the text file.
  52.         /// </summary>
  53.         /// <param name="myConnection">an open SQL Connection</param>
  54.         /// <param name="Message">the message to log</param>
  55.         public static void writeMessage(SqlConnection myConnection, string Message)
  56.         {
  57.             writeMessage(Message);
  58.             writetodatabase(myConnection, Message);
  59.         }
  60.  
  61.         /// <summary>
  62.         /// writes a message to the database only.
  63.         /// </summary>
  64.         /// <param name="myConnection">an open SQL Connection</param>
  65.         /// <param name="errmessage">The message</param>
  66.         public static void writetodatabase(SqlConnection myConnection, string errmessage)
  67.         {
  68.             string sql="INSERT INTO ErrorLog ([errormessage],[errorversion]) VALUES (@message,@version)";
  69.             SqlCommand myCommand = new SqlCommand(sql, myConnection);
  70.             try
  71.             {
  72.                 myCommand.Parameters.AddWithValue("@message", errmessage);
  73.                 myCommand.Parameters.AddWithValue("@version", Application.ProductVersion);
  74.                 myCommand.ExecuteNonQuery();
  75.             }
  76.             catch (Exception ex)
  77.             {
  78.                 log.writeMessage(string.Format("Error Logging Error to Database: {0}",ex.Message));
  79.             }
  80.         }
  81.  
  82.         /// <summary>
  83.         /// constructs a message from a format string and a list of arguments
  84.         /// </summary>
  85.         /// <param name="formatString">the format string i.e. "error {0}: {1}"</param>
  86.         /// <param name="args">a list of params to place in the format string</param>
  87.         public static void writeMessage(string formatString, params object[] args)
  88.         {
  89.             writeMessage(String.Format(formatString, args));
  90.         }
  91.  
  92.         /// <summary>
  93.         /// Writes a formatted message to the database and the text error log.
  94.         /// </summary>
  95.         /// <param name="myConnection">an open SQL Connection</param>
  96.         /// <param name="formatString">a foramting string</param>
  97.         /// <param name="args">the parameters to place in the formatting string</param>
  98.         public static void writeMessage(SqlConnection myConnection, string formatString, params object[] args)
  99.         {
  100.             string theMessage = String.Format(formatString, args);
  101.             writeMessage(theMessage);
  102.             writetodatabase(myConnection, theMessage);
  103.         }
  104.  
  105.         /// <summary>
  106.         /// opens a windows browser to the logs directory.
  107.         /// </summary>
  108.         public static void ShowLogFolder()
  109.         {
  110.             string path = LogDataPath();
  111.             string windir = Environment.GetEnvironmentVariable("WINDIR");
  112.             System.Diagnostics.Process prc = new System.Diagnostics.Process();
  113.             prc.StartInfo.FileName = windir + @"\explorer.exe";
  114.             prc.StartInfo.Arguments = path;
  115.             prc.Start();
  116.         }
  117.     }
  118. }
  119.  
  120.