Search this blog

You can search for all the topics in our blog using our google custom search now.

Search the blog

Loading

Wednesday, September 8, 2010

Reading and Writing Events to/from EventLog

In this post we will explore the following topics:

1. How to view Event Logs?
2. How to Register an Event Source?
3. How to Log Events?
4. How to Read Events?

The following is the sample code for the above topics:


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

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
          

            if (!EventLog.Exists("My Log"))
            {
                EventLog.CreateEventSource("Sam Application", "My Log");
            }

            EventLog mylog = new EventLog("My Log");
            mylog.Source = "Sam Application";
            mylog.WriteEntry("Could not connect", EventLogEntryType.Error, 1001, 1);

            Console.WriteLine("Event log written now trying to read from event log");
            Console.ReadKey();
            Console.Clear();
            foreach (EventLogEntry entry in mylog.Entries)
                Console.WriteLine(entry.Message);
            Console.WriteLine("Press any key to exit!!!");
            Console.ReadKey();
          
        }
    }
}

We will see in detail all the three topics in the coming video tutorials which will be posted soon.

Regards
Sameer Shaik

Tags: Microsoft MCTS exam, Learn, Tutorials, Free C#, Free Videos, Microsoft Dot Net Jobs,Microsoft Dot Net Jobs Interview, Free Training and Preparation, Jobs MCTS specialization, visual studio.net, study guide, sample questions, Exam Prep, Exam Practice Test

No comments:

Post a Comment