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(); // 如果需要等待程序执行完成可以使用这个方法
}
}
}
作者:VipSoft
标签:exe,C#,true,process,using,startInfo,日志,Data From: https://blog.51cto.com/u_15116285/11959418