Csharp/C Sharp by API/System.Diagnostics/EventLog
Содержание
EventLog.CreateEventSource
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);
}
}
EventLog.Delete
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);
}
}
EventLog.DeleteEventSource
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]);
}
}
}
}
EventLog.Entries
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
using Microsoft.Win32;
using System.Diagnostics;
public class MainClass{
public static void Main(){
EventLog log = new EventLog();
for(int i = 0; i < 5; i++)
{
try
{
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);
}
catch{}
}
log.Close();
}
}
EventLog.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();
}
}
EventLog.Source
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();
}
}
EventLog.SourceExists
using System;
using System.Diagnostics;
class MainClass
{
public static void Main ()
{
if (!EventLog.SourceExists("MyEventSource"))
{
EventLog.CreateEventSource("MyEventSource", "Application");
}
EventLog.WriteEntry(
"MyEventSource",
"A simple test event.",
EventLogEntryType.Information,
1,
0,
new byte[] {10, 55, 200}
);
}
}
EventLog.WriteEntry
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
using Microsoft.Win32;
using System.Diagnostics;
public class MainClass{
public static void Main(){
EventLog log = new EventLog();
log.Log = "Application";
log.Source = "MainClass";
log.WriteEntry("Hey");
log.Close();
}
}
new EventLog()
using System;
using System.Collections;
using System.ruponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
public class Service1 : System.ServiceProcess.ServiceBase
{
private System.Diagnostics.EventLog eventLog1;
public Service1()
{
this.eventLog1 = new System.Diagnostics.EventLog();
((System.ruponentModel.ISupportInitialize)(this.eventLog1)).BeginInit();
this.eventLog1.Log = "MyCustomLog";
this.eventLog1.Source = "CustomEventService2";
this.AutoLog = false;
this.ServiceName = "CustomEventService";
((System.ruponentModel.ISupportInitialize)(this.eventLog1)).EndInit();
}
static void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Service1() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}
protected override void OnStart(string[] args)
{
if ( !EventLog.SourceExists( eventLog1.Source ) )
{
EventLog.CreateEventSource( eventLog1.Source, eventLog1.Log );
}
eventLog1.WriteEntry( "Hello", EventLogEntryType.Information );
}
protected override void OnStop()
{
eventLog1.WriteEntry( "Goodbye", EventLogEntryType.Warning );
}
}