Tuesday 8 April 2014

Logging

In the previous post we used the trace class to implement simple logging to a text file, when you're working in dev and no one needs to see these logs then it makes sense.but if you're creating an application that is complex and will need to be trouble shot, then maybe logging and tracing to a text file isn't an option. what you should do instead is implement logging to the system log files. we can use the EevntLog Class to implement just such functionality.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Logging
{
    class Person
    {
        static int runningID = 0;
        public int Id { get; private set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }

        public Person(string FirstName, string LastName)
        {
            Id = runningID++;
            this.FirstName = FirstName;
            this.LastName = LastName;
        }

        public override string ToString()
        {
            return string.Format("{0} {1} {2}",Id,FirstName,LastName);
        }

    }
   
    class Program
    {
        static void Main(string[] args)
        {
            if (!EventLog.SourceExists("Person Maker"))
                EventLog.CreateEventSource("Person Maker", "Application");

            int selection =-1;
            var people = new SortedList<int,Person>();

            do{
                do
                {
                    Console.WriteLine("1) Add Person\n2) Remove Person\n3) list People\n0) Exit");
                } while (!int.TryParse(Console.ReadLine(), out selection));
               
                switch(selection)
                {
                    case 1:
                        var p = CreatePerson();
                        people.Add(p.Id, p);
                        break;
                    case 2:
                        RemovePerson(ref people);
                        break;
                    case 3:
                        ListPeople(people);
                        break;
                }

            }while(selection != 0);
        }

        public static Person CreatePerson()
        {
            Console.WriteLine("enter in a persons first Name");
            string fname = Console.ReadLine();

            Console.WriteLine("enter in a persons last name");
            string lname = Console.ReadLine();

            var p = new Person(fname, lname);
            var m = string.Format("create person {0}", p.ToString());
            EventLog.WriteEntry("Person Maker", m , EventLogEntryType.Information, 1001);
            return p;
        }

        public static bool RemovePerson(ref SortedList<int,Person> people)
        {
            string LogMsg = string.Empty;
            int logID = -1;
            var logType = EventLogEntryType.Information;

            if (people.Count > 0)
            {
                foreach(var p in people)
                {
                    Console.WriteLine(p.Value.ToString());
                }

                int key;
                Console.WriteLine("Enter in the id of the person to delete");
                string keySTR = Console.ReadLine();
                if (int.TryParse(keySTR, out key))
                {
                    if (people.ContainsKey(key))
                    {
                        int index = people.IndexOfKey(key);
                        people.RemoveAt(index);
                        return true;
                    }
                    LogMsg = "Couldn't delete form list, no such key: " + key;
                    logID = 1002;
                    logType = EventLogEntryType.Error;
                }
                else
                {
                    LogMsg = "Couldn't delete form list, not a valid key: " + keySTR;
                    logID = 1002;
                    logType = EventLogEntryType.Error;
                }
               
            }
            else
            {
                LogMsg = "Couldn't delete form list, list is empty";
                logID = 1001;
                logType = EventLogEntryType.Information;
            }

            EventLog.WriteEntry("Person Maker", LogMsg, logType, logID);
           
            return false;
        }

        public static void ListPeople(SortedList<int,Person> people)
        {
            if (people.Count > 0)
                foreach (var p in people)
                    Console.WriteLine(p.Value.ToString());
            else
                EventLog.WriteEntry("Person Maker", "Listed People", EventLogEntryType.Information , 1002);
        }
    }

  
}


now if we run the above, more or less very similar to our previous example with the tracing

what we did was basically

  • created user paul chooch
  • created user magda tyvonok
  • deleted a user by a key that doesn't exist

now all this gets logged to our system logs, if we open the ones up for application we'll see

a much more robust solution then logging to a text file.

No comments:

Post a Comment