1
0
mirror of https://github.com/JKorf/CryptoExchange.Net.git synced 2026-08-16 19:03:01 +00:00
This commit is contained in:
Jkorf
2022-01-17 13:47:58 +01:00
parent b65669659d
commit e33e7c6775
5 changed files with 60 additions and 56 deletions
+32 -32
View File
@@ -23,7 +23,7 @@ With for example an ASP.Net Core or Blazor project the logging can be added to t
Adding `UseSerilog()` in the `CreateHostBuilder` will add the Serilog logging implementation as an ILogger which you can inject into implementations.
*Configuring Serilog as ILogger:*
````C#
```csharp
public static void Main(string[] args)
{
@@ -43,11 +43,11 @@ public static IHostBuilder CreateHostBuilder(string[] args) =>
webBuilder.UseStartup<Startup>();
});
````
```
*Injecting ILogger:*
````C#
```csharp
public class BinanceDataProvider
{
@@ -64,7 +64,7 @@ public class BinanceDataProvider
}
}
````
```
</BlockQuote>
</Details>
@@ -78,7 +78,7 @@ public class BinanceDataProvider
When using the `Add[Library]` extension method, for instance `AddBinance()`, there is a small issue that there is no available `ILogger<>` yet when adding the library. This can be solved as follows:
*Configuring Serilog as ILogger:*
````C#
```csharp
public static void Main(string[] args)
{
@@ -98,11 +98,11 @@ public static IHostBuilder CreateHostBuilder(string[] args) =>
context => new Startup(context.Configuration, LoggerFactory.Create(config => config.AddSerilog()) )); // <- this allows us to use ILoggerFactory in the Startup.cs
});
````
```
*Injecting ILogger:*
````C#
```csharp
public class Startup
{
@@ -127,7 +127,7 @@ public class Startup
}
}
````
```
</BlockQuote>
</Details>
@@ -136,7 +136,7 @@ public class Startup
If you don't have a dependency injection service available because you are for example working on a simple console application you can use a slightly different approach.
*Configuring Serilog as ILogger:*
````C#
```csharp
var serilogLogger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.Console()
@@ -145,17 +145,17 @@ var serilogLogger = new LoggerConfiguration()
var loggerFactory = (ILoggerFactory)new LoggerFactory();
loggerFactory.AddSerilog(serilogLogger);
````
```
*Injecting ILogger:*
````C#
```csharp
var client = new BinanceClient(new BinanceClientOptions
{
LogLevel = LogLevel.Trace,
LogWriters = new List<ILogger> { loggerFactory.CreateLogger("") }
});
````
```
The `BinanceClient` will now write the logging it produces to the Serilog logger.
@@ -165,7 +165,7 @@ To make the CryptoExchange.Net logging write to the Log4Net logge with for examp
Adding `AddLog4Net()` in the `ConfigureLogging` call will add the Log4Net implementation as an ILogger which you can inject into implementations. Make sure you have a log4net.config configuration file in your project.
*Configuring Log4Net as ILogger:*
````C#
```csharp
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
@@ -177,10 +177,10 @@ public static IHostBuilder CreateHostBuilder(string[] args) =>
});
webBuilder.UseStartup<Startup>();
});
````
```
*Injecting ILogger:*
````C#
```csharp
public class BinanceDataProvider
{
@@ -197,7 +197,7 @@ public class BinanceDataProvider
}
}
````
```
If you don't have the Dotnet dependency container available you'll need to provide your own ILogger implementation. See [Custom logger](#custom-logger).
@@ -210,7 +210,7 @@ With for example an ASP.Net Core or Blazor project the logging can be added to t
Adding `UseNLog()` to the `CreateHostBuilder()` method will add the NLog implementation as an ILogger which you can inject into implementations. Make sure you have a nlog.config configuration file in your project.
*Configuring NLog as ILogger:*
````C#
```csharp
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
@@ -223,10 +223,10 @@ Adding `UseNLog()` to the `CreateHostBuilder()` method will add the NLog impleme
logging.SetMinimumLevel(LogLevel.Trace);
})
.UseNLog();
````
```
*Injecting ILogger:*
````C#
```csharp
public class BinanceDataProvider
{
@@ -243,7 +243,7 @@ public class BinanceDataProvider
}
}
````
```
If you don't have the Dotnet dependency container available you'll need to provide your own ILogger implementation. See [Custom logger](#custom-logger).
@@ -251,7 +251,7 @@ If you don't have the Dotnet dependency container available you'll need to provi
If you're using a different framework or for some other reason these methods don't work for you you can create a custom ILogger implementation to receive the logging. All you need to do is create an implementation of the ILogger interface and provide that to the client.
*A simple console logging implementation (note that the ConsoleLogger is already available in the CryptoExchange.Net library)*:
````C#
```csharp
public class ConsoleLogger : ILogger
{
@@ -266,10 +266,10 @@ public class ConsoleLogger : ILogger
}
}
````
```
*Injecting the console logging implementation:*
````C#
```csharp
var client = new BinanceClient(new BinanceClientOptions
{
@@ -277,7 +277,7 @@ var client = new BinanceClient(new BinanceClientOptions
LogWriters = new List<ILogger> { new ConsoleLogger() }
});
````
```
## Provide logging for issues
A big debugging tool when opening an issue on Github is providing logging of what data caused the issue. This can be provided two ways, via the `OriginalData` property of the call result or data event, or collecting the Trace logging.
@@ -285,15 +285,15 @@ A big debugging tool when opening an issue on Github is providing logging of wha
This is only useful when there is an issue in deserialization. So either a call result is giving a Deserialization error, or the result has a value that is unexpected. If that is the issue, please provide the original data that is received so the deserialization issue can be resolved based on the received data.
By default the `OriginalData` property in the `WebCallResult`/`DataEvent` object is not filled as saving the original data has a (very small) performance penalty. To save the original data in the `OriginalData` property the `OutputOriginalData` option should be set to `true` in the client options.
*Enabled output data*
````C#
```csharp
var client = new BinanceClient(new BinanceClientOptions
{
OutputOriginalData = true
});
````
```
*Accessing original data*
````C#
```csharp
// Rest request
var tickerResult = client.SpotApi.ExchangeData.GetTickersAsync();
var originallyRecievedData = tickerResult.OriginalData;
@@ -302,23 +302,23 @@ var originallyRecievedData = tickerResult.OriginalData;
client.SpotStreams.SubscribeToAllTickerUpdatesAsync(update => {
var originallyRecievedData = update.OriginalData;
});
````
```
### Trace logging
Trace logging, which is the most verbose log level, can be enabled in the client options.
*Enabled output data*
````C#
```csharp
var client = new BinanceClient(new BinanceClientOptions
{
LogLevel = LogLevel.Trace
});
````
```
After enabling trace logging all data send to/received from the server is written to the log writers. By default this is written to the output window in Visual Studio via Debug.WriteLine, though this might be different depending on how you configured your logging.
Output data will look something like this:
````
```
2021-12-17 10:40:42:296 | Debug | Binance | Client configuration: LogLevel: Trace, Writers: 1, OutputOriginalData: False, Proxy: -, AutoReconnect: True, ReconnectInterval: 00:00:05, MaxReconnectTries: , MaxResubscribeTries: 5, MaxConcurrentResubscriptionsPerSocket: 5, SocketResponseTimeout: 00:00:10, SocketNoDataTimeout: 00:00:00, SocketSubscriptionsCombineTarget: , CryptoExchange.Net: v5.0.0.0, Binance.Net: v8.0.0.0
2021-12-17 10:40:42:410 | Debug | Binance | [15] Creating request for https://api.binance.com/api/v3/ticker/24hr
2021-12-17 10:40:42:439 | Debug | Binance | [15] Sending GET request to https://api.binance.com/api/v3/ticker/24hr?symbol=BTCUSDT with headers Accept=[application/json], X-MBX-APIKEY=[XXX]
2021-12-17 10:40:43:024 | Debug | Binance | [15] Response received in 571ms: {"symbol":"BTCUSDT","priceChange":"-1726.47000000","priceChangePercent":"-3.531","weightedAvgPrice":"48061.51544204","prevClosePrice":"48901.44000000","lastPrice":"47174.97000000","lastQty":"0.00352000","bidPrice":"47174.96000000","bidQty":"0.65849000","askPrice":"47174.97000000","askQty":"0.13802000","openPrice":"48901.44000000","highPrice":"49436.43000000","lowPrice":"46749.55000000","volume":"33136.69765000","quoteVolume":"1592599905.80360790","openTime":1639647642763,"closeTime":1639734042763,"firstId":1191596486,"lastId":1192649611,"count":1053126}
````
```
When opening an issue, please provide this logging when available.