private string GetServiceFabricSessionOutputPath()
        {
            StringBuilder serviceFabricSessionName       = new StringBuilder(LttProducerWorker.ServiceFabricSessionName);
            StringBuilder serviceFabricSessionOutputPath = new StringBuilder(LttProducerWorker.PathMaxSize);

            // If processing traces from container LTTng session the session is not accessible from host.
            // In this case the the active session is the one and only one folder inside the Log/Containers/sf-containerid/lttng-traces folder.
            string containerTraceFolder;

            if (this.TryGetContainerTraceFolder(out containerTraceFolder))
            {
                return(containerTraceFolder);
            }

            LttngReaderStatusCode res = LttngReaderBindings.get_active_lttng_session_path(serviceFabricSessionName, serviceFabricSessionOutputPath, LttProducerWorker.PathMaxSize);

            if (res == LttngReaderStatusCode.SUCCESS)
            {
                return(serviceFabricSessionOutputPath.ToString().TrimEnd(Path.DirectorySeparatorChar));
            }
            else
            {
                string errorMessage = LttngReaderStatusMessage.GetMessage(res);
                this.traceSource.WriteWarning(
                    this.logSourceId,
                    "Unable to find active Service Fabric Ltt trace session with name starting with: {0}.\nInner error : {1}",
                    LttProducerWorker.ServiceFabricSessionName,
                    errorMessage);
            }

            return(null);
        }
        private string GetServiceFabricSessionOutputPath()
        {
            StringBuilder serviceFabricSessionName       = new StringBuilder(LttProducerWorker.ServiceFabricSessionName);
            StringBuilder serviceFabricSessionOutputPath = new StringBuilder(LttProducerWorker.PathMaxSize);

            if (LttngReaderBindings.get_active_lttng_session_path(serviceFabricSessionName, serviceFabricSessionOutputPath, LttProducerWorker.PathMaxSize))
            {
                return(serviceFabricSessionOutputPath.ToString().TrimEnd(Path.DirectorySeparatorChar));
            }
            else
            {
                this.traceSource.WriteWarning(
                    this.logSourceId,
                    "Unable to find active Service Fabric Ltt trace session with name starting with: {0}",
                    LttProducerWorker.ServiceFabricSessionName);
            }

            return(null);
        }
        internal ulong ProcessTraces(string lttTracesDirectory, bool seekTimestamp, ulong timestampUnixEpochNanoSec)
        {
            IntPtr ctx;
            IntPtr ctf_iter;

            this.traceSource.WriteInfo(
                this.logSourceId,
                "Starting to process Ltt traces at: {0} - starting at timestamp: {1}",
                lttTracesDirectory,
                (new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds((double)(timestampUnixEpochNanoSec / 1000000)).ToString("o")));
            StringBuilder lttTracesDir = new StringBuilder();

            lttTracesDir.Append(lttTracesDirectory);

            // initializing for reading traces
            int t_handle_id = LttngReaderBindings.initialize_trace_processing(lttTracesDir, out ctx, out ctf_iter, seekTimestamp, timestampUnixEpochNanoSec);

            if (t_handle_id < 0)
            {
                this.traceSource.WriteError(
                    this.logSourceId,
                    "Error ({0}) occurred while trying to process Ltt trace folder file {1}",
                    t_handle_id,
                    lttTracesDir.ToString());
                return(timestampUnixEpochNanoSec);
            }

            // reading traces
            int           err;
            EventRecord   eventRecord            = new EventRecord();
            StringBuilder unstructuredTextString = new StringBuilder(64000);
            ulong         lastReadTimestamp      = 0;
            bool          isUnstructured         = false;
            ulong         numEventsRead          = 0;

            // needed for table event filtering for unstructured events in linux
            StringBuilder taskNameEventName = new StringBuilder(200);

            string baseFileName                = DateTime.UtcNow.ToString("yyyy-MM-dd_HH:mm:ss");
            string csvOutputFilePath           = Path.Combine(this.dtrTracesDirectory, baseFileName + ".trace");
            string tableOutputFilePath         = Path.Combine(this.dtrTracesDirectory, baseFileName + ".table1");
            string csvOutputFileFinishedPath   = Path.Combine(this.dtrTracesDirectory, baseFileName + ".dtr");
            string tableOutputFileFinishedPath = Path.Combine(this.dtrTracesDirectory, baseFileName + ".table");

            csvOutputFilePath = Path.Combine(this.dtrTracesDirectory, csvOutputFilePath);

            // reading event by event using libbabeltrace API and writing it to CSV file
            using (StreamWriter fileStreamDtr = new StreamWriter(csvOutputFilePath))
                using (StreamWriter fileStreamTable = (this.createTableFiles ? new StreamWriter(tableOutputFilePath) : null))
                {
                    while ((err = LttngReaderBindings.read_next_event(ref eventRecord, unstructuredTextString, taskNameEventName, ctf_iter, ref lastReadTimestamp, ref isUnstructured)) == 0)
                    {
                        numEventsRead++;
                        timestampUnixEpochNanoSec = lastReadTimestamp;

                        this.DeliverEventToSinks(ref eventRecord, unstructuredTextString, isUnstructured);
                        this.WriteEventToCsvAndTableFiles(eventRecord, unstructuredTextString.ToString(), taskNameEventName.ToString(), isUnstructured, fileStreamDtr, fileStreamTable);
                    }
                }

            // cleaning up
            LttngReaderBindings.finalize_trace_processing(ctx, ctf_iter, t_handle_id);

            // check for failure in iterator
            if (err != -2)
            {
                this.traceSource.WriteError(
                    this.logSourceId,
                    "Error ({0}) when trying to get next ctf_iterator", err);
                return(timestampUnixEpochNanoSec);
            }

            // renaming files to their final names after finishing processing.
            var filePairs = new [] { new { Temp = csvOutputFilePath, Finished = csvOutputFileFinishedPath }, new { Temp = tableOutputFilePath, Finished = tableOutputFileFinishedPath } };

            foreach (var fp in filePairs)
            {
                try
                {
                    if (File.Exists(fp.Temp))
                    {
                        File.Move(fp.Temp, fp.Finished);
                    }
                }
                catch (Exception e)
                {
                    this.traceSource.WriteError(
                        this.logSourceId,
                        "Exception encountered while moving trace and table files ({0} and {1}). {2}",
                        fp.Temp,
                        fp.Finished,
                        e);
                }
            }

            this.traceSource.WriteInfo(
                this.logSourceId,
                "Finished to process Ltt traces at: {0} - finished at timestamp: {1} - {2} events read",
                lttTracesDirectory,
                (new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds((double)(timestampUnixEpochNanoSec / 1000000)).ToString("o")),
                numEventsRead);


            return(timestampUnixEpochNanoSec);
        }