SimpleLogging.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Diagnostics;

namespace Skippy
{
    //http://coercedcode.blogspot.com/2007/08/simple-logging-class-in-c.html

    //    This class is simple there are three static methods. One to write to a log file, one to write to the windows event log, and one to generate a log file name which can be used to create a log file in the directory which contains the executable of whatever application is calling it, or in the case of a web site it will be in the web site root directory.

    //Sample Usage:

    //Logging.WriteToEventLog("MyTestApp", "Testing", System.Diagnostics.EventLogEntryType.Information);

    //Logging.WriteToLog(Logging.GenerateDefaultLogFileName("MyTestApp"), "Testing");
    //This will give you a file named MyTestApp_8_16_2007.log in your application directory and log entries that look like this: 8/16/2007 3:35:52 PM Testing

    //If you look in your application event log you will see an entry like this: Event Type: Information Event Source: MyTestApp Event Category: None Event ID: 0 Date: 8/16/2007 Time: 3:35:54 PM User: N/A Computer: ComputerNameHere Description: Testing

    //Updated 8-1-2008 with using statement to dispose of the file stream in the event of a problem 




    public class Logging
    {

        public static string GenerateDefaultLogFileName(string BaseFileName)
        {

            return AppDomain.CurrentDomain.BaseDirectory + "\\" + BaseFileName + "_" + DateTime.Now.Month + "_" + DateTime.Now.Day + "_" + DateTime.Now.Year + ".log";

        }
        


        /// <summary>
        /// Pass in the fully qualified name of the log file you want to write to
        /// and the message to write
        /// </summary>
        /// <param name="LogPath"></param>
        /// <param name="Message"></param>
        public static void WriteToLog(string LogPath, string Message)
        {
            try
            {
                using (StreamWriter s = File.AppendText(LogPath))
                {
                    s.WriteLine(DateTime.Now + "\t" + Message);
                    //s.WriteLine();
                    //s.WriteLine();
                }
            }

            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
            }

        }





        /// <summary>
        /// Writes a message to the application event log
        /// /// </summary>
        /// <param name="Source">Source is the source of the message ususally you will want this to be the application name</param>
        /// <param name="Message">message to be written</param>
        /// <param name="EntryType">the entry type to use to categorize the message like for exmaple error or information</param>
        public static void WriteToEventLog(string Source, string Message, System.Diagnostics.EventLogEntryType EntryType)
        {
            try
            {
                if (!EventLog.SourceExists(Source))
                {
                    EventLog.CreateEventSource(Source, "Application");
                }

                EventLog.WriteEntry(Source, Message, EntryType);
            }

            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
            }

        }

    }
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s