Saturday, 17 June 2017

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 sorta makes sense. However if you're creating an application that is complex what you should do is implement logging to the system log files. This can be accomplished via the EevntLog class to implement just such functionality.

using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace Logging
{
    class Person
    {
        static int runningID = 0;
        public int Id { get; private set; } = runningID++;
        public string Name { get; set; }
        public Person(string Name) { this.Name = Name; }
        public override string ToString() { return string.Format($"{Id} {Name}"); }
    }

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

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

            do
            {
                do Console.WriteLine("1) Add Person\n2) Remove Person\n" +
                                     "3) 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(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();

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

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

            try
            {
                if (people.Count > 0)
                {
                    foreach (var p in people)
                        Console.WriteLine($"{p.Key} {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))
                        {
                            people.Remove(key);
                            LogMsg = "deleted form list id " + key;
                            logID = 1001;
                            logType = EventLogEntryType.Information;
                            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;
                }
                return false;

            }
            finally
            {
                EventLog.WriteEntry("Person Maker", LogMsg, logType, logID);
            }

        }

        public static void ListPeople(Dictionary<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