AB4227 - Moved Logging

This commit is contained in:
Bjarke Berg
2019-12-18 12:27:14 +01:00
parent 897cb63ad6
commit 38519b2bab
38 changed files with 63 additions and 54 deletions

View File

@@ -129,19 +129,15 @@
<Compile Include="Compose\AuditEventsComponent.cs" />
<Compile Include="Compose\AuditEventsComposer.cs" />
<Compile Include="Composing\RegisterFactory.cs" />
<Compile Include="CompositionExtensions.cs" />
<Compile Include="Composing\CompositionExtensions\Configuration.cs" />
<Compile Include="Composing\CompositionExtensions\CoreMappingProfiles.cs" />
<Compile Include="Composing\CompositionExtensions\FileSystems.cs" />
<Compile Include="Composing\CompositionExtensions\Repositories.cs" />
<Compile Include="Composing\CompositionExtensions\Services.cs" />
<Compile Include="CompositionExtensions_Essentials.cs" />
<Compile Include="CompositionExtensions_FileSystems.cs" />
<Compile Include="Deploy\IGridCellValueConnector.cs" />
<Compile Include="Composing\Current.cs" />
<Compile Include="Composing\LightInject\LightInjectContainer.cs" />
<Compile Include="Composing\LightInject\MixedLightInjectScopeManagerProvider.cs" />
<Compile Include="Logging\Viewer\LogTimePeriod.cs" />
<Compile Include="Models\Identity\BackOfficeIdentityUser.cs" />
<Compile Include="Models\Identity\IdentityMapDefinition.cs" />
<Compile Include="Models\Identity\IdentityUser.cs" />
@@ -155,35 +151,11 @@
<Compile Include="Security\UmbracoBackOfficeIdentity.cs" />
<Compile Include="StringExtensions.cs" />
<Compile Include="TypeLoaderExtensions.cs" />
<Compile Include="Logging\Viewer\CountingFilter.cs" />
<Compile Include="Logging\Viewer\ErrorCounterFilter.cs" />
<Compile Include="Logging\Viewer\ExpressionFilter.cs" />
<Compile Include="Logging\Viewer\ILogFilter.cs" />
<Compile Include="Logging\Viewer\LogTemplate.cs" />
<Compile Include="Logging\Viewer\ILogViewer.cs" />
<Compile Include="Logging\Viewer\LogLevelCounts.cs" />
<Compile Include="Logging\Viewer\JsonLogViewer.cs" />
<Compile Include="Logging\Viewer\LogMessage.cs" />
<Compile Include="Logging\Viewer\LogViewerComposer.cs" />
<Compile Include="Logging\Viewer\LogViewerSourceBase.cs" />
<Compile Include="Logging\LogHttpRequest.cs" />
<Compile Include="Logging\MessageTemplates.cs" />
<Compile Include="Logging\Serilog\Enrichers\HttpRequestIdEnricher.cs" />
<Compile Include="Logging\Serilog\Enrichers\HttpRequestNumberEnricher.cs" />
<Compile Include="Logging\Serilog\Enrichers\HttpSessionIdEnricher.cs" />
<Compile Include="Logging\Serilog\LoggerConfigExtensions.cs" />
<Compile Include="Logging\Serilog\Enrichers\Log4NetLevelMapperEnricher.cs" />
<Compile Include="Logging\Viewer\MessageTemplateFilter.cs" />
<Compile Include="Logging\Viewer\SavedLogSearch.cs" />
<Compile Include="RuntimeOptions.cs" />
<Compile Include="Runtime\CoreRuntime.cs" />
<Compile Include="Runtime\CoreInitialComponent.cs" />
<Compile Include="Diagnostics\MiniDump.cs" />
<Compile Include="Composing\LightInject\LightInjectException.cs" />
<Compile Include="FileResources\Files.Designer.cs" />
<Compile Include="Logging\Serilog\SerilogLogger.cs" />
<Compile Include="Logging\OwinLogger.cs" />
<Compile Include="Logging\OwinLoggerFactory.cs" />
<Compile Include="MainDom.cs" />
<Compile Include="ContentExtensions.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
@@ -222,8 +194,5 @@
<Name>Umbraco.Infrastructure</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Folder Include="Sync" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

View File

