QuickOPC.NET: How to log OPC Alarms and Events notifications into an XML file

From OPC Labs Knowledge Base
Revision as of 14:23, 31 July 2016 by User (talk | contribs) (Created page with "The example below subscribes to all notifications from an OPC A&E Server, and logs the event notifications to an XML file. There are three factors that make the code very sho...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

The example below subscribes to all notifications from an OPC A&E Server, and logs the event notifications to an XML file.

There are three factors that make the code very short:

  • You can use anonymous methods (or lambdas) to specify the handler (callback) for event notifications.
  • The event arguments that are passed to the event handler or callback are directly serializable to XML (same as with most QuickOPC objects).
  • There is no need for reconnection logic (more about this further down in this post).


The output file generated by the logger (OpcEvents.xml) is listed at the bottom.

What makes this example quite nice is the fact it is is fairly complete - it is fully resistant against network problems, OPC server crashes etc. You do not need to write any additional code for that! To prove it, increase the logging time in the code to e.g. 10 minutes, start the logger, and then find and select the OPC Simulation Server process (opcrtkit.exe) in the Task Manager, and "kill it" by pressing the "End process" button. The logger will log an en exception record into the output XML. If you, however, wait for some minutes, you will see that the server process restarts itself (this is caused by the reconnection facility in the QuickOPC component), and events from the OPC server will be outputted to the XML file again. The reconnection delays can be influenced by setting various properties on EasyAEClient object. This example project is included with the product. Please use the example from the product itself for the most up-to-date code of the example. There is also an example that shows logging of OPC Data Access Item changes into XML – in a separate application note.

OpcEvents.xml: