Skip to content

VkNet.NLog.Extensions.Logging

atckun edited this page Mar 13, 2019 · 3 revisions

Конфигурация NLog

public static void Main(string[] args)
        {
            // Контейнер для инверсии зависимостей
            var services = new ServiceCollection();
            
            // Регистрация логгера
            services.AddSingleton<ILoggerFactory, LoggerFactory>();
            services.AddSingleton(typeof(ILogger<>), typeof(Logger<>));
            services.AddLogging(builder =>
            {
                builder.ClearProviders();
                builder.SetMinimumLevel(LogLevel.Trace);
                builder.AddNLog(new NLogProviderOptions
                {
                    CaptureMessageProperties = true,
                    CaptureMessageTemplates = true
                });
            });
            NLog.LogManager.LoadConfiguration("nlog.config");
            var api = new VkApi(services);
            api.Authorization(...);
            Console.WriteLine(api.Token);

            Console.ReadLine();
            
            // Ensure to flush and stop internal timers/threads before application-exit (Avoid segmentation fault on Linux)
            NLog.LogManager.Shutdown();
        }

Файл конфигурации

Добавьте в проект nlog.config (File Properties: Copy Always).

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <targets>
        <target name="logfile" xsi:type="File" fileName="file.txt" layout="${longdate} ${callsite} ${level} ${message}" />
        <target name="logconsole" xsi:type="Console" layout="${longdate} ${callsite} ${level} ${message}" />
    </targets>

    <rules>
    <logger name="*" minlevel="Trace" writeTo="logconsole" />
        <logger name="*" minlevel="Trace" writeTo="logfile" />
    </rules>
</nlog>