diff --git a/csharp/TraderBot/.gitignore b/csharp/TraderBot/.gitignore index 361e824f..4f4a3286 100644 --- a/csharp/TraderBot/.gitignore +++ b/csharp/TraderBot/.gitignore @@ -1,3 +1,4 @@ appsettings.json log.txt -log.*.txt \ No newline at end of file +log.*.txt +logs/ \ No newline at end of file diff --git a/csharp/TraderBot/NLog.config b/csharp/TraderBot/NLog.config new file mode 100644 index 00000000..bc44ea8a --- /dev/null +++ b/csharp/TraderBot/NLog.config @@ -0,0 +1,46 @@ + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/csharp/TraderBot/Program.cs b/csharp/TraderBot/Program.cs index e5ac64bb..e9f59dc1 100644 --- a/csharp/TraderBot/Program.cs +++ b/csharp/TraderBot/Program.cs @@ -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(_ => diff --git a/csharp/TraderBot/TraderBot.csproj b/csharp/TraderBot/TraderBot.csproj index 12374722..4bcd23e8 100644 --- a/csharp/TraderBot/TraderBot.csproj +++ b/csharp/TraderBot/TraderBot.csproj @@ -13,8 +13,15 @@ + + + + PreserveNewest + + + diff --git a/examples/test_logging.cs b/examples/test_logging.cs new file mode 100644 index 00000000..0fcc0c8f --- /dev/null +++ b/examples/test_logging.cs @@ -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>(); + +// 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."); \ No newline at end of file