ChipDNAClientCLI/ErrorLogger.cs

61 lines
1.9 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;
namespace Creditcall.ChipDna.Client
{
public static class ErrorLogger
{
private static readonly HttpClient httpClient = new HttpClient
{
Timeout = TimeSpan.FromSeconds(10)
};
public static async Task SendLogErrorAsync(Dictionary<string, string> statusResult)
{
try
{
if (statusResult == null || statusResult.Count == 0)
return;
var url = "http://127.0.0.1:9091/logerror";
var wrappedResult = new SerializableKeyValueList(statusResult);
var serializer = new XmlSerializer(typeof(SerializableKeyValueList));
var ns = new XmlSerializerNamespaces();
ns.Add(string.Empty, string.Empty);
var settings = new XmlWriterSettings
{
OmitXmlDeclaration = false,
Indent = false,
Encoding = new UTF8Encoding(false)
};
using (var ms = new MemoryStream())
{
using (var xw = XmlWriter.Create(ms, settings))
{
serializer.Serialize(xw, wrappedResult, ns);
}
var content = new ByteArrayContent(ms.ToArray());
content.Headers.ContentType =
new MediaTypeHeaderValue("application/xml");
await httpClient.PostAsync(url, content);
}
}
catch (Exception ex)
{
Console.WriteLine("SendLogErrorAsync failed: " + ex.Message);
}
}
}
}