Skip to content

Instantly share code, notes, and snippets.

@sliekens
Last active September 6, 2015 18:40
Show Gist options
  • Save sliekens/ac56c1613d10aa0480ac to your computer and use it in GitHub Desktop.
Save sliekens/ac56c1613d10aa0480ac to your computer and use it in GitHub Desktop.
Add timestamps to console output
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
namespace Sys
{
class Program
{
static void Main(string[] args)
{
if (args.Length == 0)
{
var location = Assembly.GetExecutingAssembly().Location;
var fileName = Path.GetFileName(location);
Console.WriteLine($"Usage: {fileName} <executable> [<arguments>]");
Environment.Exit(1);
}
var si = new ProcessStartInfo(args[0])
{
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
Arguments = string.Join(" ", args.Skip(1))
};
var proc = new Process
{
EnableRaisingEvents = true,
StartInfo = si,
};
proc.Start();
proc.OutputDataReceived += Proc_DataReceived;
proc.ErrorDataReceived += Proc_DataReceived;
proc.BeginOutputReadLine();
proc.BeginErrorReadLine();
proc.WaitForExit();
}
private static void Proc_DataReceived(object sender, DataReceivedEventArgs e)
{
if (string.IsNullOrWhiteSpace(e.Data))
{
Console.WriteLine();
}
else
{
Console.WriteLine($"{DateTime.Now:yyyy/MM/dd HH:mm:ss.fff} {e.Data}");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment