Thursday, 20 September 2018

Error Capture in EventLog

CMD Command: regedit
CMD event view short command: eventvwr
The errors occurred in the system are captured in the event log of the server. We can access the event log of the server by typing event viewer in run command of the local system.
Permissions required: need read/full control permission for network service as shown below at the registry on HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\EventLog.in order read and write




Call Function:  ErrorLog.trackEventLog("PersonalLoanApplication", ex.ToString(), "error", 0, 0);
C sharp Code:
public class ErrorLog
    {
 public static void trackEventLog(string appName, string errrorMessage, string logType, int eventID = 0, short shortCat = 0)
        {
       
            try
            {
if (!string.IsNullOrEmpty(appName) && !string.IsNullOrEmpty(errrorMessage) && !string.IsNullOrEmpty(logType))
                {
                    if (!EventLog.SourceExists(appName))
                    {
                        EventSourceCreationData eventSourceData = new EventSourceCreationData(appName, appName);
                        EventLog.CreateEventSource(eventSourceData);
                    }

                    using (EventLog myLogger = new EventLog(appName, ".", appName))
                    {
                        if (logType.Trim().ToLower() == "error")
                        {
                            myLogger.WriteEntry(errrorMessage, EventLogEntryType.Error, eventID, shortCat);
                        }
                        else if (logType.Trim().ToLower() == "warning")
                        {
                            myLogger.WriteEntry(errrorMessage, EventLogEntryType.Warning, eventID, shortCat);
                        }
                        else if (logType.Trim().ToLower() == "information")
                        {
                            myLogger.WriteEntry(errrorMessage, EventLogEntryType.Information, eventID, shortCat);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failsafe EventLog");
                //throw ex;
            }
        }

 public static string GetEventLog(string SourceName)
        {
            try
            {
                if (!string.IsNullOrEmpty(SourceName))
                {
                    EventLog myLog = new EventLog();
                    myLog.Log = SourceName;
                    foreach (EventLogEntry entry in myLog.Entries)
                    {
                        string s = entry.Message;
                        s = s + entry.TimeGenerated;
                        s = s + entry.EntryType;
                        s = s + entry.Source;
                        s = s + entry.Index;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return "";
        }
}

No comments:

Post a Comment