2020-04-03 01:08:52 +11:00
|
|
|
using System.IO;
|
2020-03-25 20:42:02 +01:00
|
|
|
using System.Reflection;
|
2020-02-24 16:18:47 +01:00
|
|
|
using Microsoft.AspNetCore.Hosting;
|
2020-03-16 14:02:08 +01:00
|
|
|
using Microsoft.Extensions.Configuration;
|
2020-02-18 08:32:06 +01:00
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2020-04-22 14:23:56 +10:00
|
|
|
using Microsoft.Extensions.Logging;
|
2020-08-21 14:52:47 +01:00
|
|
|
using Microsoft.Extensions.Options;
|
2020-04-22 14:23:56 +10:00
|
|
|
using Serilog;
|
|
|
|
|
using Serilog.Extensions.Hosting;
|
2021-02-09 10:22:42 +01:00
|
|
|
using Umbraco.Cms.Core;
|
|
|
|
|
using Umbraco.Cms.Core.Cache;
|
|
|
|
|
using Umbraco.Cms.Core.Composing;
|
|
|
|
|
using Umbraco.Cms.Core.Configuration.Models;
|
|
|
|
|
using Umbraco.Cms.Core.DependencyInjection;
|
|
|
|
|
using Umbraco.Cms.Core.Logging;
|
2020-02-24 16:18:47 +01:00
|
|
|
using Umbraco.Core.Cache;
|
|
|
|
|
using Umbraco.Core.Logging;
|
|
|
|
|
using Umbraco.Core.Logging.Serilog;
|
2020-02-25 08:56:58 +01:00
|
|
|
using Umbraco.Core.Runtime;
|
2020-03-31 12:22:11 +02:00
|
|
|
using Umbraco.Web.Common.AspNetCore;
|
2020-05-12 10:21:40 +10:00
|
|
|
using Umbraco.Web.Common.Profiler;
|
2021-02-09 10:22:42 +01:00
|
|
|
using IHostingEnvironment = Umbraco.Cms.Core.Hosting.IHostingEnvironment;
|
2020-02-18 08:32:06 +01:00
|
|
|
|
2020-05-07 10:08:23 +02:00
|
|
|
namespace Umbraco.Extensions
|
2020-02-18 08:32:06 +01:00
|
|
|
{
|
2020-03-24 14:48:32 +11:00
|
|
|
public static class UmbracoCoreServiceCollectionExtensions
|
2020-02-18 08:32:06 +01:00
|
|
|
{
|
2020-03-24 10:51:53 +01:00
|
|
|
|
2020-04-22 14:23:56 +10:00
|
|
|
/// <summary>
|
|
|
|
|
/// Create and configure the logger
|
|
|
|
|
/// </summary>
|
2020-11-18 17:40:23 +00:00
|
|
|
public static IServiceCollection AddLogger(
|
|
|
|
|
this IServiceCollection services,
|
2020-10-30 13:56:13 +01:00
|
|
|
IHostingEnvironment hostingEnvironment,
|
2020-09-24 09:39:48 +02:00
|
|
|
ILoggingConfiguration loggingConfiguration,
|
2020-10-06 21:23:15 +02:00
|
|
|
IConfiguration configuration)
|
2020-04-22 14:23:56 +10:00
|
|
|
{
|
|
|
|
|
// Create a serilog logger
|
2020-09-17 09:01:10 +02:00
|
|
|
var logger = SerilogLogger.CreateWithDefaultConfiguration(hostingEnvironment, loggingConfiguration, configuration);
|
|
|
|
|
|
|
|
|
|
// This is nessasary to pick up all the loggins to MS ILogger.
|
|
|
|
|
Log.Logger = logger.SerilogLog;
|
|
|
|
|
|
2020-04-22 14:23:56 +10:00
|
|
|
// Wire up all the bits that serilog needs. We need to use our own code since the Serilog ext methods don't cater to our needs since
|
|
|
|
|
// we don't want to use the global serilog `Log` object and we don't have our own ILogger implementation before the HostBuilder runs which
|
2020-04-22 10:29:21 +02:00
|
|
|
// is the only other option that these ext methods allow.
|
2020-04-22 14:23:56 +10:00
|
|
|
// I have created a PR to make this nicer https://github.com/serilog/serilog-extensions-hosting/pull/19 but we'll need to wait for that.
|
|
|
|
|
// Also see : https://github.com/serilog/serilog-extensions-hosting/blob/dev/src/Serilog.Extensions.Hosting/SerilogHostBuilderExtensions.cs
|
|
|
|
|
|
2020-09-17 09:59:13 +02:00
|
|
|
services.AddLogging(configure =>
|
2020-09-16 14:29:33 +02:00
|
|
|
{
|
2020-09-17 09:59:13 +02:00
|
|
|
configure.AddSerilog(logger.SerilogLog, false);
|
2020-09-16 14:29:33 +02:00
|
|
|
});
|
|
|
|
|
|
2020-10-06 21:23:15 +02:00
|
|
|
// This won't (and shouldn't) take ownership of the logger.
|
|
|
|
|
services.AddSingleton(logger.SerilogLog);
|
2020-09-17 09:01:10 +02:00
|
|
|
|
2020-10-06 21:23:15 +02:00
|
|
|
// Registered to provide two services...
|
|
|
|
|
var diagnosticContext = new DiagnosticContext(logger.SerilogLog);
|
2020-04-22 14:23:56 +10:00
|
|
|
|
2020-10-06 21:23:15 +02:00
|
|
|
// Consumed by e.g. middleware
|
|
|
|
|
services.AddSingleton(diagnosticContext);
|
|
|
|
|
|
|
|
|
|
// Consumed by user code
|
|
|
|
|
services.AddSingleton<IDiagnosticContext>(diagnosticContext);
|
2020-11-23 10:27:13 +00:00
|
|
|
services.AddSingleton(loggingConfiguration);
|
2020-11-18 17:40:23 +00:00
|
|
|
|
|
|
|
|
return services;
|
2020-04-22 14:23:56 +10:00
|
|
|
}
|
|
|
|
|
|
2020-11-24 09:22:38 +00:00
|
|
|
internal static ITypeFinder AddTypeFinder(
|
|
|
|
|
this IServiceCollection services,
|
|
|
|
|
ILoggerFactory loggerFactory,
|
|
|
|
|
IWebHostEnvironment webHostEnvironment,
|
|
|
|
|
Assembly entryAssembly,
|
|
|
|
|
IConfiguration config,
|
|
|
|
|
IProfilingLogger profilingLogger)
|
2020-03-25 05:39:25 +01:00
|
|
|
{
|
|
|
|
|
|
2021-02-09 10:22:42 +01:00
|
|
|
var typeFinderSettings = config.GetSection(Constants.Configuration.ConfigTypeFinder).Get<TypeFinderSettings>() ?? new TypeFinderSettings();
|
2020-03-25 05:39:25 +01:00
|
|
|
|
2020-11-20 11:11:52 +00:00
|
|
|
var runtimeHashPaths = new RuntimeHashPaths().AddFolder(new DirectoryInfo(Path.Combine(webHostEnvironment.ContentRootPath, "bin")));
|
2020-11-24 09:22:38 +00:00
|
|
|
var runtimeHash = new RuntimeHash(profilingLogger, runtimeHashPaths);
|
2020-11-20 11:11:52 +00:00
|
|
|
|
2020-11-20 12:24:16 +00:00
|
|
|
var typeFinder = new TypeFinder(
|
2020-11-20 11:11:52 +00:00
|
|
|
loggerFactory.CreateLogger<TypeFinder>(),
|
|
|
|
|
new DefaultUmbracoAssemblyProvider(entryAssembly),
|
|
|
|
|
runtimeHash,
|
|
|
|
|
new TypeFinderConfig(Options.Create(typeFinderSettings))
|
|
|
|
|
);
|
2020-11-20 12:24:16 +00:00
|
|
|
|
|
|
|
|
services.AddUnique<ITypeFinder>(typeFinder);
|
|
|
|
|
|
|
|
|
|
return typeFinder;
|
2020-03-25 05:39:25 +01:00
|
|
|
}
|
2020-05-12 10:21:40 +10:00
|
|
|
|
2020-11-24 09:22:38 +00:00
|
|
|
public static TypeLoader AddTypeLoader(
|
2020-11-20 12:24:16 +00:00
|
|
|
this IServiceCollection services,
|
2020-11-20 11:48:32 +00:00
|
|
|
Assembly entryAssembly,
|
2020-11-20 11:11:52 +00:00
|
|
|
IWebHostEnvironment webHostEnvironment,
|
2020-11-23 10:27:13 +00:00
|
|
|
IHostingEnvironment hostingEnvironment,
|
2020-11-20 11:11:52 +00:00
|
|
|
ILoggerFactory loggerFactory,
|
2020-11-20 11:48:32 +00:00
|
|
|
AppCaches appCaches,
|
2020-11-24 09:22:38 +00:00
|
|
|
IConfiguration configuration,
|
|
|
|
|
IProfiler profiler)
|
2020-03-13 18:44:58 +11:00
|
|
|
{
|
2020-11-24 09:22:38 +00:00
|
|
|
var profilingLogger = new ProfilingLogger(loggerFactory.CreateLogger<ProfilingLogger>(), profiler);
|
|
|
|
|
var typeFinder = services.AddTypeFinder(loggerFactory, webHostEnvironment, entryAssembly, configuration, profilingLogger);
|
2020-03-24 14:48:32 +11:00
|
|
|
|
2020-11-24 09:22:38 +00:00
|
|
|
var typeLoader = new TypeLoader(
|
2020-11-20 11:11:52 +00:00
|
|
|
typeFinder,
|
2020-11-20 11:48:32 +00:00
|
|
|
appCaches.RuntimeCache,
|
2020-11-20 11:11:52 +00:00
|
|
|
new DirectoryInfo(hostingEnvironment.LocalTempPath),
|
|
|
|
|
loggerFactory.CreateLogger<TypeLoader>(),
|
|
|
|
|
profilingLogger
|
|
|
|
|
);
|
2020-11-20 12:24:16 +00:00
|
|
|
|
|
|
|
|
services.AddUnique<TypeLoader>(typeLoader);
|
2020-04-20 06:19:59 +02:00
|
|
|
|
2020-11-20 12:24:16 +00:00
|
|
|
return typeLoader;
|
2020-11-20 11:11:52 +00:00
|
|
|
}
|
2020-02-18 08:32:06 +01:00
|
|
|
}
|
|
|
|
|
}
|