Add migrated to .NET 8.0 variant of Hcs.Client
This commit is contained in:
43
Hcs.ClientNet/Client/MessageCapturer/FileMessageCapturer.cs
Normal file
43
Hcs.ClientNet/Client/MessageCapturer/FileMessageCapturer.cs
Normal file
@ -0,0 +1,43 @@
|
||||
using Hcs.ClientNet.Logger;
|
||||
using System.Text;
|
||||
|
||||
namespace Hcs.ClientNet.MessageCapturer
|
||||
{
|
||||
/// <summary>
|
||||
/// Реализация механизма захвата содержимого сообщений SOAP, записывающая
|
||||
/// каждое сообщение в отдельный файл на диске
|
||||
/// </summary>
|
||||
public class FileMessageCapturer(string directory, ILogger logger) : IMessageCapturer
|
||||
{
|
||||
private readonly string directory = directory;
|
||||
private readonly ILogger logger = logger;
|
||||
|
||||
public void CaptureMessage(bool sent, string messageBody)
|
||||
{
|
||||
var index = 0;
|
||||
var maxIndex = 1000000;
|
||||
string fileName;
|
||||
do
|
||||
{
|
||||
index += 1;
|
||||
|
||||
if (index > maxIndex)
|
||||
{
|
||||
throw new System.Exception("index value exceeds maxIndex value");
|
||||
}
|
||||
|
||||
fileName = index.ToString("D3") + "_" + (sent ? "message" : "response") + ".xml";
|
||||
|
||||
if (!string.IsNullOrEmpty(directory))
|
||||
{
|
||||
fileName = System.IO.Path.Combine(directory, fileName);
|
||||
}
|
||||
}
|
||||
while (System.IO.File.Exists(fileName));
|
||||
|
||||
logger?.WriteLine($"Capturing message to file {fileName}...");
|
||||
|
||||
System.IO.File.WriteAllText(fileName, messageBody, Encoding.UTF8);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user