Skip to content
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
3 changes: 2 additions & 1 deletion csharp/TraderBot/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
appsettings.json
log.txt
log.*.txt
log.*.txt
logs/
46 changes: 46 additions & 0 deletions csharp/TraderBot/NLog.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<targets>
<!-- File target for errors and above -->
<target xsi:type="File"
name="errorFile"
fileName="${basedir}/logs/errors-${shortdate}.log"
archiveFileName="${basedir}/logs/archived/errors-{#}.log"
archiveAboveSize="10485760"
archiveNumbering="Rolling"
maxArchiveFiles="7"
concurrentWrites="true"
keepFileOpen="false"
layout="${longdate} ${uppercase:${level}} ${logger} ${message} ${exception:format=tostring}" />

<!-- File target for all logs -->
<target xsi:type="File"
name="allFile"
fileName="${basedir}/logs/all-${shortdate}.log"
archiveFileName="${basedir}/logs/archived/all-{#}.log"
archiveAboveSize="10485760"
archiveNumbering="Rolling"
maxArchiveFiles="3"
concurrentWrites="true"
keepFileOpen="false"
layout="${longdate} ${uppercase:${level}} ${logger} ${message} ${exception:format=tostring}" />

<!-- Console target for development -->
<target xsi:type="Console"
name="console"
layout="${time} ${uppercase:${level}} ${message} ${exception:format=tostring}" />
</targets>

<rules>
<!-- Log all errors and above to error file -->
<logger name="*" minlevel="Error" writeTo="errorFile" />

<!-- Log everything to all file -->
<logger name="*" minlevel="Info" writeTo="allFile" />

<!-- Log to console for development -->
<logger name="*" minlevel="Info" writeTo="console" />
</rules>
</nlog>
7 changes: 7 additions & 0 deletions csharp/TraderBot/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.UserSecrets;
using Microsoft.Extensions.Logging;
using Tinkoff.InvestApi;
using TraderBot;
using NLog.Extensions.Logging;

var builder = Host.CreateDefaultBuilder(args);
var host = builder
.ConfigureLogging(logging =>
{
logging.ClearProviders();
logging.AddNLog();
})
.ConfigureServices((context, services) =>
{
services.AddSingleton(_ =>
Expand Down
7 changes: 7 additions & 0 deletions csharp/TraderBot/TraderBot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,15 @@
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="9.0.2" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.2" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.2" />
<PackageReference Include="NLog.Extensions.Logging" Version="5.3.8" />
<PackageReference Include="Platform.Data.Doublets.Sequences" Version="0.1.1" />
<PackageReference Include="Tinkoff.InvestApi" Version="0.6.1" />
</ItemGroup>

<ItemGroup>
<None Update="NLog.config">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
30 changes: 30 additions & 0 deletions examples/test_logging.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NLog.Extensions.Logging;

// Create minimal host to test logging
var services = new ServiceCollection();
services.AddLogging(builder =>
{
builder.ClearProviders();
builder.AddNLog();
});

var serviceProvider = services.BuildServiceProvider();
var logger = serviceProvider.GetRequiredService<ILogger<Program>>();

// Test different log levels
logger.LogInformation("This is an information message");
logger.LogWarning("This is a warning message");
logger.LogError("This is an error message that should be logged to file");

try
{
throw new Exception("Test exception for logging");
}
catch (Exception ex)
{
logger.LogError(ex, "Test exception caught and logged");
}

Console.WriteLine("Logging test completed. Check the logs directory for files.");
Loading