Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logging into file #162

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions csharp/FileManager/Context.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,28 @@

namespace FileManager
{
/// <summary>
/// <para>
/// Represents the context.
/// </para>
/// <para></para>
/// </summary>
/// <summary>
/// <para>
/// Represents the context.
/// </para>
/// <para></para>
/// </summary>
public class Context
{
/// <summary>
/// <para>
/// Gets or sets the args value.
/// </para>
/// <para></para>
/// </summary>
/// <summary>
/// <para>
/// Gets or sets the args value.
/// </para>
/// <para></para>
/// </summary>
public string[] Args { get; set; }

/// <summary>
/// <para>
/// Gets or sets the file storage value.
/// </para>
/// <para></para>
/// </summary>
/// <summary>
/// <para>
/// Gets or sets the file storage value.
/// </para>
/// <para></para>
/// </summary>
public FileStorage FileStorage { get; set; }
}
}
24 changes: 12 additions & 12 deletions csharp/FileManager/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@

namespace FileManager
{
/// <summary>
/// <para>
/// Represents the program.
/// </para>
/// <para></para>
/// </summary>
/// <summary>
/// <para>
/// Represents the program.
/// </para>
/// <para></para>
/// </summary>
internal class Program
{
/// <summary>
/// <para>
/// The get files by file set name trigger.
/// </para>
/// <para></para>
/// </summary>
/// <summary>
/// <para>
/// The get files by file set name trigger.
/// </para>
/// <para></para>
/// </summary>
public static List<ITrigger<Context>> Handlers = new()
{
new CreateTrigger(),
Expand Down
57 changes: 57 additions & 0 deletions csharp/TraderBot/Logger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tinkoff.InvestApi.V1;
using System.IO;
using Platform.IO;

namespace TraderBot
{
public class Logger
{
Logger<TradingService> LogProvider { get; set; }

public readonly string FileName;

public Logger(ILogger<TradingService> logger, string fileName)
{
if (string.IsNullOrWhiteSpace(fileName))
{
throw new ArgumentOutOfRangeException(nameof(fileName), "File name is empty.");
}
LogProvider = (Logger<TradingService>?)logger;
FileName = fileName;
}

public void LogInformation(string @string)
{
LogProvider.LogInformation(@string);
using var fileStream = Platform.IO.FileHelpers.Append(FileName);
using StreamWriter writer = new(fileStream);
writer.WriteLine(@string);
}
public void LogInformation(string @string, PositionsSecurities positions)
{
LogProvider.LogInformation(@string, positions);
using var fileStream = Platform.IO.FileHelpers.Append(FileName);
using StreamWriter writer = new(fileStream);
writer.WriteLine(@string);
}
public void LogError(Exception @exception,string @string)
{
LogProvider.LogError(@string, @exception);
using var fileStream = Platform.IO.FileHelpers.Append(FileName);
using StreamWriter writer = new(fileStream);
writer.WriteLine(@string);
}
public void LogError(string @string)
{
LogProvider.LogError(@string);
using var fileStream = Platform.IO.FileHelpers.Append(FileName);
using StreamWriter writer = new(fileStream);
writer.WriteLine(@string);
}
}
}
1 change: 1 addition & 0 deletions csharp/TraderBot/TraderBot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<PackageReference Include="GraphQL.Client.Serializer.Newtonsoft" Version="4.0.2" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" />
<PackageReference Include="Platform.Data.Doublets.Sequences" Version="0.1.1" />
<PackageReference Include="Platform.IO" Version="0.3.2" />
<PackageReference Include="Tinkoff.InvestApi" Version="0.1.6" />
</ItemGroup>

Expand Down
13 changes: 10 additions & 3 deletions csharp/TraderBot/TradingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,24 @@ namespace TraderBot;
public class TradingService : BackgroundService
{
protected readonly InvestApiClient InvestApi;
protected readonly ILogger<TradingService> Logger;
protected readonly TraderBot.Logger Logger;
protected readonly IHostApplicationLifetime Lifetime;
protected readonly TradingSettings Settings;
protected readonly Account CurrentAccount;
protected readonly Etf CurrentInstrument;
protected readonly decimal PriceStep;
protected decimal CashBalance;
protected string? FileToLog;
protected readonly ConcurrentDictionary<string, OrderState> ActiveBuyOrders;
protected readonly ConcurrentDictionary<string, OrderState> ActiveSellOrders;
protected readonly ConcurrentDictionary<decimal, long> LotsSets;
protected readonly ConcurrentDictionary<string, decimal> ActiveSellOrderSourcePrice;

public TradingService(ILogger<TradingService> logger, InvestApiClient investApi, IHostApplicationLifetime lifetime, TradingSettings settings)

public TradingService(ILogger<TradingService> log, InvestApiClient investApi, IHostApplicationLifetime lifetime, TradingSettings settings)
{
Logger = logger;

Logger = new Logger(log, settings.FileToLog);
InvestApi = investApi;
Lifetime = lifetime;
Settings = settings;
Expand All @@ -37,6 +40,9 @@ public TradingService(ILogger<TradingService> logger, InvestApiClient investApi,
{
Logger.LogInformation($"[{i}]: {accounts[i]}");
}
Logger.LogInformation($"selected account: {accounts[Settings.AccountIndex]}");
Logger.LogInformation($"Press any key to continue");
Console.Read();
CurrentAccount = accounts[Settings.AccountIndex];
Logger.LogInformation($"CurrentAccount (with {Settings.AccountIndex} index): {CurrentAccount}");
CurrentInstrument = InvestApi.Instruments.Etfs().Instruments.First(etf => etf.Ticker == Settings.EtfTicker);
Expand All @@ -47,6 +53,7 @@ public TradingService(ILogger<TradingService> logger, InvestApiClient investApi,
ActiveSellOrders = new ConcurrentDictionary<string, OrderState>();
LotsSets = new ConcurrentDictionary<decimal, long>();
ActiveSellOrderSourcePrice = new ConcurrentDictionary<string, decimal>();
Thread.Sleep(1000);
}

protected async Task ReceiveTrades(CancellationToken cancellationToken)
Expand Down
1 change: 1 addition & 0 deletions csharp/TraderBot/TradingSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ public class TradingSettings
public string? CashCurrency { get; set; }
public int AccountIndex { get; set; }
public bool AllowSamePriceSell { get; set; }
public string? FileToLog { get; set; }
}
7 changes: 4 additions & 3 deletions csharp/TraderBot/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
"Microsoft.Hosting.Lifetime": "Information"
}
},
"InvestApiSettings": {
Expand All @@ -12,7 +12,8 @@
"TradingSettings": {
"EtfTicker": "TRUR",
"CashCurrency": "rub",
"AccountIndex": 0,
"AllowSamePriceSell": true
"AccountIndex": 1,
"AllowSamePriceSell": true,
"FileToLog": ""
}
}