forked from FarmMaps/FarmMapsApiClient
improved logging
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
using System;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using FarmmapsApi.HttpMessageHandlers;
|
||||
@@ -6,6 +7,10 @@ using FarmmapsApi.Services;
|
||||
using IdentityModel.Client;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Serilog;
|
||||
using Serilog.Events;
|
||||
using Serilog.Filters;
|
||||
|
||||
namespace FarmmapsApi
|
||||
{
|
||||
@@ -37,10 +42,12 @@ namespace FarmmapsApi
|
||||
.AddHttpMessageHandler<FarmmapsAuthenticationHandler>()
|
||||
.Services;
|
||||
|
||||
|
||||
Configure(serviceCollection);
|
||||
|
||||
var serviceProvider = serviceCollection.BuildServiceProvider();
|
||||
|
||||
ConfigureLogger(serviceProvider);
|
||||
|
||||
await serviceProvider.GetService<FarmmapsApiService>().AuthenticateAsync();
|
||||
// await serviceProvider.GetService<FarmmapsEventHub>().StartEventHub();
|
||||
|
||||
@@ -48,5 +55,20 @@ namespace FarmmapsApi
|
||||
}
|
||||
|
||||
protected abstract void Configure(IServiceCollection serviceCollection);
|
||||
|
||||
private static void ConfigureLogger(IServiceProvider serviceProvider)
|
||||
{
|
||||
var isService = Matching.FromSource("Microsoft");
|
||||
|
||||
Log.Logger = new LoggerConfiguration()
|
||||
.MinimumLevel.Information()
|
||||
.WriteTo.Logger(l => l.Filter.ByIncludingOnly(isService)
|
||||
.WriteTo.Console(LogEventLevel.Warning))
|
||||
.WriteTo.File(path: "Logs/log.log", rollingInterval: RollingInterval.Day)
|
||||
.CreateLogger();
|
||||
|
||||
var loggerFactory = serviceProvider.GetRequiredService<ILoggerFactory>();
|
||||
loggerFactory.AddSerilog();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user