2017-05-06 02:57:33 +02:00
|
|
|
|
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())
|
2017-05-06 02:57:33 +02:00
|
|
|
|
{
|
|
|
|
|
if(filter == null)
|
|
|
|
|
{
|
|
|
|
|
filter = (e) => true;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-07 22:31:00 +02:00
|
|
|
|
var config = new LoggerConfiguration()
|
2017-05-06 02:57:33 +02:00
|
|
|
|
.Enrich.FromLogContext()
|
2017-08-07 22:31:00 +02:00
|
|
|
|
.Filter.ByIncludingOnly(filter);
|
2017-05-06 02:57:33 +02:00
|
|
|
|
|
2017-08-09 05:06:28 +02:00
|
|
|
|
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));
|
|
|
|
|
}
|
2017-08-09 05:06:28 +02:00
|
|
|
|
else if(CoreHelpers.SettingHasValue(globalSettings.LogDirectory))
|
2017-08-07 22:31:00 +02:00
|
|
|
|
{
|
2017-08-09 05:06:28 +02:00
|
|
|
|
config.WriteTo.RollingFile($"{globalSettings.LogDirectory}/{globalSettings.ProjectName}/{{Date}}.txt");
|
2017-08-07 22:31:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var serilog = config.CreateLogger();
|
2017-05-06 02:57:33 +02:00
|
|
|
|
factory.AddSerilog(serilog);
|
|
|
|
|
appLifetime.ApplicationStopped.Register(Log.CloseAndFlush);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return factory;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|