C#: use EventLog to store error message example

EventLog lets you access or customize Windows event logs, which record information about important software or hardware events. Using EventLog, you can read from existing logs, write entries to logs, create or delete event sources, delete logs, and respond to log entries. You can also create new logs when creating an event source.

The example will show how to use event log in your application

The Code

 private void button1_Click(object sender, EventArgs e)
 {
    try
    {
        string[] a = new string[3];
        a[4] = "test";
    }
    catch (Exception ex)
   {
        LogError(ex);
    }
 }
public void LogError(Exception ex)
 {
    string EventLogSourceName = "Example Log Error";
    int EventLogErrorCode = 99;
    string msg = "The application encountered an unknown error:";
    msg += "\r\nExecuting Method: " + new System.Diagnostics.StackFrame(1, false).GetMethod().Name;
    msg += "\r\nError Message: " + ex.Message;
    EventLog.WriteEntry(EventLogSourceName, msg, EventLogEntryType.Error, EventLogErrorCode);
    MessageBox.Show(msg, "ERROR", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
 }



*The code will get run time error because you need to run application as administrator.


The Output - Event Viewer




By
NOTE : – If You have Found this post Helpful, I will appreciate if you can Share it on Facebook, Twitter and Other Social Media Sites. Thanks =)

Popular posts from this blog

Example to disable save as certain file type in SSRS Report Viewer

Phone book application example - Java

Modal Popup Message Box ASP.NEt C# Example