Skip to content

Commit

Permalink
增加aria2启动日志输出
Browse files Browse the repository at this point in the history
  • Loading branch information
aiqinxuancai committed Aug 2, 2024
1 parent 9b6bb21 commit 2932aec
Showing 1 changed file with 53 additions and 3 deletions.
56 changes: 53 additions & 3 deletions Aria2Fast/Service/Aria2ApiManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
using Aria2Fast.Utils;
using Newtonsoft.Json.Linq;
using System.Threading.Channels;
using System.Text;
using System.Collections.Concurrent;

namespace Aria2Fast.Service
{
Expand Down Expand Up @@ -53,6 +55,8 @@ public class Aria2ApiManager
private Timer _debounceTimer;
private readonly object _locker = new object();

private static ConcurrentQueue<string> _outputQueue = new ConcurrentQueue<string>();


private Process? _aria2Process = null;

Expand Down Expand Up @@ -101,7 +105,7 @@ public void Init()
/// <summary>
/// 启动本地Aria2并管理,退出时跟随退出
/// </summary>
private void StartupLocalAria2()
private async void StartupLocalAria2()
{
//key=ARIA2FAST
//port=6809
Expand Down Expand Up @@ -170,9 +174,35 @@ private void StartupLocalAria2()
WorkingDirectory = aria2Path,
CreateNoWindow = true,
UseShellExecute = false,

RedirectStandardOutput = true,
RedirectStandardError = true,
StandardOutputEncoding = Encoding.UTF8, // 设置标准输出编码
StandardErrorEncoding = Encoding.UTF8 // 设置标准错误编码
};

_aria2Process = new Process
{
StartInfo = startInfo,
EnableRaisingEvents = true
};
_aria2Process = Process.Start(startInfo);

_aria2Process.OutputDataReceived += (sender, e) => { if (e.Data != null) AddToQueue(e.Data); };
_aria2Process.ErrorDataReceived += (sender, e) => { if (e.Data != null) AddToQueue(e.Data); };

_aria2Process.Start();
_aria2Process.BeginOutputReadLine();
_aria2Process.BeginErrorReadLine();

// 等待 5 秒(异步)



Task.Run(async () =>
{
await Task.Delay(5000);
RecordOutput();
});

EasyLogManager.Logger.Info($"Aria2已启动");
}
else
Expand All @@ -188,6 +218,26 @@ private void StartupLocalAria2()
}
}

private static void AddToQueue(string line)
{
_outputQueue.Enqueue(line);
while (_outputQueue.Count > 1000)
{
_outputQueue.TryDequeue(out _);
}
}

private static void RecordOutput()
{
StringBuilder sb = new StringBuilder();
foreach (var line in _outputQueue)
{
sb.AppendLine(line);
}
EasyLogManager.Logger.Info(@$"Aria2启动日志:{sb.ToString()}");
}


public static bool ExistLocalAria2()
{
var aria2Path = Path.Combine(Directory.GetCurrentDirectory(), "Aria2");
Expand Down

0 comments on commit 2932aec

Please sign in to comment.