C # Operating System logs

  Using the C # programming, check the system log, log describes two categories: EventLog and EventLogEntry class, as well as interact with the system log.

.NET Framework Class Library EventLog class provided with the system log class and EventLogEntry interact both belong System.Diagnostics Namespace

First, we declare a variable: private EventLogEntryCollection eventCollection represents a collection of system logs. 

    EventLog class attributes are:

  Entris return a EventLogEntryCollection type value that represents the contents of the event log

  Log acquisition or return the name of the log, which is the application log Application, a system log System, a security log Security, the default is an empty string.

  LogDisplayName get the friendly name of the event log 

    MachineName Gets or sets the read or write to the event on which the name of the computer

  Source Gets or sets when writing to the event log to register and use the name of the source

Examples EventEntryCollection class defines the size and the enumerator EventLogEntry set. Some key properties EventLogEntry class as follows:

  Category acquisition of CategoryNumber with the corresponding text;

    CategoryNumber get the class number;  

  Get Data corresponding to the binary data;

EntryType get this type of event, its value belongs to EventLogEntryType enumeration, enumeration of the main members are as follows:

  Error Error event, which indicates that the user should be aware of the serious problems, such as loss of data or functionality;

  FailureAudit Failure Audit event which indicates when the audit failed access attempts, such as attempts to open the file.;

  . Information which indicates that important information about the event, the success of the event;

  SuccessAudit Success Audit events indicate when it audited access attempt is successful, such as security incidents when they log in.;

  Warning Warning events indicate it is not immediately an issue of importance, but this might indicate that the condition can cause problems in the future.;

  EventID Gets the application of this event item identifier of a specific event;

  Index Gets the index in the event log;

  MachineName Gets the name of the computer that is produced;

  Message Gets the localized message with the event;

  Get the replacement string corresponding to ReplacementStrings;

  Source Gets the name of the application that generated the event;

  TimeGenerated get local time generated the event;

  TimeWritten get local time written to the event log;

  UserName Gets the name of the user responsible for the event.

 Code examples are as follows:

using System;

using System.Diagnostics;

namespace LogView

{

public class SysLogView

{

private EventLogEntryCollection eventCollection;

private EventLog systemEvent;

public SysLogView()

{

systemEvent=new  EventLog ();

systemEvent.Log="System";

eventCollection=systemEvent.Entries;

}

private void LoadEventLog(int c)

{

EventLog systemEvent=new EventLog();

systemEvent.Log="System";

eventCollection=systemEvent.Entries;

int length=eventCollection.Count;

EventLogEntry entry=eventCollection[c];

string [] title={

entry.EntryType.ToString(),

entry.TimeGenerated.ToLongDateString(),

entry.TimeGenerated.ToLongTimeString(),

entry.Source,

entry.Category,

entry.EventID.ToString(),

entry.UserName,

entry.MachineName

};

for(int j=0;j<title.Count;j++)

{

Console.WriteLine(title[j]);

}

Console.WriteLine("\n"+entry.Message );

}

private string DisplayEventCount()

{

return (length.ToString());

}

public static void Main(string [] args)

{

SysLogView slv=new SysLogView();
if(args.length==1)
{
int x=Convert.ToInt32(args[0]);
slv.LoadEventLog(x);
}
else
{
Console.WriteLine(" Event count:"+slv.DisplayEventCount());
}
}
}
}

C # Create a system log

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

namespace Log
{
class LogWirter
{
/// <summary>
/// 事件源名称
/// </summary>
private string eventSourceName;
EventLogEntryType eventLogType;
public LogWirter()
{
eventSourceName = "test";
eventLogType = EventLogEntryType.Error;
}

/// <Summary>
/// Event message source name
/// </ Summary>
public String EventSourceName
{
SET = {eventSourceName value;}
}

/// <Summary>
/// Event Message Type
/// </ Summary>
public EventLogType an EventLogEntryType
{
SET = {eventLogType value;}
}

/// <summary>
/// 写入系统日志
/// </summary>
/// <param name="message">事件内容</param>
public void LogEvent(string message)
{
if (!EventLog.SourceExists(eventSourceName))
{
EventLog.CreateEventSource(eventSourceName, "Application");
}
EventLog.WriteEntry(eventSourceName, message, EventLogEntryType.Error);
}
}

 

 

Guess you like

Origin www.cnblogs.com/b2cup/p/11936620.html