QuickOPC User's Guide and Reference
Installed Examples - Console - XmlEventLogger
View with Navigation Tools

The example below logs OPC Alarms and Events notifications into an XML file.

The main program:

// Logs OPC Alarms and Events notifications into an XML file.

using System;
using System.Diagnostics;
using System.Xml;
using System.Xml.Serialization;
using OpcLabs.BaseLib.Runtime.InteropServices;
using OpcLabs.EasyOpc.AlarmsAndEvents;
using OpcLabs.EasyOpc.AlarmsAndEvents.OperationModel;

namespace XmlEventLogger
{
    class Program
    {
        static void Main()
        {
            ComManagement.Instance.AssureSecurityInitialization();

            Console.WriteLine("Starting up...");
            var xmlSerializer = new XmlSerializer(typeof(EasyAENotificationEventArgs));
            var xmlWriter = XmlWriter.Create("OpcEvents.xml", new XmlWriterSettings
            {
                Indent = true,
                CloseOutput = true
            });
            // The root element can have any name you need, but the name below also allows reading the log back as .NET array
            xmlWriter.WriteStartElement("ArrayOfEasyAENotificationEventArgs");

            Console.WriteLine("Logging events for 30 seconds...");
            int handle = EasyAEClient.SharedInstance.SubscribeEvents("", "OPCLabs.KitEventServer.2", 100,
               (_, eventArgs) =>
                   {
                       Debug.Assert(!(eventArgs is null));
                       Console.Write(".");
                       xmlSerializer.Serialize(xmlWriter, eventArgs);
                   });
            System.Threading.Thread.Sleep(30 * 1000);

            Console.WriteLine();
            Console.WriteLine("Shutting down...");
            EasyAEClient.SharedInstance.UnsubscribeEvents(handle);
            xmlWriter.WriteEndElement();    // not really necessary - XmlWriter would write the end tag for us anyway
            xmlWriter.Close();

            Console.WriteLine("Finished.");
        }
    }
}

 

See Also

Conceptual

Examples - OPC Alarms&Events