mirror of
https://github.com/JKorf/CryptoExchange.Net.git
synced 2026-08-21 05:13:21 +00:00
Added logging docs
+126
-1
@@ -1 +1,126 @@
|
|||||||
WIP
|
The library offers extensive logging, for which you can supply your own logging implementation. The logging can be configured via the client options (see XXX).
|
||||||
|
|
||||||
|
Logging is based on the `Microsoft.Extensions.Logging.ILogger` interface. This should provide ease of use when trying to connect the library logging to your existing log implementation.
|
||||||
|
|
||||||
|
### Serilog
|
||||||
|
To make the CryptoExchange.Net logging write to the Serilog logger you can use the following ways, depending on the type of project you're using. The following examples assume that the `Serilog.Sinks.Console` package is already installed.
|
||||||
|
|
||||||
|
#### ASP.NET Core/Blazor
|
||||||
|
With an ASP.Net Core or Blazor project, make sure to install the `Serilog.AspNetCore` package (https://github.com/serilog/serilog-aspnetcore). Adding `UseSerilog` will add the serilogger implementation as an ILogger which you can inject into implementations.
|
||||||
|
|
||||||
|
*Configuring Serilog as ILogger:*
|
||||||
|
````C#
|
||||||
|
|
||||||
|
public static void Main(string[] args)
|
||||||
|
{
|
||||||
|
Log.Logger = new LoggerConfiguration()
|
||||||
|
.MinimumLevel.Debug()
|
||||||
|
.WriteTo.Console()
|
||||||
|
.CreateLogger();
|
||||||
|
|
||||||
|
CreateHostBuilder(args).Build().Run();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IHostBuilder CreateHostBuilder(string[] args) =>
|
||||||
|
Host.CreateDefaultBuilder(args)
|
||||||
|
.UseSerilog()
|
||||||
|
.ConfigureWebHostDefaults(webBuilder =>
|
||||||
|
{
|
||||||
|
webBuilder.UseStartup<Startup>();
|
||||||
|
});
|
||||||
|
|
||||||
|
````
|
||||||
|
|
||||||
|
|
||||||
|
*Injecting ILogger:*
|
||||||
|
````C#
|
||||||
|
|
||||||
|
public class BinanceDataProvider
|
||||||
|
{
|
||||||
|
BinanceClient _client;
|
||||||
|
|
||||||
|
public BinanceDataProvider(ILogger<BinanceDataProvider> logger)
|
||||||
|
{
|
||||||
|
_client = new BinanceClient(new BinanceClientOptions
|
||||||
|
{
|
||||||
|
LogLevel = LogLevel.Trace,
|
||||||
|
LogWriters = new List<ILogger> { logger }
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
````
|
||||||
|
|
||||||
|
#### Console application
|
||||||
|
If you don't have a dependency injection service available because you are for example working on a simple console application you use a slightly different approach.
|
||||||
|
|
||||||
|
*Configuring Serilog as ILogger:*
|
||||||
|
````C#
|
||||||
|
var serilogLogger = new LoggerConfiguration()
|
||||||
|
.MinimumLevel.Debug()
|
||||||
|
.WriteTo.Console()
|
||||||
|
.CreateLogger();
|
||||||
|
|
||||||
|
var loggerFactory = (ILoggerFactory)new LoggerFactory();
|
||||||
|
loggerFactory.AddSerilog(serilogLogger);
|
||||||
|
|
||||||
|
````
|
||||||
|
|
||||||
|
*Injecting ILogger:*
|
||||||
|
````C#
|
||||||
|
|
||||||
|
var client = new BinanceClient(new Binance.Net.Objects.BinanceClientOptions
|
||||||
|
{
|
||||||
|
LogLevel = LogLevel.Trace,
|
||||||
|
LogWriters = new List<ILogger> { logger }
|
||||||
|
});
|
||||||
|
````
|
||||||
|
|
||||||
|
The `BinanceClient` will now use the Serilog logger.
|
||||||
|
|
||||||
|
### Log4Net
|
||||||
|
To make the CryptoExchange.Net logging write to the Serilog logger you can use the following ways, depending on the type of project you're using.
|
||||||
|
|
||||||
|
#### ASP.NET Core/Blazor
|
||||||
|
|
||||||
|
With an ASP.Net Core or Blazor project, make sure to install the `Microsoft.Extensions.Logging.Log4Net.AspNetCore` package (https://github.com/huorswords/Microsoft.Extensions.Logging.Log4Net.AspNetCore).
|
||||||
|
Adding `AddLog4Net` 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#
|
||||||
|
public static IHostBuilder CreateHostBuilder(string[] args) =>
|
||||||
|
Host.CreateDefaultBuilder(args)
|
||||||
|
.ConfigureWebHostDefaults(webBuilder =>
|
||||||
|
{
|
||||||
|
webBuilder.ConfigureLogging(logging =>
|
||||||
|
{
|
||||||
|
logging.AddLog4Net();
|
||||||
|
logging.SetMinimumLevel(LogLevel.Trace);
|
||||||
|
});
|
||||||
|
webBuilder.UseStartup<Startup>();
|
||||||
|
});
|
||||||
|
````
|
||||||
|
|
||||||
|
*Injecting ILogger:*
|
||||||
|
````C#
|
||||||
|
|
||||||
|
public class BinanceDataProvider
|
||||||
|
{
|
||||||
|
BinanceClient _client;
|
||||||
|
|
||||||
|
public BinanceDataProvider(ILogger<BinanceDataProvider> logger)
|
||||||
|
{
|
||||||
|
_client = new BinanceClient(new BinanceClientOptions
|
||||||
|
{
|
||||||
|
LogLevel = LogLevel.Trace,
|
||||||
|
LogWriters = new List<ILogger> { logger }
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
````
|
||||||
|
|
||||||
|
#### Console application
|
||||||
|
TODO
|
||||||
+1
@@ -0,0 +1 @@
|
|||||||
|
WIP
|
||||||
Reference in New Issue
Block a user