1
0
mirror of https://github.com/bitwarden/server synced 2025-12-17 00:33:23 +00:00

centralize a lot of service registration

This commit is contained in:
Kyle Spearrin
2017-05-05 20:57:33 -04:00
parent 49bee6935a
commit 3daf0bcd18
17 changed files with 348 additions and 183 deletions

View File

@@ -0,0 +1,39 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Logging;
using Serilog;
using Serilog.Events;
using System;
namespace Bit.Core.Utilities
{
public static class LoggerFactoryExtensions
{
public static ILoggerFactory AddSerilog(
this ILoggerFactory factory,
IHostingEnvironment env,
IApplicationLifetime appLifetime,
GlobalSettings globalSettings,
Func<LogEvent, bool> filter = null)
{
if(env.IsProduction())
{
if(filter == null)
{
filter = (e) => true;
}
var serilog = new LoggerConfiguration()
.Enrich.FromLogContext()
.Filter.ByIncludingOnly(filter)
.WriteTo.AzureDocumentDB(new Uri(globalSettings.DocumentDb.Uri), globalSettings.DocumentDb.Key,
timeToLive: TimeSpan.FromDays(7))
.CreateLogger();
factory.AddSerilog(serilog);
appLifetime.ApplicationStopped.Register(Log.CloseAndFlush);
}
return factory;
}
}
}