Search code examples
c#perforcep4api

C# P4Api use -zversion and -zprog for custom logging


I am building an app with C# using the P4Api, but I need to use the parameters -zprog and -zversion so the server logs show from what app the commands are coming as this post explains: https://community.perforce.com/s/article/11551

string logParams = "-zprog=MyApp -zversion=1.1";
P4Command cmd = new P4Command(rep, "sync", false, path);
  • I've tried to pass the logParams as an argument to the P4Command, before and after the path, but it recognize them as another path to a file, returning a "no such file(s)" message error.
  • I've tried to add it before the "sync", but it recognize it as a command, so it returns a "Unknown command" message error.

As the link explains, using the cmd, this command should be "p4 -zprog=MyApp -zversion=1.1 sync [path]", so this parameters should be for the "p4" and not for the "sync"

Is it possible to add this parameters to the command? If not, any suggestion on how to do this?

Thanks.


Solution

  • I end up getting rid of the P4Api, as suggested on the comment, and basically I am using a System.Diagnostics.Process to call p4.exe and pass some arguments to do whatever I need to do. This is my solution for this:

    public bool RunCommand<T>(string command, Func<string, string, T> output,out T outputResult, params string[] args)
    {
        string logParams = "-zprog=MyApp -zversion=1.1";
        Process proc = new Process();
        proc.StartInfo.WorkingDirectory = "";
        proc.StartInfo.FileName = "p4.exe";
        proc.StartInfo.Arguments = logParams + " " + command + " ";
        foreach (string s in args)
        {
            proc.StartInfo.Arguments += s + " ";
        }
    
        proc.StartInfo.UseShellExecute = false;
        proc.StartInfo.CreateNoWindow = true;
        proc.StartInfo.RedirectStandardOutput = true;
        proc.StartInfo.RedirectStandardError = true;
        proc.Start();
        StreamReader strOutput = proc.StandardOutput;
        StreamReader strError = proc.StandardError;
        outputResult = output(strOutput.ReadToEnd(), strError.ReadToEnd());
        proc.WaitForExit();
        return true;
    }
    

    Func<string, string, T> output allows me to parse the process output and errors, to get the data needed from a P4 query and return it with out T outputResult, like the command "where" that returns the Depot path from a workspace path.

    I hope this helps to other people with similar issues.