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:
parent
754f49e0c4
commit
56d5bbcdf4
@ -1,5 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
@ -25,10 +26,19 @@ namespace CryptoExchange.Net.Converters
|
|||||||
|
|
||||||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
||||||
{
|
{
|
||||||
var val = Mapping.SingleOrDefault(v => v.Value == reader.Value.ToString()).Key;
|
if (reader.Value == null)
|
||||||
if (val != null)
|
return null;
|
||||||
return val;
|
|
||||||
return Mapping.Single(v => v.Value.ToLower() == reader.Value.ToString().ToLower()).Key;
|
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)
|
public T ReadString(string data)
|
||||||
|
@ -206,13 +206,19 @@ namespace CryptoExchange.Net
|
|||||||
}
|
}
|
||||||
catch (JsonReaderException jre)
|
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);
|
log.Write(LogVerbosity.Error, info);
|
||||||
return new CallResult<T>(null, new DeserializeError(info));
|
return new CallResult<T>(null, new DeserializeError(info));
|
||||||
}
|
}
|
||||||
catch (JsonSerializationException jse)
|
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);
|
log.Write(LogVerbosity.Error, info);
|
||||||
return new CallResult<T>(null, new DeserializeError(info));
|
return new CallResult<T>(null, new DeserializeError(info));
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,7 @@ namespace CryptoExchange.Net.Logging
|
|||||||
private List<TextWriter> writers;
|
private List<TextWriter> writers;
|
||||||
private LogVerbosity level = LogVerbosity.Info;
|
private LogVerbosity level = LogVerbosity.Info;
|
||||||
|
|
||||||
|
|
||||||
public LogVerbosity Level
|
public LogVerbosity Level
|
||||||
{
|
{
|
||||||
get => level;
|
get => level;
|
||||||
@ -36,16 +37,16 @@ namespace CryptoExchange.Net.Logging
|
|||||||
|
|
||||||
public void Write(LogVerbosity logType, string message)
|
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
|
try
|
||||||
{
|
{
|
||||||
if ((int) logType >= (int) Level)
|
writer.WriteLine(logMessage);
|
||||||
writer.WriteLine($"{DateTime.Now:yyyy/MM/dd hh:mm:ss:fff} | {logType} | {message}");
|
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
Debug.WriteLine("Failed to write log: " + e.Message);
|
Debug.WriteLine($"Failed to write log to writer {writer.GetType()}: " + e.Message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,14 @@
|
|||||||
using System.IO;
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace CryptoExchange.Net.Logging
|
namespace CryptoExchange.Net.Logging
|
||||||
{
|
{
|
||||||
public class ThreadSafeFileWriter: TextWriter
|
public class ThreadSafeFileWriter: TextWriter
|
||||||
{
|
{
|
||||||
|
private static object openedFilesLock = new object();
|
||||||
|
private static List<string> openedFiles = new List<string>();
|
||||||
|
|
||||||
private StreamWriter logWriter;
|
private StreamWriter logWriter;
|
||||||
private object writeLock;
|
private object writeLock;
|
||||||
|
|
||||||
@ -12,16 +16,23 @@ namespace CryptoExchange.Net.Logging
|
|||||||
|
|
||||||
public ThreadSafeFileWriter(string path)
|
public ThreadSafeFileWriter(string path)
|
||||||
{
|
{
|
||||||
writeLock = new object();
|
logWriter = new StreamWriter(File.Open(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite));
|
||||||
logWriter = new StreamWriter(File.Open(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read));
|
|
||||||
logWriter.AutoFlush = true;
|
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)
|
public override void WriteLine(string logMessage)
|
||||||
{
|
{
|
||||||
lock (writeLock)
|
lock(writeLock)
|
||||||
logWriter.WriteLine(logMessage);
|
logWriter.WriteLine(logMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void Dispose(bool disposing)
|
protected override void Dispose(bool disposing)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user