C# 调用 exe 输出日志
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 转载请注明原文链接:https://www.cnblogs.com/vipsoft/p/18398785