Skip to content

Commit 373bfe4

Browse files
committed
[MT4] Log improvements
1 parent 0cd8edb commit 373bfe4

File tree

1 file changed

+62
-1
lines changed

1 file changed

+62
-1
lines changed

MtApi/MtApiClient.cs

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public sealed class MtApiClient
2626
#endregion
2727

2828
#region Private Fields
29+
private static readonly MtLog Log = LogConfigurator.GetLogger(typeof(MtApiClient));
30+
2931
private MtClient _client;
3032
private readonly object _locker = new object();
3133
private MtConnectionState _connectionState = MtConnectionState.Disconnected;
@@ -37,7 +39,12 @@ public sealed class MtApiClient
3739

3840
public MtApiClient()
3941
{
40-
LogConfigurator.Setup(LogProfileName);
42+
#if (DEBUG)
43+
const LogLevel logLevel = LogLevel.Debug;
44+
#else
45+
const LogLevel logLevel = LogLevel.Info;
46+
#endif
47+
LogConfigurator.Setup(LogProfileName, logLevel);
4148
}
4249
#endregion
4350

@@ -49,6 +56,7 @@ public MtApiClient()
4956
///<param name="port">Port of host connection (default 8222) </param>
5057
public void BeginConnect(string host, int port)
5158
{
59+
Log.Info($"BeginConnect: host = {host}, port = {port}");
5260
Task.Factory.StartNew(() => Connect(host, port));
5361
}
5462

@@ -58,6 +66,7 @@ public void BeginConnect(string host, int port)
5866
///<param name="port">Port of host connection (default 8222) </param>
5967
public void BeginConnect(int port)
6068
{
69+
Log.Info($"BeginConnect: port = {port}");
6170
Task.Factory.StartNew(() => Connect(port));
6271
}
6372

@@ -66,6 +75,7 @@ public void BeginConnect(int port)
6675
///</summary>
6776
public void BeginDisconnect()
6877
{
78+
Log.Info("BeginDisconnect called.");
6979
Task.Factory.StartNew(() => Disconnect(false));
7080
}
7181

