2018-03-23 18:33:31 +01:00
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
2017-05-05 20:57:33 -04:00
|
|
|
|
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,
|
2018-03-23 18:33:31 +01:00
|
|
|
|
IApplicationBuilder appBuilder,
|
2017-05-05 20:57:33 -04:00
|
|
|
|
IHostingEnvironment env,
|
|
|
|
|
|
IApplicationLifetime appLifetime,
|
|
|
|
|
|
GlobalSettings globalSettings,
|
|
|
|
|
|
Func<LogEvent, bool> filter = null)
|
|
|
|
|
|
{
|
2018-03-27 22:16:55 -04:00
|
|
|
|
if(env.IsDevelopment())
|
2017-05-05 20:57:33 -04:00
|
|
|
|
{
|
2018-03-27 22:16:55 -04:00
|
|
|
|
return factory;
|
|
|
|
|
|
}
|
2017-05-05 20:57:33 -04:00
|
|
|
|
|
2018-03-27 22:16:55 -04:00
|
|
|
|
if(filter == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
filter = (e) => true;
|
|
|
|
|
|
}
|
2017-05-05 20:57:33 -04:00
|
|
|
|
|
2018-03-27 22:16:55 -04:00
|
|
|
|
var config = new LoggerConfiguration()
|
|
|
|
|
|
.Enrich.FromLogContext()
|
|
|
|
|
|
.Filter.ByIncludingOnly(filter);
|
2018-03-23 18:33:31 +01:00
|
|
|
|
|
2018-03-27 22:16:55 -04:00
|
|
|
|
if(CoreHelpers.SettingHasValue(globalSettings?.DocumentDb.Uri) &&
|
|
|
|
|
|
CoreHelpers.SettingHasValue(globalSettings?.DocumentDb.Key))
|
|
|
|
|
|
{
|
|
|
|
|
|
config.WriteTo.AzureDocumentDB(new Uri(globalSettings.DocumentDb.Uri),
|
2018-03-30 00:01:53 -04:00
|
|
|
|
globalSettings.DocumentDb.Key, timeToLive: TimeSpan.FromDays(7))
|
|
|
|
|
|
.Enrich.FromLogContext()
|
|
|
|
|
|
.Enrich.WithProperty("Project", globalSettings.ProjectName);
|
2018-03-27 22:16:55 -04:00
|
|
|
|
}
|
|
|
|
|
|
else if(CoreHelpers.SettingHasValue(globalSettings?.Sentry.Dsn))
|
|
|
|
|
|
{
|
|
|
|
|
|
config.WriteTo.Sentry(globalSettings.Sentry.Dsn)
|
|
|
|
|
|
.Enrich.FromLogContext()
|
|
|
|
|
|
.Enrich.WithProperty("Project", globalSettings.ProjectName)
|
|
|
|
|
|
.Destructure.With<HttpContextDestructingPolicy>()
|
|
|
|
|
|
.Filter.ByExcluding(e => e.Exception?.CheckIfCaptured() == true);
|
2017-08-07 16:31:00 -04:00
|
|
|
|
|
2018-03-27 22:16:55 -04:00
|
|
|
|
appBuilder.AddSentryContext();
|
2017-05-05 20:57:33 -04:00
|
|
|
|
}
|
2018-03-27 22:16:55 -04:00
|
|
|
|
else if(CoreHelpers.SettingHasValue(globalSettings.LogDirectory))
|
|
|
|
|
|
{
|
2018-03-30 00:01:53 -04:00
|
|
|
|
config.WriteTo.RollingFile($"{globalSettings.LogDirectory}/{globalSettings.ProjectName}/{{Date}}.txt")
|
|
|
|
|
|
.Enrich.FromLogContext()
|
|
|
|
|
|
.Enrich.WithProperty("Project", globalSettings.ProjectName);
|
2018-03-27 22:16:55 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var serilog = config.CreateLogger();
|
|
|
|
|
|
factory.AddSerilog(serilog);
|
|
|
|
|
|
appLifetime.ApplicationStopped.Register(Log.CloseAndFlush);
|
2017-05-05 20:57:33 -04:00
|
|
|
|
|
|
|
|
|
|
return factory;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|