Sunday, March 9, 2014

Debug


EventLog


  1. EventLog is a service, offered by Microsoft Windows.
  2. It is used to store the exceptions / warnings / any messages permanently, even though the application is closed.
  3. Let us imagine you have done a live project and issue it to the client.
  4. At run time there may be so many exceptions. But after few days, you went to the client location, and want to know what kind of run time errors were coming in your application.
  5. Then no information is available with you.
  6. So, in this case, you require some thing, that stores the exceptions information automatically, for further information, which helps you while you want to debug your application and develop a next version.
  7. That kind of service is EventLog.
  8. To open EventLog and view the current events: Start –> Control Panel –> Administrative Tools –> Event Viewer.

Writing a new entry programmatically:
System.Diagnostics.EventLog.WriteEntry("project name", "message", EventLogEntryType.Error);


Program of Event Log
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
namespace EventLogDemo
{
class Program
 {
  static void Main(string[] args)
  {
   try
   {
    string filename = "c:\\sample.txt";
    StreamReader sr = new StreamReader(filename);
    string content = sr.ReadToEnd();
    Console.WriteLine("The file content is:\n");
    Console.WriteLine(content);
   }
  catch (Exception ex)
  {
   string EventMsg = ex.Message + "\n\n" + ex.StackTrace;
   EventLog.WriteEntry("My Sample Project 1.0", EventMsg, 
   EventLogEntryType.Error);
   Console.WriteLine(ex.Message);
  }
 finally
  {
   Console.WriteLine("Press any key to exit..");
   Console.Read();
  } 
  }
 }
}