Managing a process in C#

While I don’t often program in C#, recently I had been given a task to complete in it.

While starting a process is in itself not particularly difficult, giving it input and reading the output isn’t all that convenient, although after reading some documentation I managed to get things working.


    public class CustomProcess
    {
        private Process process = new Process();
        private Action<string> lamb;

        public CustomProcess(string application, string options)
        {
            ProcessStartInfo psi = new ProcessStartInfo();
            psi.FileName = application;
            psi.Arguments = options;
            psi.UseShellExecute = false;
            psi.RedirectStandardInput = true;  // If all three of these aren't activated, providing input 
            psi.RedirectStandardError = true;  // to the application will not work, and it will throw
            psi.RedirectStandardOutput = true; // an error. Lovely feature.

            process.OutputDataReceived += ProcessOutput;
            process.StartInfo = psi;
        }

        public CustomProcess(string application) : this(application, "")
        {
            // An overloaded constructor which simply passes an
            // empty set of options if they are not specified
        }

        // Enables you to send data to an already executing application
        // for example how you would make use of a terminal window       
        public void InputText(string input)
        {
            process.StandardInput.WriteLine(input);
        }

        // If you want to do anything with the output from the application
        // this is the way to do it. Accepts a lambda with a string argument
        // and no return value
        public void OnOutput(Action<string> lamb)
        {
            this.lamb = lamb;
        }

        private void ProcessOutput(object sender, DataReceivedEventArgs e)
        {
            if(lamb != null)
            {
                lamb(e.Data);
            }
        }

        public void Start()
        {
            process.Start();
            process.BeginOutputReadLine();
        }

        // Closes the process
        public void Stop()
        {
            try
            {
                process.Kill();
                process.Dispose();
                process = null;
            }
            catch (Exception e)
            {
                // It closed too soon, which makes no difference 
                // from a security perspective, naturally.
            }
        }
    }

Per-class toggle-able logging with WildFly (JBoss)

At work we have a large Java EE code base where there are hundreds of web-facing methods in dozens of classes, and many of them are used in quick succession by the highly interactive platform we develop. As part of any system of course you need adequate logging for the sanity of the developers, but logs can become bloated when everything is spewing out every little bit of data it can offer.

Continue reading