Wednesday 12 December 2012

Logging Event with C# and .Net Framework into Event Log

We can use Event log to store any error that our application generates as a result of testing or daily usage by the users. It is very useful when we are trying to log error messages which are events as well. The primary advantage is that we can see very detailed report of errors that occurred. This is much better mechanism of capturing errors since users may not always describe to us what the nature of the error is that they are getting via the interface. However, there are couple very serous considerations we have to take into account. First, Event Log must be run under very strict security framework. Second, Event Log is process intensive operation. Finally, Event Log can throw exceptions if Event Log is filled. Event log should be avoided with partial trust assemblies.   In order to create Event Log and write to it we need to use EventLog class.

public static void CreateEventLog()
{
     
EventLog DemoLog = new EventLog("Demo");
     DemoLog.Source = "Demo";
     //WriteEntry
     DemoLog.WriteEntry("CreateEventLog call", EventLogEntryType.Information);
} This will crate an entry in our list of Event Logs.     If we want to remove Event Log that we just created we’ll do it the following waypublic static void DeleteEvantLog()
{
   EventLog.Delete("Demo");
}
  EventLog is also useful to work with Built-in Event Logs such as Application Log, Security Log and System Log. Example of writing to Event Logpublic static void WriteToApplicationEventLog()
{
            EventLog DemoLog = new EventLog("Application");
            

DemoLog.Source = "DemoApp";
            DemoLog.WriteEntry("Written to Application Log",  EventLogEntryType.Information);
}     Reading from Event Log is very important if we try to display errors or failed attempt to run application to the web. We can do this using following code.public static void ReadEventLog()
{
     EventLog MyDemoLog = new EventLog();
     MyDemoLog.Log = "Demo";
     foreach (EventLogEntry MyDemoEntry in MyDemoLog.Entries)
     {
      Console.WriteLine(MyDemoEntry.Source + ":" + MyDemoEntry.Message);
     }
}
    Clearing Log is even simpler.public static void ClearEventLog()
   {
     EventLog MyLogDemo = new EventLog("Demo");
     MyLogDemo.Source = "DemoApp";
     MyLogDemo.Clear();
   }

C# Stopwatch

Stopwatch measures time elapsed. It is useful for micro-benchmarks in code optimization. It can perform routine and continuous performance monitoring. The Stopwatch type, found in System.Diagnostics, is useful in many contexts
Example
First, Stopwatch is a class in the .NET Framework that is ideal for timing any operation in your C# programs. You must create Stopwatch as an instance. This makes it useful in multithreaded applications or websites.
Program that uses Stopwatch [C#]

using System;
using System.Diagnostics;
using System.Threading;

class Program
{
    static void Main()
    {
 // Create new stopwatch
 Stopwatch stopwatch = new Stopwatch();

 // Begin timing
 stopwatch.Start();

 // Do something
 for (int i = 0; i < 1000; i++)
 {
     Thread.Sleep(1);
 }

 // Stop timing
 stopwatch.Stop();

 // Write result
 Console.WriteLine("Time elapsed: {0}",
     stopwatch.Elapsed);
    }
}

Output

Time elapsed: 00:00:01.0001477

The code includes the "using System.Diagnostics" namespace at the top. This is where the Stopwatch class is defined in the Framework. You can see the Stopwatch is created with the new operator in Main