improved logging

This commit is contained in:
Mark van der Wal 2021-04-19 13:14:30 +02:00
parent beaf00a043
commit fefd348930
3 changed files with 32 additions and 10 deletions

View File

@ -6,13 +6,15 @@
<ItemGroup>
<PackageReference Include="Google.Apis" Version="1.44.1" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="3.1.2" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.2" />
<PackageReference Include="Microsoft.Extensions.Http" Version="3.1.2" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="3.1.2" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="3.1.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.2" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="3.1.14" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.14" />
<PackageReference Include="Microsoft.Extensions.Http" Version="3.1.14" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="3.1.14" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="3.1.14" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.14" />
<PackageReference Include="IdentityModel.OidcClient" Version="3.1.2" />
<PackageReference Include="Serilog.AspNetCore" Version="4.1.0" />
<PackageReference Include="Serilog.Sinks.File" Version="4.1.0" />
<PackageReference Include="Winista.MimeDetect" Version="1.0.1" />
</ItemGroup>

View File

@ -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();
}
}
}

View File

@ -14,9 +14,7 @@ namespace FarmmapsVRApoten
protected override void Configure(IServiceCollection serviceCollection)
{
serviceCollection.AddLogging(opts => opts
.AddConsole()
.AddFilter("System.Net.Http", LogLevel.Warning))
serviceCollection.AddLogging()
.AddTransient<PotenService>();
}
}