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

124 lines
4.9 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;
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;
using Umbraco.Web.Common.Profiler;
using IHostingEnvironment = Umbraco.Cms.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-03-24 10:51:53 +01: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-24 09:22:38 +00:00
internal static ITypeFinder AddTypeFinder(
this IServiceCollection services,
ILoggerFactory loggerFactory,
IWebHostEnvironment webHostEnvironment,
Assembly entryAssembly,
IConfiguration config,
IProfilingLogger profilingLogger)
{
var typeFinderSettings = config.GetSection(Constants.Configuration.ConfigTypeFinder).Get<TypeFinderSettings>() ?? new TypeFinderSettings();
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 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-24 09:22:38 +00:00
public static TypeLoader AddTypeLoader(
2020-11-20 12:24:16 +00:00
this IServiceCollection services,
Assembly entryAssembly,
IWebHostEnvironment webHostEnvironment,
IHostingEnvironment hostingEnvironment,
ILoggerFactory loggerFactory,
AppCaches appCaches,
2020-11-24 09:22:38 +00:00
IConfiguration configuration,
IProfiler profiler)
{
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-11-24 09:22:38 +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);
2020-11-20 12:24:16 +00:00
return typeLoader;
}
2020-02-18 08:32:06 +01:00
}
}