Home > C#

Export Event Logs Windows 2003 in .NET - C#

7. April 2009

This is a small console application I wrote to export the event log files in C#.  I did this because in Windows 2003 I couldn't find a way to get it to XML by doing a Save As or anything like that.  I did notice in Windows Vista that you can save it as XML...which is nice, but couldn't find a way for Server 2003.  Anyway I did this more or less because I wanted to see stuff happening with ASP.NET 2.0 since it writes to the Application Log with any unhandled exception.  So as I always do I'm just going to paste the entire code...about 140 lines. This program takes the following information:

Server Name
Source of Entry (i.e., ASP.NET 2.0.50727.0)
Log Type ( i.e., Application, Security)

This ends up exporting the event log from Windows 2003 to an XML file.

Oh yea, as always use it at your own risk, if anything it just won't work because it can't open the Event Log or can't write the file...not too much error catching going on in here since I wrote it in about 30 minutes or so.

using System;
using System.Diagnostics;
using System.IO;

namespace AppLogExporter
{
    class Program
    {

        static int evtType = 0;
        static int logType = 0;
        static string serverName = "";
        static string eventSource = "";

        static void Main(string[] args)
        {
            ShowEntryMenu();
            GetLogInfo();
            Console.ReadLine();
        }

        #region "Show Entry Menu"
        private static void ShowEntryMenu()
        {

            Console.Write("Enter Server Name: ");
            serverName = Console.ReadLine();

            Console.WriteLine();

            Console.Write("Enter Source for Log Entry: ");
            eventSource = Console.ReadLine();

            Console.WriteLine();

            Console.WriteLine("Select Log Type");
            Console.WriteLine("1:Application");
            Console.WriteLine("2:Security");
            Console.WriteLine("3:Setup");
            Console.Write("Log Type: ");

            logType = Int32.Parse(Console.ReadLine());

            Console.WriteLine();

            Console.WriteLine("Select the Event Type to Export");
            Console.WriteLine("1:Error");
            Console.WriteLine("2:Information");
            Console.WriteLine("3:Warning");
            Console.WriteLine("4:All");
            Console.Write("Event Type: ");

            evtType = Int32.Parse(Console.ReadLine());

        }

        #endregion

        #region "GetLogInfo"
        private static void GetLogInfo()
        {
            EventLog eventLog = null;
            string eventType = string.Empty;
            StreamWriter swLog = new StreamWriter("D:\\ApplicationLogExport.xml", false);
           
            swLog.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
            swLog.WriteLine("<SystemLog>");

            try
            {
                switch (logType)
                {
                    case 1: eventLog = new EventLog("Application", serverName);
                        break;
                    case 2: eventLog = new EventLog("Security", serverName);
                        break;
                    case 3: eventLog = new EventLog("Setup", serverName);
                        break;
                    default: break;
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception: Unable to open log file for the following reason: {0}", ex.Message);
                return;              
            }
          
            switch (evtType)
            {
                case 1: eventType = "Error";
                    break;
                case 2: eventType = "Information";
                    break;
                case 3: eventType = "Warning";
                    break;
                case 4: eventType = "All";
                    break;
                default: break;

            }

            //READ EACH ENTRY IN THE LOG AND WRITE TO FILE
            foreach (EventLogEntry evtLogEntry in eventLog.Entries)
            {

                if (eventType == "All")
                {
                    //get all event types
                    if (evtLogEntry.Source == eventSource)
                    {
                        Console.WriteLine(string.Format("Entry: {0} - Message: {1}", evtLogEntry.EntryType, evtLogEntry.Message));
                        swLog.WriteLine("<Entry>");
                        swLog.WriteLine("<Type>{0}</Type>", evtLogEntry.EntryType);
                        swLog.WriteLine("<EntryDate>{0}</EntryDate>", evtLogEntry.TimeWritten);
                        swLog.WriteLine("<Message>{0}</Message>", evtLogEntry.Message);
                        swLog.WriteLine("</Entry>");
                    }
                }
                else
                {
                    if (evtLogEntry.Source == eventSource && evtLogEntry.EntryType.ToString() == eventType)
                    {
                        Console.WriteLine(string.Format("Entry: {0} - Message: {1}", evtLogEntry.EntryType, evtLogEntry.Message));

                        swLog.WriteLine("<Entry>");
                        swLog.WriteLine("<Type>{0}</Type>", evtLogEntry.EntryType);
                        swLog.WriteLine("<EntryDate>{0}</EntryDate>", evtLogEntry.TimeWritten);
                        swLog.WriteLine("<Message><![CDATA[{0}]]></Message>", evtLogEntry.Message);
                        swLog.WriteLine("</Entry>");
                    }
                }
            }

            swLog.WriteLine("</SystemLog>");
            swLog.Close();
            swLog.Dispose();
        }
        #endregion
    }
}

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

C#

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading