Материал из .Net Framework эксперт
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Add entry to event log inside a service
using System;
using System.Collections;
using System.ruponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
public class Service1 : System.ServiceProcess.ServiceBase
{
public Service1()
{
this.ServiceName = "MyFirstService";
}
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)
{
EventLog.WriteEntry( "Hello from MyFirstService" );
}
protected override void OnStop()
{
EventLog.WriteEntry( "Goodbye from MyFirstService" );
}
}
Create event source and write an event to the event log
using System;
using System.Diagnostics;
class MainClass
{
public static void Main ()
{
if (!EventLog.SourceExists("MyEventSource"))
{
EventLog.CreateEventSource("MyEventSource", "Application");
}
// Write an event to the event log.
EventLog.WriteEntry(
"MyEventSource", // 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
);
}
}
Display First 5 Entries in your system Event log
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();
}
}
Is a event source in the event log
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();
}
// The main entry point for the process
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 );
}
}
Write entry to event log
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();
}
}