1
0
mirror of https://github.com/bitwarden/server.git synced 2024-12-04 14:13:28 +01:00
bitwarden-server/src/Core/Utilities/LoggerFactoryExtensions.cs

49 lines
1.7 KiB
C#
Raw Normal View History

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)
{
2017-07-14 19:29:52 +02:00
if(!env.IsDevelopment())
{
if(filter == null)
{
filter = (e) => true;
}
2017-08-07 22:31:00 +02:00
var config = new LoggerConfiguration()
.Enrich.FromLogContext()
2017-08-07 22:31:00 +02:00
.Filter.ByIncludingOnly(filter);
if(globalSettings.DocumentDb != null && CoreHelpers.SettingHasValue(globalSettings.DocumentDb.Uri) &&
CoreHelpers.SettingHasValue(globalSettings.DocumentDb.Key))
2017-08-07 22:31:00 +02:00
{
config.WriteTo.AzureDocumentDB(new Uri(globalSettings.DocumentDb.Uri), globalSettings.DocumentDb.Key,
timeToLive: TimeSpan.FromDays(7));
}
else if(CoreHelpers.SettingHasValue(globalSettings.LogDirectory))
2017-08-07 22:31:00 +02:00
{
config.WriteTo.RollingFile($"{globalSettings.LogDirectory}/{globalSettings.ProjectName}/{{Date}}.txt");
2017-08-07 22:31:00 +02:00
}
var serilog = config.CreateLogger();
factory.AddSerilog(serilog);
appLifetime.ApplicationStopped.Register(Log.CloseAndFlush);
}
return factory;
}
}
}