Files
Umbraco-CMS/src/Umbraco.Web.Common/Extensions/UmbracoCoreServiceCollectionExtensions.cs

143 lines
5.6 KiB
C#
Raw Normal View History

using System.IO;
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;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Serilog;
using Serilog.Extensions.Hosting;
2020-03-16 19:14:04 +01:00
using Umbraco.Core;
2020-02-24 16:18:47 +01:00
using Umbraco.Core.Cache;
using Umbraco.Core.Composing;
using Umbraco.Core.Configuration.Models;
2020-02-24 16:18:47 +01:00
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;
using Umbraco.Web.Common.Profiler;
using IHostingEnvironment = Umbraco.Core.Hosting.IHostingEnvironment;
2020-02-18 08:32:06 +01:00
namespace Umbraco.Extensions
2020-02-18 08:32:06 +01:00
{
public static class UmbracoCoreServiceCollectionExtensions
2020-02-18 08:32:06 +01:00
{
2020-11-20 12:24:16 +00:00
/// <summary>
/// Create and configure the logger
/// </summary>
public static IServiceCollection AddLogger(
this IServiceCollection services,
IHostingEnvironment hostingEnvironment,
ILoggingConfiguration loggingConfiguration,
IConfiguration configuration)
{
// Create a serilog logger
var logger = SerilogLogger.CreateWithDefaultConfiguration(hostingEnvironment, loggingConfiguration, configuration);
// This is nessasary to pick up all the loggins to MS ILogger.
Log.Logger = logger.SerilogLog;
// 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
// is the only other option that these ext methods allow.
// 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-17 09:59:13 +02:00
configure.AddSerilog(logger.SerilogLog, false);
});
// This won't (and shouldn't) take ownership of the logger.
services.AddSingleton(logger.SerilogLog);
// Registered to provide two services...
var diagnosticContext = new DiagnosticContext(logger.SerilogLog);
// Consumed by e.g. middleware
services.AddSingleton(diagnosticContext);
// Consumed by user code
services.AddSingleton<IDiagnosticContext>(diagnosticContext);
services.AddSingleton(loggingConfiguration);
return services;
}
2020-11-20 12:24:16 +00:00
internal static IProfiler GetWebProfiler(IConfiguration config)
{
var isDebug = config.GetValue<bool>($"{Constants.Configuration.ConfigHosting}:Debug");
// create and start asap to profile boot
if (!isDebug)
{
// should let it be null, that's how MiniProfiler is meant to work,
// but our own IProfiler expects an instance so let's get one
return new VoidProfiler();
}
var webProfiler = new WebProfiler();
webProfiler.StartBoot();
return webProfiler;
}
2020-11-20 12:24:16 +00:00
internal static ITypeFinder AddTypeFinder(this IServiceCollection services, ILoggerFactory loggerFactory, IWebHostEnvironment webHostEnvironment, Assembly entryAssembly, IConfiguration config)
{
var profiler = GetWebProfiler(config);
var typeFinderSettings = config.GetSection(Core.Constants.Configuration.ConfigTypeFinder).Get<TypeFinderSettings>() ?? new TypeFinderSettings();
var runtimeHashPaths = new RuntimeHashPaths().AddFolder(new DirectoryInfo(Path.Combine(webHostEnvironment.ContentRootPath, "bin")));
var runtimeHash = new RuntimeHash(new ProfilingLogger(loggerFactory.CreateLogger<ProfilingLogger>(), profiler), runtimeHashPaths);
2020-11-20 12:24:16 +00:00
var typeFinder = new TypeFinder(
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-11-20 12:24:16 +00:00
internal static TypeLoader AddTypeLoader(
this IServiceCollection services,
Assembly entryAssembly,
IWebHostEnvironment webHostEnvironment,
IHostingEnvironment hostingEnvironment,
ILoggerFactory loggerFactory,
AppCaches appCaches,
IConfiguration configuration)
{
2020-11-20 12:24:16 +00:00
var typeFinder = services.AddTypeFinder(loggerFactory, webHostEnvironment, entryAssembly, configuration);
var profilingLogger = new ProfilingLogger(loggerFactory.CreateLogger<ProfilingLogger>(), GetWebProfiler(configuration));
2020-11-20 12:24:16 +00:00
var typeLoader = new TypeLoader(
typeFinder,
appCaches.RuntimeCache,
new DirectoryInfo(hostingEnvironment.LocalTempPath),
loggerFactory.CreateLogger<TypeLoader>(),
profilingLogger
);
2020-11-20 12:24:16 +00:00
services.AddUnique<TypeLoader>(typeLoader);
return typeLoader;
}
2020-11-20 12:24:16 +00:00
internal class AspNetCoreBootPermissionsChecker : IUmbracoBootPermissionChecker
{
public void ThrowIfNotPermissions()
{
// nothing to check
}
}
2020-02-18 08:32:06 +01:00
}
2020-02-18 08:32:06 +01:00
}