blog.easyciel.net author="Patrick Rabian" about="c#, sharepoint, biztalk, team system resources" more="news, samples, tips for .NET world's developers !"

How to read an event log

Tuesday, 2 January 2007 17:57 by prabian

Sample console application that read the content of an event log…

——–

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

namespace EventLogReader{
  
public class 
Program {
    
static void Main (string
[] args){
      EventLog logger=
new EventLog("Application"
);
      
int maxLineLength=30
;
      
string logFilter="*";     
//*, I, W, E, ?            
      
string 
message;
      
string 
type;
      
int counter=0
;
      DateTime start, end;
      TimeSpan executionTime;
      start=DateTime.Now;
      
foreach (EventLogEntry entry in 
logger.Entries){
        message=entry.Message.Replace(
"\n"" "
);
        
if (message.Length>maxLineLength) message=message.Substring(0, maxLineLength)+"…"
;
        
switch 
(entry.EntryType){
          
case EventLogEntryType.Information: type="I"
;
          
break
;
          
case EventLogEntryType.Warning: type="W"
;
          
break
;
          
case EventLogEntryType.Error: type="E"
;
          
break
;
          
default: type="?"
;
          
break
;
        }
        
if (logFilter=="*" 
|| logFilter==type) {
          Console.WriteLine(
"[{4}>{0}-{1}({2})] {3}", type, entry.Source, entry.InstanceId, message, entry.TimeWritten.ToString("dd/MM/yyyy HH:mm:ss"
));
          Console.Out.Flush();
          counter++;
        }
      }
      end=DateTime.Now;
      executionTime=end-start;
      Console.WriteLine(
"{0} row(s) displayed in {1} ms. Press a key to quit…"
, counter, executionTime.TotalMilliseconds);
      Console.Read();
    }

  }

}

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:   ,
Categories:   C# sample code
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

Related posts

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

November 20. 2008 23:03