2018-05-22 10:11:50 +02:00
|
|
|
|
using System;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using NLog.Extensions.Logging;
|
|
|
|
|
|
2018-12-15 22:07:29 +01:00
|
|
|
|
namespace Core.Helper
|
|
|
|
|
{
|
|
|
|
|
public static class ServiceHelper
|
|
|
|
|
{
|
2018-05-22 10:11:50 +02:00
|
|
|
|
|
2018-12-15 22:07:29 +01:00
|
|
|
|
public static IServiceProvider BuildLoggerService()
|
|
|
|
|
{
|
2018-05-22 10:11:50 +02:00
|
|
|
|
return ServiceHelper.BuildLoggerService("");
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-15 22:07:29 +01:00
|
|
|
|
public static IServiceProvider BuildLoggerService(string basePath)
|
|
|
|
|
{
|
2018-05-22 10:11:50 +02:00
|
|
|
|
ServiceCollection services = new ServiceCollection();
|
|
|
|
|
|
|
|
|
|
services.AddTransient<LogHelper>();
|
|
|
|
|
|
|
|
|
|
services.AddSingleton<ILoggerFactory, LoggerFactory>();
|
|
|
|
|
services.AddSingleton(typeof(ILogger<>), typeof(Logger<>));
|
|
|
|
|
services.AddLogging((builder) => builder.SetMinimumLevel(LogLevel.Trace));
|
|
|
|
|
|
|
|
|
|
ServiceProvider serviceProvider = services.BuildServiceProvider();
|
|
|
|
|
|
|
|
|
|
ILoggerFactory loggerFactory = serviceProvider.GetRequiredService<ILoggerFactory>();
|
|
|
|
|
|
|
|
|
|
//configure NLog
|
|
|
|
|
loggerFactory.AddNLog(new NLogProviderOptions { CaptureMessageTemplates = true, CaptureMessageProperties = true });
|
|
|
|
|
loggerFactory.ConfigureNLog(basePath + "nlog.config");
|
|
|
|
|
|
|
|
|
|
return serviceProvider;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|