Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- using System.Windows.Forms;
- using System.Data.SqlClient;
- namespace Onesource
- {
- public class log
- {
- /// <summary>
- /// Find the application data path
- /// </summary>
- /// <returns></returns>
- public static string LogDataPath()
- {
- string path = Application.CommonAppDataPath;
- return path;
- }
- /// <summary>
- /// Returns the name of the log file for today
- /// </summary>
- /// <returns></returns>
- public static string LogDataFile()
- {
- string path = LogDataPath();
- string filename = string.Format(@"{0}\{1}.{2}", path, DateTime.Today.ToString("yyyy.MM.dd"), "log");
- return filename;
- }
- /// <summary>
- /// Appends the message to the current text log.
- /// </summary>
- /// <param name="Message">the messaget to be logged.</param>
- public static void writeMessage(string Message)
- {
- string filename = LogDataFile();
- StreamWriter w = File.AppendText(filename);
- w.WriteLine(string.Format("{0}: {1}", DateTime.Now.ToShortTimeString(), Message));
- w.Flush();
- w.Close();
- w.Dispose();
- }
- /// <summary>
- /// Writes the message to the database AND the text file.
- /// </summary>
- /// <param name="myConnection">an open SQL Connection</param>
- /// <param name="Message">the message to log</param>
- public static void writeMessage(SqlConnection myConnection, string Message)
- {
- writeMessage(Message);
- writetodatabase(myConnection, Message);
- }
- /// <summary>
- /// writes a message to the database only.
- /// </summary>
- /// <param name="myConnection">an open SQL Connection</param>
- /// <param name="errmessage">The message</param>
- public static void writetodatabase(SqlConnection myConnection, string errmessage)
- {
- string sql="INSERT INTO ErrorLog ([errormessage],[errorversion]) VALUES (@message,@version)";
- SqlCommand myCommand = new SqlCommand(sql, myConnection);
- try
- {
- myCommand.Parameters.AddWithValue("@message", errmessage);
- myCommand.Parameters.AddWithValue("@version", Application.ProductVersion);
- myCommand.ExecuteNonQuery();
- }
- catch (Exception ex)
- {
- log.writeMessage(string.Format("Error Logging Error to Database: {0}",ex.Message));
- }
- }
- /// <summary>
- /// constructs a message from a format string and a list of arguments
- /// </summary>
- /// <param name="formatString">the format string i.e. "error {0}: {1}"</param>
- /// <param name="args">a list of params to place in the format string</param>
- public static void writeMessage(string formatString, params object[] args)
- {
- writeMessage(String.Format(formatString, args));
- }
- /// <summary>
- /// Writes a formatted message to the database and the text error log.
- /// </summary>
- /// <param name="myConnection">an open SQL Connection</param>
- /// <param name="formatString">a foramting string</param>
- /// <param name="args">the parameters to place in the formatting string</param>
- public static void writeMessage(SqlConnection myConnection, string formatString, params object[] args)
- {
- string theMessage = String.Format(formatString, args);
- writeMessage(theMessage);
- writetodatabase(myConnection, theMessage);
- }
- /// <summary>
- /// opens a windows browser to the logs directory.
- /// </summary>
- public static void ShowLogFolder()
- {
- string path = LogDataPath();
- string windir = Environment.GetEnvironmentVariable("WINDIR");
- System.Diagnostics.Process prc = new System.Diagnostics.Process();
- prc.StartInfo.FileName = windir + @"\explorer.exe";
- prc.StartInfo.Arguments = path;
- prc.Start();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement