Csharp/C Sharp/Windows/Event Log — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 11:42, 26 мая 2010

Check Event Source Existance and Create Event Source

using System;
using System.Diagnostics;
class MainClass {
    public static void Main() {
        if (!EventLog.SourceExists("C#")) {
            EventLog.CreateEventSource("C#", "Application");
        }
        EventLog.WriteEntry(
            "C#",                          // Registered event source
            "A simple test event.",        // Event entry message
            EventLogEntryType.Information, // Event type
            1,                             // Application-specific ID
            0,                             // Application-specific category
            new byte[] { 10, 55, 200 }       // Event data
        );
    }
}


Get first 5 message from system event log

  using System;
  using System.Resources;
  using System.Drawing;
  using System.Collections;
  using System.Windows.Forms;
  using System.Resources;
  using Microsoft.Win32;
  // For event log.
  using System.Diagnostics;
  class Test
  {
    static void Main(string[] args)
    {
      EventLog log = new EventLog();
      log.Log = "Application";
      log.Source = "www.nfex.ru";
      log.WriteEntry("message from my own exa,ple...");
      
      // Display the first 5 entries in the Application log.
      for(int i = 0; i < 5; i++)
      {
                Console.WriteLine("Message: " + log.Entries[i].Message + "\n" +  
            "Box: " + log.Entries[i].MachineName + "\n" +
            "App: " + log.Entries[i].Source + "\n" +
            "Time entered: " + log.Entries[i].TimeWritten, 
            "Application Log entry:");
      }
      log.Close();
    }
  }


Removes the registration for an event source if it has been registered

/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa
Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
// RemSrc.cs -- Removes the registration for an event source if
//              it has been registered.
//
//              Compile this program with the following command line:
//                  C:>csc RemSrc.cs
using System;
using System.Diagnostics;
namespace nsEventLogs
{
    public class RemSrc
    {
        static public void Main (string [] args)
        {
            if (args.Length == 0)
            {
                Console.WriteLine ("Please enter an event source to remove.");
                Console.WriteLine ("Usage: RemSrc [source]");
                return;
            }
            if (EventLog.SourceExists (args[0]))
            {
                Console.WriteLine ("Deleting event log source " + args[0]);
                EventLog.DeleteEventSource (args[0]);
            }
        }
    }
}


Specify EventLogEntryType

using System;
using System.Diagnostics;
class MainClass {
    public static void Main() {
        if (!EventLog.SourceExists("C#")) {
            EventLog.CreateEventSource("C#", "Application");
        }
        EventLog.WriteEntry(
            "C#",                          // Registered event source
            "A simple test event.",        // Event entry message
            EventLogEntryType.Information, // Event type
            1,                             // Application-specific ID
            0,                             // Application-specific category
            new byte[] { 10, 55, 200 }       // Event data
        );
    }
}


Write Message to system event log

  using System;
  using System.Resources;
  using System.Drawing;
  using System.Collections;
  using System.Windows.Forms;
  using System.Resources;
  using Microsoft.Win32;
  // For event log.
  using System.Diagnostics;
  class Test
  {
    static void Main(string[] args)
    {
      EventLog log = new EventLog();
      log.Log = "Application";
      log.Source = "www.nfex.ru";
      log.WriteEntry("message from my own exa,ple...");
      
      // Display the first 5 entries in the Application log.
      for(int i = 0; i < 5; i++)
      {
                Console.WriteLine("Message: " + log.Entries[i].Message + "\n" +  
            "Box: " + log.Entries[i].MachineName + "\n" +
            "App: " + log.Entries[i].Source + "\n" +
            "Time entered: " + log.Entries[i].TimeWritten, 
            "Application Log entry:");
      }
      log.Close();
    }
  }


Write the Event Log Entry type as Information

 

using System;
using System.Diagnostics;
class MainClass {
    public static void Main() {
        string source = "EventSource";
        string log = "MainClass.LOG";
        EventLog el = new EventLog();
        if (!EventLog.SourceExists(source)) {
            EventLog.CreateEventSource(source, log);
        }
        el.Source = source;
        String message = "Starting Up";
        el.WriteEntry(message, EventLogEntryType.Information);
        message = "Processing";
        el.WriteEntry(message, EventLogEntryType.Information);
        message = "Shutting down";
        el.WriteEntry(message, EventLogEntryType.Information);
        el = new EventLog();
        if (!EventLog.SourceExists(source)) {
            Console.WriteLine("Event Log does not exist!");
            return;
        }
        el.Source = source;
        foreach (EventLogEntry entry in el.Entries) {
            Console.WriteLine("\tEntry: " + entry.Message);
        }
        EventLog.Delete(log);
    }
}