2018-03-23 18:33:31 +01:00
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
2019-07-23 16:38:49 -04:00
|
|
|
|
using Microsoft.Extensions.Configuration;
|
2020-01-10 08:33:13 -05:00
|
|
|
|
using Microsoft.Extensions.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
|
|
|
|
|
|
{
|
2019-07-23 16:38:49 -04:00
|
|
|
|
public static void UseSerilog(
|
|
|
|
|
|
this IApplicationBuilder appBuilder,
|
2020-01-10 08:33:13 -05:00
|
|
|
|
IWebHostEnvironment env,
|
|
|
|
|
|
IHostApplicationLifetime applicationLifetime,
|
2019-07-23 16:38:49 -04:00
|
|
|
|
GlobalSettings globalSettings)
|
2017-05-05 20:57:33 -04:00
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (env.IsDevelopment())
|
2017-05-05 20:57:33 -04:00
|
|
|
|
{
|
2019-11-22 10:33:57 -05:00
|
|
|
|
return;
|
2019-07-23 16:38:49 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
applicationLifetime.ApplicationStopped.Register(Log.CloseAndFlush);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static ILoggingBuilder AddSerilog(
|
|
|
|
|
|
this ILoggingBuilder builder,
|
|
|
|
|
|
WebHostBuilderContext context,
|
|
|
|
|
|
Func<LogEvent, bool> filter = null)
|
|
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (context.HostingEnvironment.IsDevelopment())
|
2019-07-23 16:38:49 -04:00
|
|
|
|
{
|
2019-11-22 10:33:57 -05:00
|
|
|
|
return builder;
|
2018-03-27 22:16:55 -04:00
|
|
|
|
}
|
2017-05-05 20:57:33 -04:00
|
|
|
|
|
2018-08-15 10:54:15 -04:00
|
|
|
|
bool inclusionPredicate(LogEvent e)
|
2018-03-27 22:16:55 -04:00
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (filter == null)
|
2018-08-15 10:54:15 -04:00
|
|
|
|
{
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
var eventId = e.Properties.ContainsKey("EventId") ? e.Properties["EventId"].ToString() : null;
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (eventId?.Contains(Constants.BypassFiltersEventId.ToString()) ?? false)
|
2018-08-15 10:54:15 -04:00
|
|
|
|
{
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
return filter(e);
|
2018-03-27 22:16:55 -04:00
|
|
|
|
}
|
2017-05-05 20:57:33 -04:00
|
|
|
|
|
2019-07-23 16:38:49 -04:00
|
|
|
|
var globalSettings = new GlobalSettings();
|
|
|
|
|
|
ConfigurationBinder.Bind(context.Configuration.GetSection("GlobalSettings"), globalSettings);
|
|
|
|
|
|
|
2018-03-27 22:16:55 -04:00
|
|
|
|
var config = new LoggerConfiguration()
|
|
|
|
|
|
.Enrich.FromLogContext()
|
2018-08-15 10:54:15 -04:00
|
|
|
|
.Filter.ByIncludingOnly(inclusionPredicate);
|
2018-03-23 18:33:31 +01:00
|
|
|
|
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (CoreHelpers.SettingHasValue(globalSettings?.DocumentDb.Uri) &&
|
2018-03-27 22:16:55 -04:00
|
|
|
|
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
|
|
|
|
}
|
2020-03-27 14:36:37 -04:00
|
|
|
|
else if (CoreHelpers.SettingHasValue(globalSettings?.Sentry.Dsn))
|
2018-03-27 22:16:55 -04:00
|
|
|
|
{
|
|
|
|
|
|
config.WriteTo.Sentry(globalSettings.Sentry.Dsn)
|
|
|
|
|
|
.Enrich.FromLogContext()
|
2020-03-16 13:25:46 +01:00
|
|
|
|
.Enrich.WithProperty("Project", globalSettings.ProjectName);
|
2017-05-05 20:57:33 -04:00
|
|
|
|
}
|
2020-03-27 14:36:37 -04:00
|
|
|
|
else if (CoreHelpers.SettingHasValue(globalSettings.LogDirectory))
|
2018-03-27 22:16:55 -04:00
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (globalSettings.LogRollBySizeLimit.HasValue)
|
2019-11-22 10:33:57 -05:00
|
|
|
|
{
|
2019-11-25 11:40:04 -05:00
|
|
|
|
config.WriteTo.File($"{globalSettings.LogDirectory}/{globalSettings.ProjectName}/log.txt",
|
|
|
|
|
|
rollOnFileSizeLimit: true, fileSizeLimitBytes: globalSettings.LogRollBySizeLimit);
|
2019-11-22 10:33:57 -05:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
config.WriteTo
|
|
|
|
|
|
.RollingFile($"{globalSettings.LogDirectory}/{globalSettings.ProjectName}/{{Date}}.txt");
|
|
|
|
|
|
}
|
|
|
|
|
|
config
|
2018-03-30 00:01:53 -04:00
|
|
|
|
.Enrich.FromLogContext()
|
|
|
|
|
|
.Enrich.WithProperty("Project", globalSettings.ProjectName);
|
2018-03-27 22:16:55 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var serilog = config.CreateLogger();
|
2019-07-23 16:38:49 -04:00
|
|
|
|
builder.AddSerilog(serilog);
|
2017-05-05 20:57:33 -04:00
|
|
|
|
|
2019-07-23 16:38:49 -04:00
|
|
|
|
return builder;
|
2017-05-05 20:57:33 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|