4 Logging
inyutin-maxim edited this page 2020-05-23 16:06:27 +03:00
This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

Для настройки логгирования вы можете использовать все совместимые с Microsoft.Extensions.Logging, пакеты:

Пример конфигурирования с использованием пакета https://www.nuget.org/packages/VkNet.NLog.Extensions.Logging/ смотри здесь

Пример Serilog

Установите пакеты:

  • Serilog
  • Serilog.Extensions.Logging
  • Serilog.Sinks.Console
  • Serilog.Sinks.File
  • VkNet
static void Main(string[] args)
{
	Log.Logger = new LoggerConfiguration()
		.MinimumLevel
		.Debug()
		.WriteTo
		.Console()
		.WriteTo
		.File("log.txt",
			rollingInterval: RollingInterval.Day,
			rollOnFileSizeLimit: true)
		.CreateLogger();
	// Контейнер для инверсии зависимостей
	var services = new ServiceCollection();

	// Регистрация логгера
	services.AddLogging(builder =>
	{
		builder.ClearProviders();
		builder.SetMinimumLevel(LogLevel.Trace);
		builder.AddSerilog(dispose: true);
	});
	var api = new VkApi(services);
	api.Authorize(...);
	Console.WriteLine(api.Token);

	Console.ReadLine();
}