Displaying Logged Events in C#

This sample applies to

Before starting with the sample be aware that the TcEventViewer ActiveX does provide a fully featured implementation of an event HMI which can easily be added to your application. If you have special needs that cant be realized using the standard TcEventViewer proceed with the sample to display a list of logged events.

1. Create a Win32 C# Application.
2. Add a reference to the Beckhoff TcEventLogger:
Displaying Logged Events in C# 1:
3. On top of the Form1.cs add
usingTCEVENTLOGGERLib;
4. Create a global instance on the TcEventlogger. 
TcEventLogtcEventLogger =newTcEventLog();  
5. Add a ListView control to your Main Form and name it eventView1
6. In Form1_Load initialize the ListView:
eventView1.View =View.Details;
eventView1.Columns.Add("Time","Time");
eventView1.Columns.Add("Type","Type");
eventView1.Columns.Add("Source","Source");
eventView1.Columns.Add("Message","Message");
7. Add code to display events in the ListView control:
///<summary>
///Display an event on top of the ListView control
///</summary>
///<param name="evt">Event to be added</param>
privatevoidAddEvent(TcEventevt)
{
    ListViewItemlvi =newListViewItem();
    lvi.Name ="Time";
    try
    { lvi.Text =Convert.ToString(evt.Date); }
    catch(Exception)
    { lvi.Text =""; }
    eventView1.Items.Insert(0, lvi);
    PrepareListViewItem(lvi);

   try
   { AddListViewSubItem(lvi,"Type", GetEventClassName(evt)); }
   catch(Exception)
    { }
   try
   { AddListViewSubItem(lvi,"Source", evt.get_SourceName(langId)); }
   catch(Exception)
    { }

   try
   { AddListViewSubItem(lvi,"Message", evt.GetMsgString(langId)); }
   catch(Exception)
    { }
}
///<summary>
///Prepare the listview item by padding its subitems with empty items
///</summary>
///<param name="lvi">ListViewItem to be prepared</param>
privatevoidPrepareListViewItem(ListViewItemlvi)
{
    for(inti = 0; i < eventView1.Columns.Count; i++)
    lvi.SubItems.Add("???");// pad items with question marks.
}
///<summary>
///Add one sub item at an indexed position to the ListViewItem
///</summary>
///<param name="lvi">Parent item. The item must have been inserted to a ListView control</param>
///<param name="name">Item name (index) of the item. A coulumn with such index must exist in the ListView control</param>
///<param name="text">Display text</param>
privatevoidAddListViewSubItem(ListViewItemlvi,stringname,stringtext)
{
   ListViewItem.ListViewSubItemlvsi =newListViewItem.ListViewSubItem();
    lvsi.Name = name;
    lvsi.Text = text;
    lvi.SubItems.Insert(lvi.ListView.Columns.IndexOfKey(lvsi.Name), lvsi);
}
///<summary>
///Get a textual representation for the event class.
///</summary>
///<param name="evt">Event object</param>
///<returns>Classname</returns>
privatestringGetEventClassName(TcEventevt)
{
   switch( (TcEventClass)evt.Class)
    {
       caseTcEventClass.TCEVENTCLASS_ALARM:
           return"Alarm";
       caseTcEventClass.TCEVENTCLASS_HINT:
           return"Hint";
       caseTcEventClass.TCEVENTCLASS_INSTRUCTION:
           return"Instruction";
       caseTcEventClass.TCEVENTCLASS_MAINTENANCE:
           return"Maintenance";
       caseTcEventClass.TCEVENTCLASS_MESSAGE:
           return"Message";
       caseTcEventClass.TCEVENTCLASS_PARAMERROR:
           return"Paramerror";
       caseTcEventClass.TCEVENTCLASS_STATEINFO:
           return"State info";
       caseTcEventClass.TCEVENTCLASS_WARNING:
           return"Warning";
       default:
           return"?";
    }
}
8. Finally, in Form1_Load, read the list of active alarms from the TcEventLogger
foreach(TcEvent evt in tcEventLogger.EnumLoggedEventsEx() )
    AddEvent(evt);  
9. Start a PLC program to issue some events.
Displaying Logged Events in C# 2:

 

Language / IDE

Source code

Visual Studio 2005

Sample01.zip