EventLog in C#.net

How to use EventLog in C#.net

ClsEventLogged.cs



public
class ClsEventLogged

{

private string _EventLogger = string.Empty;

public string EventLogger

{

get { return _EventLogger; }

set { _EventLogger = value; }

}

private string _Source = string.Empty;

public string Source

{

get { return _Source; }

set { _Source = value; }

}

private int _EventCode;

public int EventCode

{

get { return _EventCode; }

set { _EventCode = value; }

}

private string _User = string.Empty;

public string User

{

get { return _User; }

set { _User = value; }

}

public bool mLoggedEvent(EnumEventLogging EventType, string Message)

{

try

{

if (string.IsNullOrEmpty(EventLogger))

{

EventLogger = "KSLibEventMgmt";

}

if (string.IsNullOrEmpty(Source))

{

Source = "DefaultEventLogging";

}

EventLog log = new EventLog(EventLogger, ".", Source);

 

switch (EventType)

{

case EnumEventLogging.Normal:

log.WriteEntry(Message);

break;

case EnumEventLogging.Information:

log.WriteEntry(Message, EventLogEntryType.Information);

break;

case EnumEventLogging.Error:

log.WriteEntry(Message, EventLogEntryType.Error);

break;

case EnumEventLogging.FailureAudit:

log.WriteEntry(Message, EventLogEntryType.FailureAudit);

break;

case EnumEventLogging.SuccessAudit:

log.WriteEntry(Message, EventLogEntryType.SuccessAudit);

break;

case EnumEventLogging.Warning:

log.WriteEntry(Message, EventLogEntryType.Warning);

break;

default:

log.WriteEntry(Message);

break;

}

log.Dispose();

return true;

}

catch (Exception ex)

{

throw new ClsEventException("System Unable to log Event");

return false;

}

}

public bool mLoggedEvent(EnumEventLogging EventType, string Message, int EventCode)

{

try

{

if (string.IsNullOrEmpty(EventLogger))

{

EventLogger = "KSLibEventMgmt";

}

if (string.IsNullOrEmpty(Source))

{

Source = "DefaultEventLogging";

}

EventLog log = new EventLog(EventLogger, ".", Source);

switch (EventType)

{

case EnumEventLogging.Normal:

log.WriteEntry(Message);

break;

case EnumEventLogging.Information:

log.WriteEntry(Message, EventLogEntryType.Information, EventCode);

break;

case EnumEventLogging.Error:

log.WriteEntry(Message, EventLogEntryType.Error, EventCode);

break;

case EnumEventLogging.FailureAudit:

log.WriteEntry(Message, EventLogEntryType.FailureAudit, EventCode);

break;

case EnumEventLogging.SuccessAudit:

log.WriteEntry(Message, EventLogEntryType.SuccessAudit, EventCode);

break;

case EnumEventLogging.Warning:

log.WriteEntry(Message, EventLogEntryType.Warning, EventCode);

break;

default:

log.WriteEntry(Message);

break;

 

}

log.Dispose();

return true;

}

catch(Exception ex)

{

throw new ClsEventException("System Unable to log Event");

return false;

}

}

}

public enum EnumEventLogging

{

Normal,

Error,

FailureAudit,

Information,

SuccessAudit,

Warning

}

ClsEventException.cs

public class ClsEventException : ApplicationException

{

// Implement the standard constructors

public ClsEventException() : base() { }

public ClsEventException(string str) : base(str) { }

 

// Override ToString for ClsCustomException.

public override string ToString()

{

return Message;

}

}

By Kalit Sikka   Popularity  (2757 Views)