diff --git a/Logging.md b/Logging.md index 6d3f665..b101c5e 100644 --- a/Logging.md +++ b/Logging.md @@ -1 +1,126 @@ -WIP \ No newline at end of file +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(); + }); + +```` + + +*Injecting ILogger:* +````C# + +public class BinanceDataProvider +{ + BinanceClient _client; + + public BinanceDataProvider(ILogger logger) + { + _client = new BinanceClient(new BinanceClientOptions + { + LogLevel = LogLevel.Trace, + LogWriters = new List { 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 { 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(); + }); +```` + +*Injecting ILogger:* +````C# + +public class BinanceDataProvider +{ + BinanceClient _client; + + public BinanceDataProvider(ILogger logger) + { + _client = new BinanceClient(new BinanceClientOptions + { + LogLevel = LogLevel.Trace, + LogWriters = new List { logger } + }); + + } +} + +```` + +#### Console application +TODO \ No newline at end of file diff --git a/Options.md b/Options.md new file mode 100644 index 0000000..6d3f665 --- /dev/null +++ b/Options.md @@ -0,0 +1 @@ +WIP \ No newline at end of file