C# 调用 exe 输出日志 ProcessStartInfo
using System;
using System.Diagnostics;
using NLog;
class Program
{
static void Main()
{
ProcessStartInfo startInfo = new ProcessStartInfo("your_exe_path.exe");
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.CreateNoWindow = true;
using (Process process = Process.Start(startInfo))
{
process.OutputDataReceived += (sender, e) =>
{
if (!string.IsNullOrWhiteSpace(e.Data))
{
logger.Debug($"输出: {e.Data}");
}
};
process.ErrorDataReceived += (sender, e) =>
{
if (!string.IsNullOrWhiteSpace(e.Data))
{
logger.Error($"错误: {e.Data}");
}
};
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit(); // 如果需要等待程序执行完成可以使用这个方法
}
}
}
标签:ProcessStartInfo,exe,C#,process,using,startInfo,日志,Data
From: https://www.cnblogs.com/vipsoft/p/18398785