diff --git a/src/Umbraco.Core/Logging/DebugDiagnosticsLogger.cs b/src/Umbraco.Core/Logging/DebugDiagnosticsLogger.cs index b50c23d228..a8ca9b6c31 100644 --- a/src/Umbraco.Core/Logging/DebugDiagnosticsLogger.cs +++ b/src/Umbraco.Core/Logging/DebugDiagnosticsLogger.cs @@ -7,6 +7,36 @@ namespace Umbraco.Core.Logging /// public class DebugDiagnosticsLogger : ILogger { + /// + public void Fatal(Type reporting, Exception exception, string message) + { + System.Diagnostics.Debug.WriteLine(message + Environment.NewLine + exception, reporting.FullName); + } + + /// + public void Fatal(Type reporting, Exception exception) + { + System.Diagnostics.Debug.WriteLine(Environment.NewLine + exception, reporting.FullName); + } + + /// + public void Fatal(Type reporting, string message) + { + System.Diagnostics.Debug.WriteLine(message); + } + + /// + public void Fatal(Type reporting, Exception exception, string messageTemplate, params object[] args) + { + System.Diagnostics.Debug.WriteLine(string.Format(messageTemplate, args) + Environment.NewLine + exception, reporting.FullName); + } + + /// + public void Fatal(Type reporting, string messageTemplate, params object[] args) + { + System.Diagnostics.Debug.WriteLine(messageTemplate, args); + } + /// public void Error(Type reporting, Exception exception, string message) { @@ -96,15 +126,6 @@ namespace Umbraco.Core.Logging { System.Diagnostics.Debug.WriteLine(string.Format(format, args), reporting.FullName); } - - public void Fatal(Type reporting, Exception exception, string message) - { - System.Diagnostics.Debug.WriteLine(message + Environment.NewLine + exception, reporting.FullName); - } - - public void Fatal(Type reporting, Exception exception, string messageTemplate, params object[] args) - { - System.Diagnostics.Debug.WriteLine(string.Format(messageTemplate, args) + Environment.NewLine + exception, reporting.FullName); - } + } } diff --git a/src/Umbraco.Core/Logging/DisposableTimer.cs b/src/Umbraco.Core/Logging/DisposableTimer.cs index e1e9d29c5f..869ca2cd44 100644 --- a/src/Umbraco.Core/Logging/DisposableTimer.cs +++ b/src/Umbraco.Core/Logging/DisposableTimer.cs @@ -17,7 +17,7 @@ namespace Umbraco.Core.Logging private string _failMessage; private Exception _failException; private bool _failed; - private Guid _timingId; + private readonly string _timingId; internal enum LogType { @@ -38,7 +38,7 @@ namespace Umbraco.Core.Logging _endMessage = endMessage; _failMessage = failMessage; _thresholdMilliseconds = thresholdMilliseconds < 0 ? 0 : thresholdMilliseconds; - _timingId = Guid.NewGuid(); + _timingId = Guid.NewGuid().ToString("N"); if (thresholdMilliseconds == 0) { diff --git a/src/Umbraco.Core/Logging/ILogger.cs b/src/Umbraco.Core/Logging/ILogger.cs index 643ad044a3..ec6d8edf31 100644 --- a/src/Umbraco.Core/Logging/ILogger.cs +++ b/src/Umbraco.Core/Logging/ILogger.cs @@ -7,6 +7,45 @@ namespace Umbraco.Core.Logging /// public interface ILogger { + /// + /// Logs a fatal message. + /// + /// The reporting type. + /// An exception. + /// A message. + void Fatal(Type reporting, Exception exception, string message); + + /// + /// Logs a fatal message NOTE: This will log an empty message string + /// + /// The reporting type. + /// An exception. + void Fatal(Type reporting, Exception exception); + + /// + /// Logs a fatal message WITHOUT EX + /// + /// The reporting type. + /// A message. + void Fatal(Type reporting, string message); + + /// + /// Logs a fatal message - using a structured log message + /// + /// The reporting type. + /// An exception. + /// The message template that includes property values + /// Property values to log & update in message template + void Fatal(Type reporting, Exception exception, string messageTemplate, params object[] propertyValues); + + /// + /// Logs a fatal message WITHOUT EX - using a structured log message + /// + /// The reporting type. + /// The message template that includes property values + /// Property values to log & update in message template + void Fatal(Type reporting, string messageTemplate, params object[] propertyValues); + /// /// Logs an error message. /// @@ -122,22 +161,6 @@ namespace Umbraco.Core.Logging /// The message template that includes property values /// Property values to log & update in message template void Verbose(Type reporting, string messageTemplate, params object[] propertyValues); - - /// - /// Logs a fatal message. - /// - /// The reporting type. - /// An exception. - /// A message. - void Fatal(Type reporting, Exception exception, string message); - - /// - /// Logs a fatal message - using a structured log message - /// - /// The reporting type. - /// An exception. - /// The message template that includes property values - /// Property values to log & update in message template - void Fatal(Type reporting, Exception exception, string messageTemplate, params object[] propertyValues); + } } diff --git a/src/Umbraco.Core/Logging/Logger.cs b/src/Umbraco.Core/Logging/Logger.cs index 656de088a7..0a20022e93 100644 --- a/src/Umbraco.Core/Logging/Logger.cs +++ b/src/Umbraco.Core/Logging/Logger.cs @@ -49,6 +49,40 @@ namespace Umbraco.Core.Logging return new Logger(loggerConfig); } + /// + public void Fatal(Type reporting, Exception exception, string message) + { + Fatal(reporting, exception, message, null); + } + + /// + public void Fatal(Type reporting, Exception exception) + { + Fatal(reporting, exception, string.Empty); + } + + /// + public void Fatal(Type reporting, string message) + { + //Sometimes we need to throw an error without an ex + Fatal(reporting, null, message); + } + + /// + public void Fatal(Type reporting, string messageTemplate, params object[] propertyValues) + { + //Log a structured message WITHOUT an ex + Fatal(reporting, null, messageTemplate, propertyValues); + } + + /// + public void Fatal(Type reporting, Exception exception, string messageTemplate, params object[] propertyValues) + { + ErrorOrFatal(Fatal, exception, ref messageTemplate); + var logger = Log.Logger; + logger?.ForContext(reporting).Fatal(exception, messageTemplate, propertyValues); + } + /// public void Error(Type reporting, Exception exception, string message) { @@ -77,6 +111,13 @@ namespace Umbraco.Core.Logging /// public void Error(Type reporting, Exception exception, string messageTemplate, params object[] propertyValues) + { + ErrorOrFatal(Error, exception, ref messageTemplate); + var logger = Log.Logger; + logger?.ForContext(reporting).Error(exception, messageTemplate, propertyValues); + } + + private static void ErrorOrFatal(Action logAction, Exception exception, ref string messageTemplate) { var dump = false; @@ -103,18 +144,15 @@ namespace Umbraco.Core.Logging catch (Exception ex) { //Log a new entry (as opposed to appending to same log entry) - Error(ex.GetType(), ex, "Failed to create a minidump at App_Data/MiniDump ({ExType}: {ExMessage}", ex.GetType().FullName, ex.Message); + logAction(ex.GetType(), ex, "Failed to create a minidump at App_Data/MiniDump ({ExType}: {ExMessage}", + new object[]{ ex.GetType().FullName, ex.Message }); } } - - var logger = Log.Logger; - logger?.ForContext(reporting).Error(exception, messageTemplate, propertyValues); } private static bool IsMonitorEnterThreadAbortException(Exception exception) { - var abort = exception as ThreadAbortException; - if (abort == null) return false; + if (!(exception is ThreadAbortException abort)) return false; var stacktrace = abort.StackTrace; return stacktrace.Contains("System.Threading.Monitor.ReliableEnter"); @@ -122,8 +160,7 @@ namespace Umbraco.Core.Logging private static bool IsTimeoutThreadAbortException(Exception exception) { - var abort = exception as ThreadAbortException; - if (abort == null) return false; + if (!(exception is ThreadAbortException abort)) return false; if (abort.ExceptionState == null) return false; @@ -139,22 +176,19 @@ namespace Umbraco.Core.Logging /// public void Warn(Type reporting, string format) { - var logger = Log.Logger; - logger?.Warning(format); + Warn(reporting, null, format); } /// public void Warn(Type reporting, string messageTemplate, params object[] propertyValues) { - var logger = Log.Logger; - logger?.ForContext(reporting).Warning(messageTemplate, propertyValues); + Warn(reporting, null, messageTemplate, propertyValues); } /// public void Warn(Type reporting, Exception exception, string message) { - var logger = Log.Logger; - logger?.ForContext(reporting).Warning(message, exception); + Warn(reporting, exception, message, Array.Empty()); } /// @@ -167,8 +201,7 @@ namespace Umbraco.Core.Logging /// public void Info(Type reporting, string message) { - var logger = Log.Logger; - logger?.ForContext(reporting).Information(message); + Info(reporting, message, Array.Empty()); } /// @@ -181,8 +214,7 @@ namespace Umbraco.Core.Logging /// public void Debug(Type reporting, string message) { - var logger = Log.Logger; - logger?.ForContext(reporting).Debug(message); + Debug(reporting, message, Array.Empty()); } /// @@ -195,8 +227,7 @@ namespace Umbraco.Core.Logging /// public void Verbose(Type reporting, string message) { - var logger = Log.Logger; - logger?.ForContext(reporting).Verbose(message); + Verbose(reporting, message, Array.Empty()); } /// @@ -206,18 +237,6 @@ namespace Umbraco.Core.Logging logger?.ForContext(reporting).Verbose(messageTemplate, propertyValues); } - /// - public void Fatal(Type reporting, Exception exception, string message) - { - var logger = Log.Logger; - logger?.ForContext(reporting).Fatal(exception, message); - } - - /// - public void Fatal(Type reporting, Exception exception, string messageTemplate, params object[] propertyValues) - { - var logger = Log.Logger; - logger?.ForContext(reporting).Fatal(exception, messageTemplate, propertyValues); - } + } } diff --git a/src/Umbraco.Core/Logging/SerilogExtensions/LoggerConfigExtensions.cs b/src/Umbraco.Core/Logging/SerilogExtensions/LoggerConfigExtensions.cs index 6049e2a45e..150fb0395c 100644 --- a/src/Umbraco.Core/Logging/SerilogExtensions/LoggerConfigExtensions.cs +++ b/src/Umbraco.Core/Logging/SerilogExtensions/LoggerConfigExtensions.cs @@ -45,6 +45,7 @@ namespace Umbraco.Core.Logging.SerilogExtensions //Main .txt logfile - in similar format to older Log4Net output //Ends with ..txt as Date is inserted before file extension substring logConfig.WriteTo.File($@"{AppDomain.CurrentDomain.BaseDirectory}\App_Data\Logs\UmbracoTraceLog.{Environment.MachineName}..txt", + shared: true, rollingInterval: RollingInterval.Day, restrictedToMinimumLevel: minimumLevel, retainedFileCountLimit: null, //Setting to null means we keep all files - default is 31 days @@ -64,6 +65,7 @@ namespace Umbraco.Core.Logging.SerilogExtensions //.clef format (Compact log event format, that can be imported into local SEQ & will make searching/filtering logs easier) //Ends with ..txt as Date is inserted before file extension substring logConfig.WriteTo.File(new CompactJsonFormatter(), $@"{AppDomain.CurrentDomain.BaseDirectory}\App_Data\Logs\UmbracoTraceLog.{Environment.MachineName}..json", + shared: true, rollingInterval: RollingInterval.Day, //Create a new JSON file every day retainedFileCountLimit: retainedFileCount, //Setting to null means we keep all files - default is 31 days restrictedToMinimumLevel: minimumLevel); diff --git a/src/Umbraco.Tests/TestHelpers/ConsoleLogger.cs b/src/Umbraco.Tests/TestHelpers/ConsoleLogger.cs index e5efd1560c..9caa4b2aa1 100644 --- a/src/Umbraco.Tests/TestHelpers/ConsoleLogger.cs +++ b/src/Umbraco.Tests/TestHelpers/ConsoleLogger.cs @@ -5,6 +5,34 @@ namespace Umbraco.Tests.TestHelpers { public class ConsoleLogger : ILogger { + public void Fatal(Type reporting, Exception exception, string message) + { + Console.WriteLine("FATAL {0} - {1}", reporting.Name, message); + Console.WriteLine(exception); + } + + public void Fatal(Type reporting, Exception exception) + { + Console.WriteLine("FATAL {0}", reporting.Name); + Console.WriteLine(exception); + } + + public void Fatal(Type reporting, string message) + { + Console.WriteLine("FATAL {0} - {1}", reporting.Name, message); + } + + public void Fatal(Type reporting, Exception exception, string format, params object[] args) + { + Console.WriteLine("FATAL {0} - {1}", reporting.Name, string.Format(format, args)); + Console.WriteLine(exception); + } + + public void Fatal(Type reporting, string format, params object[] args) + { + Console.WriteLine("FATAL {0} - {1}", reporting.Name, string.Format(format, args)); + } + public void Error(Type reporting, Exception exception, string message) { Console.WriteLine("ERROR {0} - {1}", reporting.Name, message); @@ -84,17 +112,5 @@ namespace Umbraco.Tests.TestHelpers { Console.WriteLine("VERBOSE {0} - {1}", reporting.Name, string.Format(format, args)); } - - public void Fatal(Type reporting, Exception exception, string message) - { - Console.WriteLine("FATAL {0} - {1}", reporting.Name, message); - Console.WriteLine(exception); - } - - public void Fatal(Type reporting, Exception exception, string format, params object[] args) - { - Console.WriteLine("FATAL {0} - {1}", reporting.Name, string.Format(format, args)); - Console.WriteLine(exception); - } } } diff --git a/src/Umbraco.Tests/Testing/UmbracoTestBase.cs b/src/Umbraco.Tests/Testing/UmbracoTestBase.cs index dce0346aa4..3e40a7c3f7 100644 --- a/src/Umbraco.Tests/Testing/UmbracoTestBase.cs +++ b/src/Umbraco.Tests/Testing/UmbracoTestBase.cs @@ -167,7 +167,7 @@ namespace Umbraco.Tests.Testing } else if (option == UmbracoTestOptions.Logger.Serilog) { - Container.RegisterSingleton(f => new Logger(new FileInfo(TestHelper.MapPathForTest("~/unit-test-log4net.config")))); + Container.RegisterSingleton(f => new Logger(new FileInfo(TestHelper.MapPathForTest("~/unit-test.config")))); Container.RegisterSingleton(f => new LogProfiler(f.GetInstance())); } diff --git a/src/Umbraco.Tests/UmbracoExamine/ExamineBaseTest.cs b/src/Umbraco.Tests/UmbracoExamine/ExamineBaseTest.cs index 154b587a47..1e5e909bde 100644 --- a/src/Umbraco.Tests/UmbracoExamine/ExamineBaseTest.cs +++ b/src/Umbraco.Tests/UmbracoExamine/ExamineBaseTest.cs @@ -19,7 +19,7 @@ namespace Umbraco.Tests.UmbracoExamine [OneTimeSetUp] public void InitializeFixture() { - var logger = new Logger(new FileInfo(TestHelper.MapPathForTest("~/unit-test-log4net.config"))); + var logger = new Logger(new FileInfo(TestHelper.MapPathForTest("~/unit-test.config"))); _profilingLogger = new ProfilingLogger(logger, new LogProfiler(logger)); } diff --git a/src/Umbraco.Web.UI/config/serilog.user.config b/src/Umbraco.Web.UI/config/serilog.user.config index ad78e8e004..c308c25104 100644 --- a/src/Umbraco.Web.UI/config/serilog.user.config +++ b/src/Umbraco.Web.UI/config/serilog.user.config @@ -2,28 +2,36 @@ - + + - - + + + - - + + + + --> + +