JKorf / Binance.Net

A C# .netstandard client library for the Binance REST and Websocket Spot and Futures API focusing on clear usage and models
https://jkorf.github.io/Binance.Net/
MIT License
1.03k stars 425 forks source link

LogWriters write to text file #669

Closed eouree closed 3 years ago

eouree commented 3 years ago

Hello All,

I tried to add another log writer apart the Console.Out for writing the logs to a text file as well. I used the below code with no success. Can anyone help me on this matter? Am i doing something wrong?

                string fileName = Environment.CurrentDirectory + @"\Log.txt";

                using (StreamWriter writer = new StreamWriter(fileName))
                {
                    BinanceSocketClient.SetDefaultOptions(new BinanceSocketClientOptions
                    {
                        ApiCredentials = new ApiCredentials(mode.ApiKey, mode.ApiSecret),
                        AutoReconnect = true,
                        ReconnectInterval = TimeSpan.FromSeconds(15),
                        BaseAddress = "wss://testnet.binance.vision/",
                        LogVerbosity = LogVerbosity.Debug,
                        LogWriters = new List<TextWriter> {Console.Out, writer}
                    });
                }

Besides the above code, i also tried the Console.SetOut as here https://www.dotnetperls.com/console-setout but again didn't work.

Thanks

JKorf commented 3 years ago

You're close, but because you're using the 'using' statement for the StreamWriter the writer gets disposed immediately after the snippet. It should work if you change it to

string fileName = Environment.CurrentDirectory + @"\Log.txt";
StreamWriter writer = new StreamWriter(fileName);

BinanceSocketClient.SetDefaultOptions(new BinanceSocketClientOptions
{
    ApiCredentials = new ApiCredentials(mode.ApiKey, mode.ApiSecret),
    AutoReconnect = true,
    ReconnectInterval = TimeSpan.FromSeconds(15),
    BaseAddress = "wss://testnet.binance.vision/",
    LogVerbosity = LogVerbosity.Debug,
    LogWriters = new List<TextWriter> {Console.Out, writer}
});

Remember to close/dispose the writer when your program closes.

eouree commented 3 years ago

Thank you again