Skip to content

Commit 4bddd7b

Browse files
konardclaude
andcommitted
Implement file-based error logging for all OSes
- Add NLog.Extensions.Logging package for cross-platform file logging - Configure NLog with separate error and all logs files - Add log rotation and archiving (10MB files, 7 error archives, 3 all archives) - Update Program.cs to use NLog provider - Add logs/ directory to .gitignore - Ensure NLog.config is copied to output directory Fixes #114 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent bb9e502 commit 4bddd7b

File tree

5 files changed

+92
-1
lines changed

5 files changed

+92
-1
lines changed

csharp/TraderBot/.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
appsettings.json
22
log.txt
3-
log.*.txt
3+
log.*.txt
4+
logs/

csharp/TraderBot/NLog.config

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4+
5+
<targets>
6+
<!-- File target for errors and above -->
7+
<target xsi:type="File"
8+
name="errorFile"
9+
fileName="${basedir}/logs/errors-${shortdate}.log"
10+
archiveFileName="${basedir}/logs/archived/errors-{#}.log"
11+
archiveAboveSize="10485760"
12+
archiveNumbering="Rolling"
13+
maxArchiveFiles="7"
14+
concurrentWrites="true"
15+
keepFileOpen="false"
16+
layout="${longdate} ${uppercase:${level}} ${logger} ${message} ${exception:format=tostring}" />
17+
18+
<!-- File target for all logs -->
19+
<target xsi:type="File"
20+
name="allFile"
21+
fileName="${basedir}/logs/all-${shortdate}.log"
22+
archiveFileName="${basedir}/logs/archived/all-{#}.log"
23+
archiveAboveSize="10485760"
24+
archiveNumbering="Rolling"
25+
maxArchiveFiles="3"
26+
concurrentWrites="true"
27+
keepFileOpen="false"
28+
layout="${longdate} ${uppercase:${level}} ${logger} ${message} ${exception:format=tostring}" />
29+
30+
<!-- Console target for development -->
31+
<target xsi:type="Console"
32+
name="console"
33+
layout="${time} ${uppercase:${level}} ${message} ${exception:format=tostring}" />
34+
</targets>
35+
36+
<rules>
37+
<!-- Log all errors and above to error file -->
38+
<logger name="*" minlevel="Error" writeTo="errorFile" />
39+
40+
<!-- Log everything to all file -->
41+
<logger name="*" minlevel="Info" writeTo="allFile" />
42+
43+
<!-- Log to console for development -->
44+
<logger name="*" minlevel="Info" writeTo="console" />
45+
</rules>
46+
</nlog>

csharp/TraderBot/Program.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,18 @@
22
using Microsoft.Extensions.DependencyInjection;
33
using Microsoft.Extensions.Configuration;
44
using Microsoft.Extensions.Configuration.UserSecrets;
5+
using Microsoft.Extensions.Logging;
56
using Tinkoff.InvestApi;
67
using TraderBot;
8+
using NLog.Extensions.Logging;
79

810
var builder = Host.CreateDefaultBuilder(args);
911
var host = builder
12+
.ConfigureLogging(logging =>
13+
{
14+
logging.ClearProviders();
15+
logging.AddNLog();
16+
})
1017
.ConfigureServices((context, services) =>
1118
{
1219
services.AddSingleton(_ =>

csharp/TraderBot/TraderBot.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,15 @@
1313
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="9.0.2" />
1414
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.2" />
1515
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.2" />
16+
<PackageReference Include="NLog.Extensions.Logging" Version="5.3.8" />
1617
<PackageReference Include="Platform.Data.Doublets.Sequences" Version="0.1.1" />
1718
<PackageReference Include="Tinkoff.InvestApi" Version="0.6.1" />
1819
</ItemGroup>
1920

21+
<ItemGroup>
22+
<None Update="NLog.config">
23+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
24+
</None>
25+
</ItemGroup>
26+
2027
</Project>

examples/test_logging.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
using Microsoft.Extensions.Logging;
3+
using NLog.Extensions.Logging;
4+
5+
// Create minimal host to test logging
6+
var services = new ServiceCollection();
7+
services.AddLogging(builder =>
8+
{
9+
builder.ClearProviders();
10+
builder.AddNLog();
11+
});
12+
13+
var serviceProvider = services.BuildServiceProvider();
14+
var logger = serviceProvider.GetRequiredService<ILogger<Program>>();
15+
16+
// Test different log levels
17+
logger.LogInformation("This is an information message");
18+
logger.LogWarning("This is a warning message");
19+
logger.LogError("This is an error message that should be logged to file");
20+
21+
try
22+
{
23+
throw new Exception("Test exception for logging");
24+
}
25+
catch (Exception ex)
26+
{
27+
logger.LogError(ex, "Test exception caught and logged");
28+
}
29+
30+
Console.WriteLine("Logging test completed. Check the logs directory for files.");

0 commit comments

Comments
 (0)