From 3600a869719e9b4aea654044dc283f5ba2864a0f Mon Sep 17 00:00:00 2001 From: vdemydiuk Date: Thu, 15 Feb 2018 17:37:08 +0200 Subject: [PATCH] MtApiService version 1.0.29. Added log wrapper class MtLog --- MTApiService/LogConfigurator.cs | 52 +++++++++++++++++++++++++ MTApiService/Properties/AssemblyInfo.cs | 4 +- MtApi5/MtApi5Client.cs | 9 ++++- MtApi5/MtConverters.cs | 41 ++++++++++++++++--- 4 files changed, 97 insertions(+), 9 deletions(-) diff --git a/MTApiService/LogConfigurator.cs b/MTApiService/LogConfigurator.cs index 42ffae13..374dd101 100755 --- a/MTApiService/LogConfigurator.cs +++ b/MTApiService/LogConfigurator.cs @@ -8,6 +8,50 @@ using log4net.Repository.Hierarchy; namespace MTApiService { + public class MtLog + { + #region ctor + internal MtLog(Type type) + { + _log = LogManager.GetLogger(type); + } + #endregion + + #region Public + + public void Debug(object message) + { + _log.Debug(message); + } + + public void Error(object message) + { + _log.Error(message); + } + + public void Fatal(object message) + { + _log.Fatal(message); + } + + public void Info(object message) + { + _log.Info(message); + } + + public void Warn(object message) + { + _log.Warn(message); + } + #endregion + + #region Private + + private readonly ILog _log; + + #endregion + } + public class LogConfigurator { private const string LogFileNameExtension = "txt"; @@ -48,5 +92,13 @@ namespace MTApiService #endif hierarchy.Configured = true; } + + public static MtLog GetLogger(Type type) + { + if (type == null) + throw new ArgumentNullException(nameof(type)); + + return new MtLog(type); + } } } diff --git a/MTApiService/Properties/AssemblyInfo.cs b/MTApiService/Properties/AssemblyInfo.cs index c85694b7..dce8a23b 100644 --- a/MTApiService/Properties/AssemblyInfo.cs +++ b/MTApiService/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ using System.Runtime.InteropServices; // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.28.0")] -[assembly: AssemblyFileVersion("1.0.28.0")] \ No newline at end of file +[assembly: AssemblyVersion("1.0.29.0")] +[assembly: AssemblyFileVersion("1.0.29.0")] \ No newline at end of file diff --git a/MtApi5/MtApi5Client.cs b/MtApi5/MtApi5Client.cs index b501571c..f87194cd 100755 --- a/MtApi5/MtApi5Client.cs +++ b/MtApi5/MtApi5Client.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using MTApiService; using System.Collections; +using System.Diagnostics.CodeAnalysis; using System.ServiceModel; using MtApi5.Requests; using MtApi5.Responses; @@ -11,6 +12,7 @@ using System.Threading.Tasks; namespace MtApi5 { + [SuppressMessage("ReSharper", "InconsistentNaming")] public class MtApi5Client { #region MT Constants @@ -32,9 +34,11 @@ namespace MtApi5 #region Private Fields + private static readonly MtLog Log = LogConfigurator.GetLogger(typeof(MtApi5Client)); + private MtClient _client; private readonly object _locker = new object(); - private volatile bool _isBacktestingMode = false; + private volatile bool _isBacktestingMode; private Mt5ConnectionState _connectionState = Mt5ConnectionState.Disconnected; private int _executorHandle; #endregion @@ -78,7 +82,7 @@ namespace MtApi5 public IEnumerable GetQuotes() { var client = Client; - var quotes = client != null ? client.GetQuotes() : null; + var quotes = client?.GetQuotes(); return quotes?.Select(q => q.Parse()); } @@ -533,6 +537,7 @@ namespace MtApi5 /// Stop Loss price /// Take Profit price /// comment + /// output result /// true - successful check of the basic structures, otherwise - false. public bool PositionOpen(string symbol, ENUM_ORDER_TYPE orderType, double volume, double price, double sl, double tp, string comment , out MqlTradeResult result) { diff --git a/MtApi5/MtConverters.cs b/MtApi5/MtConverters.cs index e1e47336..d0447d7c 100755 --- a/MtApi5/MtConverters.cs +++ b/MtApi5/MtConverters.cs @@ -6,14 +6,18 @@ namespace MtApi5 { internal static class MtConverters { + private static readonly MtLog Log = LogConfigurator.GetLogger(typeof(MtConverters)); + #region Values Converters public static Mt5Quote Parse(this MtQuote quote) { - return (quote != null) ? new Mt5Quote(quote.Instrument, quote.Bid, quote.Ask) : null; + return quote != null ? new Mt5Quote(quote.Instrument, quote.Bid, quote.Ask) : null; } public static bool ParseResult(this string inputString, char separator, out MqlTradeResult result) { + Log.Debug($"ParseResult: inputString = {inputString}, separator = {separator}"); + var retVal = false; result = null; @@ -38,17 +42,25 @@ namespace MtApi5 result = new MqlTradeResult(retcode, deal, order, volume, price, bid, ask, comment, requestId); } - catch (Exception) + catch (Exception ex) { + Log.Error($"ParseResult: {ex.Message}"); + retVal = false; } } } + else + { + Log.Warn("ParseResult: input srting is null or empty!"); + } return retVal; } public static bool ParseResult(this string inputString, char separator, out MqlTradeCheckResult result) { + Log.Debug($"ParseResult: inputString = {inputString}, separator = {separator}"); + var retVal = false; result = null; @@ -72,18 +84,25 @@ namespace MtApi5 result = new MqlTradeCheckResult(retcode, balance, equity, profit, margin, marginFree, marginLevel, comment); } - catch (Exception) + catch (Exception ex) { retVal = false; + Log.Error($"ParseResult: {ex.Message}"); } } } + else + { + Log.Warn("ParseResult: input srting is null or empty!"); + } return retVal; } public static bool ParseResult(this string inputString, char separator, out double result) { + Log.Debug($"ParseResult: inputString = {inputString}, separator = {separator}"); + var retVal = false; result = 0; @@ -98,18 +117,25 @@ namespace MtApi5 result = double.Parse(values[1]); } - catch (Exception) + catch (Exception ex) { retVal = false; + Log.Error($"ParseResult: {ex.Message}"); } } } + else + { + Log.Warn("ParseResult: input srting is null or empty!"); + } return retVal; } public static bool ParseResult(this string inputString, char separator, out DateTime from, out DateTime to) { + Log.Debug($"ParseResult: inputString = {inputString}, separator = {separator}"); + var retVal = false; from = new DateTime(); @@ -130,12 +156,17 @@ namespace MtApi5 var iTo= int.Parse(values[2]); to = Mt5TimeConverter.ConvertFromMtTime(iTo); } - catch (Exception) + catch (Exception ex) { retVal = false; + Log.Error($"ParseResult: {ex.Message}"); } } } + else + { + Log.Warn("ParseResult: input srting is null or empty!"); + } return retVal; }