1
0
mirror of https://github.com/JKorf/CryptoExchange.Net synced 2025-06-24 16:26:25 +00:00

Updated threadsafefilewriter to check single instance, added Exception catch to deserialize

This commit is contained in:
JKorf 2018-05-04 10:52:38 +02:00
parent 754f49e0c4
commit 56d5bbcdf4
4 changed files with 44 additions and 16 deletions

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Newtonsoft.Json;
@ -25,10 +26,19 @@ namespace CryptoExchange.Net.Converters
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var val = Mapping.SingleOrDefault(v => v.Value == reader.Value.ToString()).Key;
if (val != null)
return val;
return Mapping.Single(v => v.Value.ToLower() == reader.Value.ToString().ToLower()).Key;
if (reader.Value == null)
return null;
var value = reader.Value.ToString();
if (Mapping.ContainsValue(value))
return Mapping.Single(m => m.Value == value).Key;
var lowerResult = Mapping.SingleOrDefault(m => m.Value.ToLower() == value.ToLower());
if (!lowerResult.Equals(default(KeyValuePair<T, string>)))
return lowerResult.Value;
Debug.WriteLine($"Cannot map enum. Type: {typeof(T)}, Value: {value}");
return null;
}
public T ReadString(string data)

View File

@ -206,13 +206,19 @@ namespace CryptoExchange.Net
}
catch (JsonReaderException jre)
{
var info = $"{jre.Message}, Path: {jre.Path}, LineNumber: {jre.LineNumber}, LinePosition: {jre.LinePosition}. Received data: {data}";
var info = $"Deserialize JsonReaderException: {jre.Message}, Path: {jre.Path}, LineNumber: {jre.LineNumber}, LinePosition: {jre.LinePosition}. Received data: {data}";
log.Write(LogVerbosity.Error, info);
return new CallResult<T>(null, new DeserializeError(info));
}
catch (JsonSerializationException jse)
{
var info = $"{jse.Message}. Received data: {data}";
var info = $"Deserialize JsonSerializationException: {jse.Message}. Received data: {data}";
log.Write(LogVerbosity.Error, info);
return new CallResult<T>(null, new DeserializeError(info));
}
catch(Exception ex)
{
var info = $"Deserialize Unknown Exception: {ex.Message}. Received data: {data}";
log.Write(LogVerbosity.Error, info);
return new CallResult<T>(null, new DeserializeError(info));
}

View File

@ -11,6 +11,7 @@ namespace CryptoExchange.Net.Logging
private List<TextWriter> writers;
private LogVerbosity level = LogVerbosity.Info;
public LogVerbosity Level
{
get => level;
@ -36,16 +37,16 @@ namespace CryptoExchange.Net.Logging
public void Write(LogVerbosity logType, string message)
{
foreach (var writer in writers)
string logMessage = $"{DateTime.Now:yyyy/MM/dd HH:mm:ss:fff} | {logType} | {message}";
foreach (var writer in writers.ToList())
{
try
{
if ((int) logType >= (int) Level)
writer.WriteLine($"{DateTime.Now:yyyy/MM/dd hh:mm:ss:fff} | {logType} | {message}");
writer.WriteLine(logMessage);
}
catch (Exception e)
{
Debug.WriteLine("Failed to write log: " + e.Message);
Debug.WriteLine($"Failed to write log to writer {writer.GetType()}: " + e.Message);
}
}
}

View File

@ -1,10 +1,14 @@
using System.IO;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace CryptoExchange.Net.Logging
{
public class ThreadSafeFileWriter: TextWriter
{
private static object openedFilesLock = new object();
private static List<string> openedFiles = new List<string>();
private StreamWriter logWriter;
private object writeLock;
@ -12,15 +16,22 @@ namespace CryptoExchange.Net.Logging
public ThreadSafeFileWriter(string path)
{
writeLock = new object();
logWriter = new StreamWriter(File.Open(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read));
logWriter = new StreamWriter(File.Open(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite));
logWriter.AutoFlush = true;
}
writeLock = new object();
lock(openedFilesLock)
{
if (openedFiles.Contains(path))
throw new System.Exception("Can't have multiple ThreadSafeFileWriters for the same file, reuse a single instance");
openedFiles.Add(path);
}
}
public override void WriteLine(string logMessage)
{
lock (writeLock)
lock(writeLock)
logWriter.WriteLine(logMessage);
}