@@ -251,6 +261,8 @@ public double OrderSwap()
251261
public int OrderSend(string symbol, TradeOperation cmd, double volume, double price, int slippage, double stoploss, double takeprofit
252262
, string comment, int magic, DateTime expiration, Color arrowColor)
253263
{
264+
Log.Debug($"OrderSend: symbol = {symbol}, cmd = {cmd}, volume = {volume}, price = {price}, slippage = {slippage}, stoploss = {stoploss}, takeprofit = {takeprofit}, comment = {comment}, magic = {magic}, expiration = {expiration}, arrowColor = {arrowColor}");
265+
254266
var response = SendRequest<OrderSendResponse>(new OrderSendRequest
255267
{
256268
Symbol = symbol,
@@ -271,6 +283,8 @@ public int OrderSend(string symbol, TradeOperation cmd, double volume, double pr
271283
public int OrderSend(string symbol, TradeOperation cmd, double volume, double price, int slippage, double stoploss, double takeprofit
272284
, string comment, int magic, DateTime expiration)
273285
{
286+
Log.Debug($"OrderSend: symbol = {symbol}, cmd = {cmd}, volume = {volume}, price = {price}, slippage = {slippage}, stoploss = {stoploss}, takeprofit = {takeprofit}, comment = {comment}, magic = {magic}, expiration = {expiration}");
287+
274288
var response = SendRequest<OrderSendResponse>(new OrderSendRequest
275289
{
276290
Symbol = symbol,
@@ -290,6 +304,8 @@ public int OrderSend(string symbol, TradeOperation cmd, double volume, double pr
290304
public int OrderSend(string symbol, TradeOperation cmd, double volume, double price, int slippage, double stoploss, double takeprofit
291305
, string comment, int magic)
292306
{
307+
Log.Debug($"OrderSend: symbol = {symbol}, cmd = {cmd}, volume = {volume}, price = {price}, slippage = {slippage}, stoploss = {stoploss}, takeprofit = {takeprofit}, comment = {comment}, magic = {magic}");
308+
293309
var response = SendRequest<OrderSendResponse>(new OrderSendRequest
294310
{
295311
Symbol = symbol,
@@ -308,6 +324,8 @@ public int OrderSend(string symbol, TradeOperation cmd, double volume, double pr
308324
public int OrderSend(string symbol, TradeOperation cmd, double volume, double price, int slippage, double stoploss, double takeprofit
309325
, string comment)
310326
{
327+
Log.Debug($"OrderSend: symbol = {symbol}, cmd = {cmd}, volume = {volume}, price = {price}, slippage = {slippage}, stoploss = {stoploss}, takeprofit = {takeprofit}, comment = {comment}");
328+
311329
var response = SendRequest<OrderSendResponse>(new OrderSendRequest
312330
{
313331
Symbol = symbol,
@@ -324,6 +342,8 @@ public int OrderSend(string symbol, TradeOperation cmd, double volume, double pr
324342

325343
public int OrderSend(string symbol, TradeOperation cmd, double volume, double price, int slippage, double stoploss, double takeprofit)
326344
{
345+
Log.Debug($"OrderSend: symbol = {symbol}, cmd = {cmd}, volume = {volume}, price = {price}, slippage = {slippage}, stoploss = {stoploss}, takeprofit = {takeprofit}");
346+
327347
var response = SendRequest<OrderSendResponse>(new OrderSendRequest
328348
{
329349
Symbol = symbol,
@@ -339,13 +359,17 @@ public int OrderSend(string symbol, TradeOperation cmd, double volume, double pr
339359

340360
public int OrderSend(string symbol, TradeOperation cmd, double volume, string price, int slippage, double stoploss, double takeprofit)
341361
{
362+
Log.Debug($"OrderSend: symbol = {symbol}, cmd = {cmd}, volume = {volume}, price = {price}, slippage = {slippage}, stoploss = {stoploss}, takeprofit = {takeprofit}");
363+
342364
double dPrice;
343365
return double.TryParse(price, out dPrice) ?
344366
OrderSend(symbol, cmd, volume, dPrice, slippage, stoploss, takeprofit) : 0;
345367
}
346368

347369
public int OrderSendBuy(string symbol, double volume, int slippage)
348370
{
371+
Log.Debug($"OrderSendBuy: symbol = {symbol}, volume = {volume}, slippage = {slippage}");
372+
349373
return OrderSendBuy(symbol, volume, slippage, 0, 0, null, 0);
350374
}
351375

@@ -366,6 +390,8 @@ public int OrderSendSell(string symbol, double volume, int slippage, double stop
366390

367391
public int OrderSendBuy(string symbol, double volume, int slippage, double stoploss, double takeprofit, string comment, int magic)
368392
{
393+
Log.Debug($"OrderSendBuy: symbol = {symbol}, volume = {volume}, slippage = {slippage}, stoploss = {stoploss}, takeprofit = {takeprofit}, comment = {comment}, magic = {magic}");
394+
369395
var response = SendRequest<OrderSendResponse>(new OrderSendRequest
370396
{
371397
Symbol = symbol,
@@ -382,6 +408,8 @@ public int OrderSendBuy(string symbol, double volume, int slippage, double stopl
382408

383409
public int OrderSendSell(string symbol, double volume, int slippage, double stoploss, double takeprofit, string comment, int magic)
384410
{
411+
Log.Debug($"OrderSendSell: symbol = {symbol}, volume = {volume}, slippage = {slippage}, stoploss = {stoploss}, takeprofit = {takeprofit}, comment = {comment}, magic = {magic}");
412+
385413
var response = SendRequest<OrderSendResponse>(new OrderSendRequest
386414
{
387415
Symbol = symbol,
@@ -398,6 +426,8 @@ public int OrderSendSell(string symbol, double volume, int slippage, double stop
398426

399427
public bool OrderClose(int ticket, double lots, double price, int slippage, Color color)
400428
{
429+
Log.Debug($"OrderClose: ticket = {ticket}, lots = {lots}, price = {price}, slippage = {slippage}, color = {color}");
430+
401431
var response = SendRequest<ResponseBase>(new OrderCloseRequest
402432
{
403433
Ticket = ticket,
@@ -411,6 +441,8 @@ public bool OrderClose(int ticket, double lots, double price, int slippage, Colo
411441

412442
public bool OrderClose(int ticket, double lots, double price, int slippage)
413443
{
444+
Log.Debug($"OrderClose: ticket = {ticket}, lots = {lots}, price = {price}, slippage = {slippage}");
445+
414446
var response = SendRequest<ResponseBase>(new OrderCloseRequest
415447
{
416448
Ticket = ticket,
@@ -423,6 +455,8 @@ public bool OrderClose(int ticket, double lots, double price, int slippage)
423455

424456
public bool OrderClose(int ticket, double lots, int slippage)
425457
{
458+
Log.Debug($"OrderClose: ticket = {ticket}, lots = {lots}, slippage = {slippage}");
459+
426460
var response = SendRequest<ResponseBase>(new OrderCloseRequest
427461
{
428462
Ticket = ticket,
@@ -434,6 +468,8 @@ public bool OrderClose(int ticket, double lots, int slippage)
434468

435469
public bool OrderClose(int ticket, int slippage)
436470
{
471+
Log.Debug($"OrderClose: ticket = {ticket}, slippage = {slippage}");
472+
437473
var response = SendRequest<ResponseBase>(new OrderCloseRequest
438474
{
439475
Ticket = ticket,
@@ -444,6 +480,8 @@ public bool OrderClose(int ticket, int slippage)
444480

445481
public bool OrderCloseBy(int ticket, int opposite, Color color)
446482
{
483+
Log.Debug($"OrderCloseBy: ticket = {ticket}, opposite = {opposite}, color = {color}");
484+
447485
var response = SendRequest<ResponseBase>(new OrderCloseByRequest
448486
{
449487
Ticket = ticket,
@@ -455,6 +493,8 @@ public bool OrderCloseBy(int ticket, int opposite, Color color)
455493

456494
public bool OrderCloseBy(int ticket, int opposite)
457495
{
496+
Log.Debug($"OrderCloseBy: ticket = {ticket}, opposite = {opposite}");
497+
458498
var response = SendRequest<ResponseBase>(new OrderCloseByRequest
459499
{
460500
Ticket = ticket,
@@ -465,6 +505,8 @@ public bool OrderCloseBy(int ticket, int opposite)
465505

466506
public bool OrderDelete(int ticket, Color color)
467507
{
508+
Log.Debug($"OrderDelete: ticket = {ticket}, color = {color}");
509+
468510
var response = SendRequest<ResponseBase>(new OrderDeleteRequest
469511
{
470512
Ticket = ticket,
@@ -475,6 +517,8 @@ public bool OrderDelete(int ticket, Color color)
475517

476518
public bool OrderDelete(int ticket)
477519
{
520+
Log.Debug($"OrderDelete: ticket = {ticket}");
521+
478522
var response = SendRequest<ResponseBase>(new OrderDeleteRequest
479523
{
480524
Ticket = ticket,
@@ -484,6 +528,8 @@ public bool OrderDelete(int ticket)
484528

485529
public bool OrderModify(int ticket, double price, double stoploss, double takeprofit, DateTime expiration, Color arrowColor)
486530
{
531+
Log.Debug($"OrderModify: ticket = {ticket}, price = {price}, stoploss = {stoploss}, takeprofit = {takeprofit}, expiration = {expiration}, arrowColor = {arrowColor}");
532+
487533
var response = SendRequest<ResponseBase>(new OrderModifyRequest
488534
{
489535
Ticket = ticket,
@@ -498,6 +544,8 @@ public bool OrderModify(int ticket, double price, double stoploss, double takepr
498544

499545
public bool OrderModify(int ticket, double price, double stoploss, double takeprofit, DateTime expiration)
500546
{
547+
Log.Debug($"OrderModify: ticket = {ticket}, price = {price}, stoploss = {stoploss}, takeprofit = {takeprofit}, expiration = {expiration}");
548+
501549
var response = SendRequest<ResponseBase>(new OrderModifyRequest
502550
{
503551
Ticket = ticket,
@@ -516,6 +564,8 @@ public void OrderPrint()
516564

517565
public bool OrderSelect(int index, OrderSelectMode select, OrderSelectSource pool)
518566
{
567+
Log.Debug($"OrderSelect: index = {index}, select = {select}, pool = {pool}");
568+
519569
var commandParameters = new ArrayList { index, (int)select, (int)pool };
520570
return SendCommand<bool>(MtCommandType.OrderSelect, commandParameters);
521571
}
@@ -2894,6 +2944,8 @@ private void Connect(MtClient client)
28942944
{
28952945
client.Dispose();
28962946
message = string.IsNullOrEmpty(client.Host) ? $"Failed connection to localhost:{client.Port}. {e.Message}" : $"Failed connection to {client.Host}:{client.Port}. {e.Message}";
2947+
2948+
Log.Warn(message);
28972949
}
28982950

28992951
if (state == MtConnectionState.Connected)
@@ -2906,6 +2958,8 @@ private void Connect(MtClient client)
29062958
_client.ServerFailed += _client_ServerFailed;
29072959
_client.MtEventReceived += _client_MtEventReceived;
29082960
message = string.IsNullOrEmpty(client.Host) ? $"Connected to localhost:{client.Port}" : $"Connected to { client.Host}:{client.Port}";
2961+
2962+
Log.Info(message);
29092963
}
29102964

29112965
_connectionState = state;
@@ -2973,6 +3027,7 @@ private void Disconnect(bool failed)
29733027
_connectionState = state;
29743028
}
29753029

3030+
Log.Info(message);
29763031

29773032
ConnectionStateChanged?.Invoke(this, new MtConnectionEventArgs(state, message));
29783033
}
@@ -2984,6 +3039,7 @@ private T SendCommand<T>(MtCommandType commandType, ArrayList commandParameters,
29843039
var client = Client;
29853040
if (client == null)
29863041
{
3042+
Log.Warn("SendCommand: No connection");
29873043
throw new MtConnectionException("No connection");
29883044
}
29893045

@@ -2993,16 +3049,19 @@ private T SendCommand<T>(MtCommandType commandType, ArrayList commandParameters,
29933049
}
29943050
catch (CommunicationException ex)
29953051
{
3052+
Log.Warn($"SendCommand: {ex.Message}");
29963053
throw new MtConnectionException(ex.Message, ex);
29973054
}
29983055

29993056
if (response == null)
30003057
{
3058+
Log.Warn("SendCommand: Response from MetaTrader is null");
30013059
throw new MtExecutionException(MtErrorCode.MtApiCustomError, "Response from MetaTrader is null");
30023060
}
30033061

30043062
if (response.ErrorCode != 0)
30053063
{
3064+
Log.Warn($"SendCommand: ErrorCode = {response.ErrorCode}. {response}");
30063065
throw new MtExecutionException((MtErrorCode)response.ErrorCode, response.ToString());
30073066
}
30083067

@@ -3026,12 +3085,14 @@ private T SendCommand<T>(MtCommandType commandType, ArrayList commandParameters,
30263085

30273086
if (res == null)
30283087
{
3088+
Log.Warn("SendRequest: Response from MetaTrader is null");
30293089
throw new MtExecutionException(MtErrorCode.MtApiCustomError, "Response from MetaTrader is null");
30303090
}
30313091

30323092
var response = JsonConvert.DeserializeObject<T>(res);
30333093
if (response.ErrorCode != 0)
30343094
{
3095+
Log.Warn($"SendRequest: ErrorCode = {response.ErrorCode}. {response}");
30353096
throw new MtExecutionException((MtErrorCode)response.ErrorCode, response.ErrorMessage);
30363097
}
30373098

0 commit comments

Comments
 (0)