@@ -0,0 +1,12 @@
using System;
namespace Umbraco.Core.Diagnostics
{
/// <summary>
/// Provides a collection of methods for allocating unmanaged memory, copying unmanaged memory blocks, and converting managed to unmanaged types, as well as other miscellaneous methods used when interacting with unmanaged code.
/// </summary>
public interface IMarchal
{
IntPtr GetExceptionPointers();
}
}

View File

@@ -78,7 +78,7 @@ namespace Umbraco.Core.Diagnostics
[DllImport("kernel32.dll", EntryPoint = "GetCurrentThreadId", ExactSpelling = true)]
private static extern uint GetCurrentThreadId();
private static bool Write(SafeHandle fileHandle, Option options, bool withException = false)
private static bool Write(IMarchal marchal, SafeHandle fileHandle, Option options, bool withException = false)
{
var currentProcess = Process.GetCurrentProcess();
var currentProcessHandle = currentProcess.Handle;
@@ -91,7 +91,7 @@ namespace Umbraco.Core.Diagnostics
exp.ExceptionPointers = IntPtr.Zero;
if (withException)
exp.ExceptionPointers = Marshal.GetExceptionPointers();
exp.ExceptionPointers = marchal.GetExceptionPointers();
var bRet = exp.ExceptionPointers == IntPtr.Zero
? MiniDumpWriteDump(currentProcessHandle, currentProcessId, fileHandle, (uint) options, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero)
@@ -100,7 +100,7 @@ namespace Umbraco.Core.Diagnostics
return bRet;
}
public static bool Dump(Option options = Option.WithFullMemory, bool withException = false)
public static bool Dump(IMarchal marchal, IIOHelper ioHelper, Option options = Option.WithFullMemory, bool withException = false)
{
lock (LockO)
{
@@ -110,8 +110,6 @@ namespace Umbraco.Core.Diagnostics
// filter everywhere in our code = not!
var stacktrace = withException ? Environment.StackTrace : string.Empty;
var ioHelper = Current.Factory.GetInstance<IIOHelper>();
var filepath = ioHelper.MapPath("~/App_Data/MiniDump");
if (Directory.Exists(filepath) == false)
Directory.CreateDirectory(filepath);
@@ -119,16 +117,15 @@ namespace Umbraco.Core.Diagnostics
var filename = Path.Combine(filepath, $"{DateTime.UtcNow:yyyyMMddTHHmmss}.{Guid.NewGuid().ToString("N").Substring(0, 4)}.dmp");
using (var stream = new FileStream(filename, FileMode.Create, FileAccess.ReadWrite, FileShare.Write))
{
return Write(stream.SafeFileHandle, options, withException);
return Write(marchal, stream.SafeFileHandle, options, withException);
}
}
}
public static bool OkToDump()
public static bool OkToDump(IIOHelper ioHelper)
{
lock (LockO)
{
var ioHelper = Current.Factory.GetInstance<IIOHelper>();
var filepath = ioHelper.MapPath("~/App_Data/MiniDump");
if (Directory.Exists(filepath) == false) return true;
var count = Directory.GetFiles(filepath, "*.dmp").Length;

View File

@@ -1,10 +1,10 @@
using System;
using Microsoft.Owin.Logging;
using Umbraco.Core.Composing;
using Umbraco.Composing;
namespace Umbraco.Core.Logging
{
internal class OwinLoggerFactory : ILoggerFactory
public class OwinLoggerFactory : ILoggerFactory
{
/// <summary>
/// Creates a new ILogger instance of the given name.

View File

@@ -5,9 +5,10 @@ using System.Threading;
using Serilog;
using Serilog.Events;
using Umbraco.Core.Cache;
using Umbraco.Core.Composing;
using Umbraco.Core.Configuration;
using Umbraco.Core.Diagnostics;
using Umbraco.Core.Hosting;
using Umbraco.Core.IO;
using Umbraco.Net;
namespace Umbraco.Core.Logging.Serilog
@@ -17,19 +18,31 @@ namespace Umbraco.Core.Logging.Serilog
///</summary>
public class SerilogLogger : ILogger, IDisposable
{
private readonly ICoreDebug _coreDebug;
private readonly IIOHelper _ioHelper;
private readonly IMarchal _marchal;
/// <summary>
/// Initialize a new instance of the <see cref="SerilogLogger"/> class with a configuration file.
/// </summary>
/// <param name="logConfigFile"></param>
public SerilogLogger(FileInfo logConfigFile)
public SerilogLogger(ICoreDebug coreDebug, IIOHelper ioHelper, IMarchal marchal, FileInfo logConfigFile)
{
_coreDebug = coreDebug;
_ioHelper = ioHelper;
_marchal = marchal;
Log.Logger = new LoggerConfiguration()
.ReadFrom.AppSettings(filePath: AppDomain.CurrentDomain.BaseDirectory + logConfigFile)
.CreateLogger();
}
public SerilogLogger(LoggerConfiguration logConfig)
public SerilogLogger(ICoreDebug coreDebug, IIOHelper ioHelper, IMarchal marchal, LoggerConfiguration logConfig)
{
_coreDebug = coreDebug;
_ioHelper = ioHelper;
_marchal = marchal;
//Configure Serilog static global logger with config passed in
Log.Logger = logConfig.CreateLogger();
}
@@ -38,7 +51,7 @@ namespace Umbraco.Core.Logging.Serilog
/// Creates a logger with some pre-defined configuration and remainder from config file
/// </summary>
/// <remarks>Used by UmbracoApplicationBase to get its logger.</remarks>
public static SerilogLogger CreateWithDefaultConfiguration(IHostingEnvironment hostingEnvironment, ISessionIdResolver sessionIdResolver, Func<IRequestCache> requestCacheGetter)
public static SerilogLogger CreateWithDefaultConfiguration(IHostingEnvironment hostingEnvironment, ISessionIdResolver sessionIdResolver, Func<IRequestCache> requestCacheGetter, ICoreDebug coreDebug, IIOHelper ioHelper, IMarchal marchal)
{
var loggerConfig = new LoggerConfiguration();
loggerConfig
@@ -46,7 +59,7 @@ namespace Umbraco.Core.Logging.Serilog
.ReadFromConfigFile()
.ReadFromUserConfigFile();
return new SerilogLogger(loggerConfig);
return new SerilogLogger(coreDebug, ioHelper, marchal, loggerConfig);
}
/// <summary>
@@ -157,7 +170,7 @@ namespace Umbraco.Core.Logging.Serilog
logger.Error(exception, messageTemplate, propertyValues);
}
private static void DumpThreadAborts(global::Serilog.ILogger logger, LogEventLevel level, Exception exception, ref string messageTemplate)
private void DumpThreadAborts(global::Serilog.ILogger logger, LogEventLevel level, Exception exception, ref string messageTemplate)
{
var dump = false;
@@ -166,17 +179,17 @@ namespace Umbraco.Core.Logging.Serilog
messageTemplate += "\r\nThe thread has been aborted, because the request has timed out.";
// dump if configured, or if stacktrace contains Monitor.ReliableEnter
dump = Current.Configs.CoreDebug().DumpOnTimeoutThreadAbort || IsMonitorEnterThreadAbortException(exception);
dump = _coreDebug.DumpOnTimeoutThreadAbort || IsMonitorEnterThreadAbortException(exception);
// dump if it is ok to dump (might have a cap on number of dump...)
dump &= MiniDump.OkToDump();
dump &= MiniDump.OkToDump(_ioHelper);
}
if (dump)
{
try
{
var dumped = MiniDump.Dump(withException: true);
var dumped = MiniDump.Dump(_marchal, _ioHelper, withException: true);
messageTemplate += dumped
? "\r\nA minidump was created in App_Data/MiniDump"
: "\r\nFailed to create a minidump";

View File

@@ -9,6 +9,7 @@
<PackageReference Include="LightInject" Version="6.2.0" />
<PackageReference Include="LightInject.Annotation" Version="1.1.0" />
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="Microsoft.Owin" Version="4.0.1" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0-beta2-19554-01" />
<PackageReference Include="MiniProfiler.Shared" Version="4.1.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
@@ -19,6 +20,7 @@
<PackageReference Include="Serilog.Filters.Expressions" Version="2.0.0" />
<PackageReference Include="Serilog.Formatting.Compact" Version="1.1.0" />
<PackageReference Include="Serilog.Formatting.Compact.Reader" Version="1.0.3" />
<PackageReference Include="Serilog.Settings.AppSettings" Version="2.2.2" />
<PackageReference Include="Serilog.Sinks.Async" Version="1.4.0" />
<PackageReference Include="Serilog.Sinks.File" Version="4.1.0" />
<PackageReference Include="Serilog.Sinks.Map" Version="1.0.0" />

View File

@@ -13,6 +13,7 @@ using Umbraco.Core.Cache;
using Umbraco.Core.Composing;
using Umbraco.Core.Configuration;
using Umbraco.Core.Configuration.UmbracoSettings;
using Umbraco.Core.Diagnostics;
using Umbraco.Core.Hosting;
using Umbraco.Core.IO;
using Umbraco.Core.Logging;
@@ -87,6 +88,8 @@ namespace Umbraco.Tests.TestHelpers
public static IShortStringHelper ShortStringHelper => new DefaultShortStringHelper(new DefaultShortStringHelperConfig());
public static IDbProviderFactoryCreator DbProviderFactoryCreator => new UmbracoDbProviderFactoryCreator(Constants.DbProviderNames.SqlCe);
public static IBulkSqlInsertProvider BulkSqlInsertProvider => new SqlCeBulkSqlInsertProvider();
public static IMarchal Marchal => new FrameworkMarchal();
public static ICoreDebug CoreDebug => new CoreDebug();
public static IIOHelper IOHelper = new IOHelper(GetHostingEnvironment());

View File

@@ -158,7 +158,6 @@ namespace Umbraco.Tests.TestHelpers
{
return SettingsForTests.GetDefaultGlobalSettings();
}
public IFileSystems GetFileSystemsMock()
{
var fileSystems = Mock.Of<IFileSystems>();

View File

@@ -241,7 +241,7 @@ namespace Umbraco.Tests.Testing
profiler = Mock.Of<IProfiler>();
break;
case UmbracoTestOptions.Logger.Serilog:
logger = new SerilogLogger(new FileInfo(TestHelper.MapPathForTest("~/unit-test.config")));
logger = new SerilogLogger(TestHelper.CoreDebug, IOHelper, TestHelper.Marchal, new FileInfo(TestHelper.MapPathForTest("~/unit-test.config")));
profiler = new LogProfiler(logger);
break;
case UmbracoTestOptions.Logger.Console:

View File

@@ -17,7 +17,7 @@ namespace Umbraco.Tests.UmbracoExamine
[OneTimeSetUp]
public void InitializeFixture()
{
var logger = new SerilogLogger(new FileInfo(TestHelper.MapPathForTest("~/unit-test.config")));
var logger = new SerilogLogger(TestHelper.CoreDebug, IOHelper, TestHelper.Marchal, new FileInfo(TestHelper.MapPathForTest("~/unit-test.config")));
_profilingLogger = new ProfilingLogger(logger, new LogProfiler(logger));
}

View File

@@ -0,0 +1,13 @@
using System;
using System.Runtime.InteropServices;
using Umbraco.Core.Diagnostics;
namespace Umbraco.Web
{
public class FrameworkMarchal : IMarchal
{
// This thing is not available in net standard, but exists in both .Net 4 and .Net Core 3
public IntPtr GetExceptionPointers() => Marshal.GetExceptionPointers();
}
}

View File

@@ -170,6 +170,7 @@
<Compile Include="Editors\MacrosController.cs" />
<Compile Include="Editors\RelationTypeController.cs" />
<Compile Include="Editors\TinyMceController.cs" />
<Compile Include="FrameworkMarchal.cs" />
<Compile Include="Hosting\AspNetHostingEnvironment.cs" />
<Compile Include="HttpContextExtensions.cs" />
<Compile Include="IUmbracoContextFactory.cs" />

View File

@@ -46,7 +46,7 @@ namespace Umbraco.Web
_profiler = new LogProfiler(_logger);
_logger = SerilogLogger.CreateWithDefaultConfiguration(_hostingEnvironment, new AspNetSessionIdResolver(), () => _factory?.GetInstance<IRequestCache>());
_logger = SerilogLogger.CreateWithDefaultConfiguration(_hostingEnvironment, new AspNetSessionIdResolver(), () => _factory?.GetInstance<IRequestCache>(), _configs.CoreDebug(), _ioHelper, new FrameworkMarchal());
_backOfficeInfo = new AspNetBackOfficeInfo(_configs.Global(), _ioHelper, _configs.Settings(), _logger);
Umbraco.Composing.Current.Logger = _logger;