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:
39
src/Core/Utilities/LoggerFactoryExtensions.cs
Normal file
39
src/Core/Utilities/LoggerFactoryExtensions.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user