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.05k stars 429 forks source link

Method not found: Void Microsoft.Extensions.Logging.LoggerExtensions.Log #1178

Open scorcher1976 opened 1 year ago

scorcher1976 commented 1 year ago

Have anybody encountered a "Method not found" error when initializing the BinanceClient?

I'm trying to incorporate the Binance.NET API into Microsoft's ERP (Dynamics 365), but am having issues initializing the BinanceClient. It works within a console application but when I added the required dependencies into my Dynamics 365 project C# project, I get the following when instanciating the BinanceClient:

Any ideas?

Method not found: 'Void Microsoft.Extensions.Logging.LoggerExtensions.Log(Microsoft.Extensions.Logging.ILogger, Microsoft.Extensions.Logging.LogLevel, System.String, System.Object[])'. at CryptoExchange.Net.Logging.Log.Write(LogLevel logLevel, String message) at CryptoExchange.Net.BaseRestClient..ctor(String name, ClientOptions options) at Binance.Net.Clients.BinanceClient..ctor(BinanceClientOptions options) at Binance.Net.Clients.BinanceClient..ctor() at ClassLibrary2.Class2.initBinanceClient() at Dynamics.AX.Application.AHOTestBatch.`run() in xppSource://Source/AHOCryptoManagement\AxClass_AHOTestBatch.xpp:line 101 at Dynamics.AX.Application.AHOTestBatch.run()

Here's the code:

public void initBinanceClient()
    {
        var clientOptions = new BinanceClientOptions();
        clientOptions.ApiCredentials = new ApiCredentials("XXX", "YYY");
        clientOptions.SpotApiOptions.BaseAddress = "https://testnet.binance.vision";
        clientOptions.LogLevel = Microsoft.Extensions.Logging.LogLevel.None;
        clientOptions.LogWriters = new List<Microsoft.Extensions.Logging.ILogger>();
        BinanceClient.SetDefaultOptions(clientOptions);

        var client = new BinanceClient();
    }

I tried adding Nuget package Microsoft.Extensions.Logging into my dependencies but it still did not work. I also updated to the latest CryptoExchange.Net library but it didn't work either.

I am using Binance.Net 8.3.0 and CryptoExchange.Net 5.3.1.

Regards, Alex IloggerError

Hulkstance commented 1 year ago

That's because you didn't provider a logger in the ILogger collection, e.g. clientOptions.LogWriters = new List<ILogger> { new ConsoleLogger() };. You should consider Serilog in opposed to ConsoleLogger.

I suggest you also use a DI framework (IoC), e.g. ServiceCollection. https://jkorf.github.io/CryptoExchange.Net/Logging.html#serilog.

// Depends on: Microsoft.Extensions.DependencyInjection
var services = new ServiceCollection();

const string apiKey = "N/A";
const string secretKey = "N/A";
services.AddBinance((restClientOptions, socketClientOptions) =>
{
    restClientOptions.ApiCredentials = new ApiCredentials(apiKey, secretKey);
    restClientOptions.SpotApiOptions = new BinanceApiClientOptions
    {
        AutoTimestamp = true,
        TimestampRecalculationInterval = TimeSpan.FromMinutes(30),
        RateLimitingBehaviour = RateLimitingBehaviour.Wait
    };
    restClientOptions.LogLevel = LogLevel.Trace;
    restClientOptions.LogWriters = new List<ILogger> { new ConsoleLogger() };

    socketClientOptions.ApiCredentials = new ApiCredentials(apiKey, secretKey);
    socketClientOptions.SpotStreamsOptions = new SocketApiClientOptions
    {
        AutoReconnect = true,
        ReconnectInterval = TimeSpan.FromSeconds(15)
    };
}, ServiceLifetime.Transient);

var serviceProvider = services.BuildServiceProvider();

var restClient = serviceProvider.GetRequiredService<IBinanceClient>();

var callResult = await restClient.SpotApi.ExchangeData.GetExchangeInfoAsync();

if (callResult.Success)
{
    // ...
}