AnsweredAssumed Answered

Programmatically redirecting output of pigetmsg -f

Question asked by John Messinger Champion on Mar 10, 2017
Latest reply on Mar 22, 2017 by gregor

So I have a requirement to programmatically capture PI message log information in near real time on a continuous basis. As there doesn't seem to be any SDK functionality to access the server (or local) message log (other than the PI-SDK, and I don't really want to go down that path), I've been mucking around with trying to redirect the output of pigetmsg, running in follow mode (-f switch). I'm doing this from some C# code, snippet as follows:

 

static void Main(string[] args)
{
  string sourceServer = "demo-pi";
  string authenticationMethod = "Windows";
  var commandArgs = new StringBuilder();
  commandArgs.AppendFormat("-node {0}", sourceServer);
  commandArgs.AppendFormat(" -{0}", authenticationMethod);
  commandArgs.Append(" -f");
  var pigetmsg = new Process();
  var startInfo = new ProcessStartInfo();
  startInfo.WindowStyle = ProcessWindowStyle.Hidden;
  startInfo.CreateNoWindow = true;
  string cmdDirectory = Environment.GetEnvironmentVariable("PIHOME") + "\\adm\\";
  startInfo.FileName = string.Format("\"{0}pigetmsg.exe\"", cmdDirectory);
  startInfo.Arguments = commandArgs.ToString();
  startInfo.RedirectStandardOutput = true;
  startInfo.UseShellExecute = false;
  pigetmsg.StartInfo = startInfo;
  pigetmsg.OutputDataReceived += new DataReceivedEventHandler(MessageLogOutputHandler);
  Console.WriteLine("Commencing process {0} {1}", startInfo.FileName, startInfo.Arguments);
  pigetmsg.Start();
  pigetmsg.BeginOutputReadLine();
  pigetmsg.WaitForExit();
}

private static void MessageLogOutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
{
  if (!string.IsNullOrEmpty(outLine.Data))
  {
  Console.WriteLine(outLine.Data);
  }
}

 

What I've found though is that the output from pigetmsg doesn't actually seem to get captured as redirection of standard output. If I substitute the startInfoArguments as per the following snippet, the code works exactly as expected, in that the output of the pigetmsg command is redirected and I can capture it and output it into my console app window:

 

var commandArgs = new StringBuilder();
commandArgs.AppendFormat("-node {0}", sourceServer);
commandArgs.AppendFormat(" -{0}", authenticationMethod);
commandArgs.Append(" -st *-10m -et *");

 

So my question here is, does pigetmsg -f not write to stdout? After scratching my head for a couple of days (and reading many SO posts), I'm starting to wonder if it's possible to even capture the output of pigetmsg this way when running with -f.

 

Appreciate any thoughts or suggestions from the community (and any OSI devs, if you're watching).

 

Cheers,

John

Outcomes