QuickOPC User's Guide and Reference
Examples - Instrumentation - OPC UA event logging
View with Navigation Tools

.NET

// This example demonstrates the loggable entries originating in the OPC-UA client engine and the EasyUAClient component.

using System;
using OpcLabs.BaseLib.Instrumentation;
using OpcLabs.EasyOpc.UA;
using OpcLabs.EasyOpc.UA.OperationModel;

namespace UADocExamples._EasyUAClient
{
    class LogEntry
    {
        public static void Main1()
        {
            UAEndpointDescriptor endpointDescriptor =
                "opc.tcp://opcua.demo-this.com:51210/UA/SampleServer";
            // or "http://opcua.demo-this.com:51211/UA/SampleServer" (currently not supported)
            // or "https://opcua.demo-this.com:51212/UA/SampleServer/"

            // Hook static events
            EasyUAClient.LogEntry += EasyUAClientOnLogEntry;

            try
            {
                // Do something - invoke an OPC read, to trigger some loggable entries.
                var client = new EasyUAClient();
                try
                {
                    client.ReadValue(endpointDescriptor, "nsu=http://test.org/UA/Data/ ;i=10853");
                }
                catch (UAException uaException)
                {
                    Console.WriteLine("*** Failure: {0}", uaException.GetBaseException().Message);
                    return;
                }

                Console.WriteLine("Processing log entry events for 1 minute...");
                System.Threading.Thread.Sleep(60 * 1000);

                Console.WriteLine("Finished.");
            }
            finally
            {
                // Unhook static events
                EasyUAClient.LogEntry -= EasyUAClientOnLogEntry;
            }
        }

        // Event handler for the LogEntry event. It simply prints out the event.
        private static void EasyUAClientOnLogEntry(object sender, LogEntryEventArgs logEntryEventArgs)
        {
            Console.WriteLine(logEntryEventArgs);
        }
    }
}

COM

// This example demonstrates the loggable entries originating in the OPC-UA client engine and the EasyUAClient component.

#include "stdafx.h"    // Includes "QuickOpc.h", and other commonly used files
#include <atlcom.h>
#include "LogEntry.h"

namespace _EasyUAClientManagement
{
    // CEasyUAClientManagementEvents

    class CEasyUAClientManagementEvents : public IDispEventImpl<1, CEasyUAClientManagementEvents>
    {
    public:
    BEGIN_SINK_MAP(CEasyUAClientManagementEvents)
        // Event handlers must have the __stdcall calling convention
        SINK_ENTRY(1, 1 /*DISPID_EASYUACLIENTManagementEVENTS_LOGENTRY*/, &CEasyUAClientManagementEvents::LogEntry)
    END_SINK_MAP()

    public:
        // Event handler for the LogEntry event. It simply prints out the event.
        STDMETHOD(LogEntry)(VARIANT varSender, _LogEntryEventArgs* pEventArgs)
        {
            _tprintf(_T("%s\n"), (LPCTSTR)CW2CT(pEventArgs->ToString));
            return S_OK;
        }
    };



    void LogEntry::Main()
    {
        // Initialize the COM library
        CoInitializeEx(NULL, COINIT_MULTITHREADED);
        {
            // The management object allows access to static behavior - here, the shared LogEntry event.
            _EasyUAClientManagementPtr ClientManagementPtr(__uuidof(EasyUAClientManagement));

            // Hook events
            CEasyUAClientManagementEvents* pClientManagementEvents = new CEasyUAClientManagementEvents();
            AtlGetObjectSourceInterface(ClientManagementPtr, &pClientManagementEvents->m_libid, 
                &pClientManagementEvents->m_iid, 
                &pClientManagementEvents->m_wMajorVerNum, &pClientManagementEvents->m_wMinorVerNum);
            pClientManagementEvents->m_iid = _uuidof(DEasyUAClientManagementEvents);
            pClientManagementEvents->DispEventAdvise(ClientManagementPtr, &pClientManagementEvents->m_iid);

            // Do something - invoke an OPC read, to trigger some loggable entries.
            _EasyUAClientPtr ClientPtr(__uuidof(EasyUAClient));
            ClientPtr->ReadValue(
                //L"http://opcua.demo-this.com:51211/UA/SampleServer", 
                L"opc.tcp://opcua.demo-this.com:51210/UA/SampleServer",
                L"nsu=http://test.org/UA/Data/ ;i=10853");

            _tprintf(_T("Processing log entry events for 1 minute...\n"));
            Sleep(60*1000);

            // Unhook events
            pClientManagementEvents->DispEventUnadvise(ClientManagementPtr, &pClientManagementEvents->m_iid);
        }
         // Release all interface pointers BEFORE calling CoUninitialize()
        CoUninitialize();
    }
}

 

See Also

Conceptual