Wednesday, 4 December 2013

C#: use EventLog to store error message example

Leave a Comment
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 =)

0 comments:

Post a Comment

Subscribe to our newsletter to get the latest updates to your inbox.

Your email address is safe with us!




Founder of developersnote.com, love programming and help others people. Work as Software Developer. Graduated from UiTM and continue study in Software Engineering at UTMSpace. Follow him on Twitter , or Facebook or .



Powered by Blogger.