49 lines
1.5 KiB
C#
49 lines
1.5 KiB
C#
using Hcs.Broker.Logger;
|
|
using System.Text;
|
|
|
|
namespace Hcs.Broker.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 Exception("index value exceeds maxIndex value");
|
|
}
|
|
|
|
fileName = index.ToString("D3") + "_" + (sent ? "message" : "response") + ".xml";
|
|
|
|
if (!string.IsNullOrEmpty(directory))
|
|
{
|
|
if (!Directory.Exists(directory))
|
|
{
|
|
Directory.CreateDirectory(directory);
|
|
}
|
|
|
|
fileName = Path.Combine(directory, fileName);
|
|
}
|
|
}
|
|
while (File.Exists(fileName));
|
|
|
|
logger?.WriteLine($"Capturing message to file {fileName}...");
|
|
|
|
File.WriteAllText(fileName, messageBody, Encoding.UTF8);
|
|
}
|
|
}
|
|
}
|