From 13992322b25715c86894f11fb71bae5c434b98f1 Mon Sep 17 00:00:00 2001 From: Frode Nilsen Date: Thu, 16 Aug 2018 14:33:02 +0200 Subject: [PATCH 01/15] Added Trace level --- .../Logging/ConsoleLoggerTestCase.cs | 28 +++++ .../Logging/LevelFilteredLoggerTests.cs | 19 ++++ .../Core.Tests/Logging/StreamLoggerTests.cs | 26 ++++- .../DynamicProxy.Tests/LoggingTestCase.cs | 40 +++++++ src/Castle.Core/Core/Logging/ConsoleLogger.cs | 4 +- .../Core/Logging/DiagnosticsLogger.cs | 2 +- src/Castle.Core/Core/Logging/ILogger.cs | 56 ++++++++++ .../Core/Logging/LevelFilteredLogger.cs | 102 ++++++++++++++++++ src/Castle.Core/Core/Logging/LoggerLevel.cs | 4 + src/Castle.Core/Core/Logging/NullLogger.cs | 70 ++++++++++++ src/Castle.Core/Core/Logging/StreamLogger.cs | 2 +- src/Castle.Core/Core/Logging/TraceLogger.cs | 5 +- .../NLogLogger.cs | 99 +++++++++++++++++ .../SerilogLogger.cs | 67 +++++++++++- .../Log4netLogger.cs | 61 +++++++++++ 15 files changed, 577 insertions(+), 8 deletions(-) diff --git a/src/Castle.Core.Tests/Core.Tests/Logging/ConsoleLoggerTestCase.cs b/src/Castle.Core.Tests/Core.Tests/Logging/ConsoleLoggerTestCase.cs index e084f494d4..94fab1e95e 100644 --- a/src/Castle.Core.Tests/Core.Tests/Logging/ConsoleLoggerTestCase.cs +++ b/src/Castle.Core.Tests/Core.Tests/Logging/ConsoleLoggerTestCase.cs @@ -53,6 +53,7 @@ public void InfoLogger() { ConsoleLogger log = new ConsoleLogger("Logger", LoggerLevel.Info); + log.Trace("Some trace message"); log.Debug("Some debug message"); log.Info("Some info message"); log.Error("Some error message"); @@ -75,6 +76,7 @@ public void DebugLogger() { ConsoleLogger log = new ConsoleLogger("Logger", LoggerLevel.Debug); + log.Trace("Some trace message"); log.Debug("Some debug message"); log.Info("Some info message"); log.Error("Some error message"); @@ -93,11 +95,37 @@ public void DebugLogger() Assert.AreEqual(expected.GetStringBuilder().ToString(), logcontents, "logcontents don't match"); } + [Test] + public void TraceLogger() + { + ConsoleLogger log = new ConsoleLogger("Logger", LoggerLevel.Trace); + + log.Trace("Some trace message"); + log.Debug("Some debug message"); + log.Info("Some info message"); + log.Error("Some error message"); + log.Fatal("Some fatal error message"); + log.Warn("Some warn message"); + + String logcontents = outWriter.GetStringBuilder().ToString(); + + StringWriter expected = new StringWriter(); + expected.WriteLine("[Trace] 'Logger' Some trace message"); + expected.WriteLine("[Debug] 'Logger' Some debug message"); + expected.WriteLine("[Info] 'Logger' Some info message"); + expected.WriteLine("[Error] 'Logger' Some error message"); + expected.WriteLine("[Fatal] 'Logger' Some fatal error message"); + expected.WriteLine("[Warn] 'Logger' Some warn message"); + + Assert.AreEqual(expected.GetStringBuilder().ToString(), logcontents, "logcontents don't match"); + } + [Test] public void WarnLogger() { ConsoleLogger log = new ConsoleLogger("Logger", LoggerLevel.Warn); + log.Trace("Some trace message"); log.Debug("Some debug message"); log.Info("Some info message"); log.Error("Some error message"); diff --git a/src/Castle.Core.Tests/Core.Tests/Logging/LevelFilteredLoggerTests.cs b/src/Castle.Core.Tests/Core.Tests/Logging/LevelFilteredLoggerTests.cs index 9fb26cf1af..065823e7bc 100644 --- a/src/Castle.Core.Tests/Core.Tests/Logging/LevelFilteredLoggerTests.cs +++ b/src/Castle.Core.Tests/Core.Tests/Logging/LevelFilteredLoggerTests.cs @@ -35,11 +35,25 @@ public void SetUp() logger = new LevelFilteredLoggerInstance(null); } + [Test] + public void LevelTrace() + { + logger.Level = LoggerLevel.Trace; + + Assert.IsTrue(logger.IsTraceEnabled, "LevelFilteredLogger.IsTraceEnabled is not returning true when the level is Debug"); + Assert.IsTrue(logger.IsDebugEnabled, "LevelFilteredLogger.IsDebugEnabled is not returning true when the level is Debug"); + Assert.IsTrue(logger.IsInfoEnabled, "LevelFilteredLogger.IsInfoEnabled is not returning true when the level is Debug"); + Assert.IsTrue(logger.IsWarnEnabled, "LevelFilteredLogger.IsWarnEnabled is not returning true when the level is Debug"); + Assert.IsTrue(logger.IsErrorEnabled, "LevelFilteredLogger.IsErrorEnabled is not returning true when the level is Debug"); + Assert.IsTrue(logger.IsFatalEnabled, "LevelFilteredLogger.IsFatalErrorEnabled is not returning true when the level is Debug"); + } + [Test] public void LevelDebug() { logger.Level = LoggerLevel.Debug; + Assert.IsFalse(logger.IsTraceEnabled, "LevelFilteredLogger.IsTraceEnabled is not returning false when the level is Debug"); Assert.IsTrue(logger.IsDebugEnabled, "LevelFilteredLogger.IsDebugEnabled is not returning true when the level is Debug"); Assert.IsTrue(logger.IsInfoEnabled, "LevelFilteredLogger.IsInfoEnabled is not returning true when the level is Debug"); Assert.IsTrue(logger.IsWarnEnabled, "LevelFilteredLogger.IsWarnEnabled is not returning true when the level is Debug"); @@ -52,6 +66,7 @@ public void LevelInfo() { logger.Level = LoggerLevel.Info; + Assert.IsFalse(logger.IsTraceEnabled, "LevelFilteredLogger.IsTraceEnabled is not returning false when the level is Debug"); Assert.IsFalse(logger.IsDebugEnabled, "LevelFilteredLogger.IsDebugEnabled is not returning false when the level is Info"); Assert.IsTrue(logger.IsWarnEnabled, "LevelFilteredLogger.IsWarnEnabled is not returning true when the level is Info"); Assert.IsTrue(logger.IsInfoEnabled, "LevelFilteredLogger.IsInfoEnabled is not returning true when the level is Info"); @@ -64,6 +79,7 @@ public void LevelWarn() { logger.Level = LoggerLevel.Warn; + Assert.IsFalse(logger.IsTraceEnabled, "LevelFilteredLogger.IsTraceEnabled is not returning false when the level is Debug"); Assert.IsFalse(logger.IsDebugEnabled, "LevelFilteredLogger.IsDebugEnabled is not returning false when the level is Warn"); Assert.IsFalse(logger.IsInfoEnabled, "LevelFilteredLogger.IsInfoEnabled is not returning false when the level is Warn"); Assert.IsTrue(logger.IsWarnEnabled, "LevelFilteredLogger.IsWarnEnabled is not returning true when the level is Warn"); @@ -76,6 +92,7 @@ public void LevelError() { logger.Level = LoggerLevel.Error; + Assert.IsFalse(logger.IsTraceEnabled, "LevelFilteredLogger.IsTraceEnabled is not returning false when the level is Debug"); Assert.IsFalse(logger.IsDebugEnabled, "LevelFilteredLogger.IsDebugEnabled is not returning false when the level is Error"); Assert.IsFalse(logger.IsInfoEnabled, "LevelFilteredLogger.IsInfoEnabled is not returning false when the level is Error"); Assert.IsFalse(logger.IsWarnEnabled, "LevelFilteredLogger.IsWarnEnabled is not returning false when the level is Error"); @@ -88,6 +105,7 @@ public void LevelFatal() { logger.Level = LoggerLevel.Fatal; + Assert.IsFalse(logger.IsTraceEnabled, "LevelFilteredLogger.IsTraceEnabled is not returning false when the level is Debug"); Assert.IsFalse(logger.IsDebugEnabled, "LevelFilteredLogger.IsDebugEnabled is not returning false when the level is Fatal"); Assert.IsFalse(logger.IsInfoEnabled, "LevelFilteredLogger.IsInfoEnabled is not returning false when the level is Fatal"); Assert.IsFalse(logger.IsWarnEnabled, "LevelFilteredLogger.IsWarnEnabled is not returning false when the level is Fatal"); @@ -100,6 +118,7 @@ public void LevelOff() { logger.Level = LoggerLevel.Off; + Assert.IsFalse(logger.IsTraceEnabled, "LevelFilteredLogger.IsTraceEnabled is not returning false when the level is Off"); Assert.IsFalse(logger.IsDebugEnabled, "LevelFilteredLogger.IsDebugEnabled is not returning false when the level is Off"); Assert.IsFalse(logger.IsInfoEnabled, "LevelFilteredLogger.IsInfoEnabled is not returning false when the level is Off"); Assert.IsFalse(logger.IsWarnEnabled, "LevelFilteredLogger.IsWarnEnabled is not returning false when the level is Off"); diff --git a/src/Castle.Core.Tests/Core.Tests/Logging/StreamLoggerTests.cs b/src/Castle.Core.Tests/Core.Tests/Logging/StreamLoggerTests.cs index 211f6ccb94..462dbdd9ac 100644 --- a/src/Castle.Core.Tests/Core.Tests/Logging/StreamLoggerTests.cs +++ b/src/Castle.Core.Tests/Core.Tests/Logging/StreamLoggerTests.cs @@ -34,7 +34,31 @@ public void SetUp() stream = new MemoryStream(); logger = new StreamLogger(Name, stream); - logger.Level = LoggerLevel.Debug; + logger.Level = LoggerLevel.Trace; + } + + [Test] + public void Trace() + { + string message = "Trace message"; + LoggerLevel level = LoggerLevel.Trace; + Exception exception = null; + + logger.Trace(message); + + ValidateCall(level, message, exception); + } + + [Test] + public void TraceWithException() + { + string message = "Trace message 2"; + LoggerLevel level = LoggerLevel.Trace; + Exception exception = new Exception(); + + logger.Trace(message, exception); + + ValidateCall(level, message, exception); } [Test] diff --git a/src/Castle.Core.Tests/DynamicProxy.Tests/LoggingTestCase.cs b/src/Castle.Core.Tests/DynamicProxy.Tests/LoggingTestCase.cs index 44d9d10c10..70192b0309 100644 --- a/src/Castle.Core.Tests/DynamicProxy.Tests/LoggingTestCase.cs +++ b/src/Castle.Core.Tests/DynamicProxy.Tests/LoggingTestCase.cs @@ -168,6 +168,41 @@ public bool RecordedMessage(LoggerLevel level, string message) return messages.Contains(level.ToString().ToUpper() + ": " + message); } + public void Trace(string message) + { + throw new NotImplementedException(); + } + + public void Trace(Func messageFactory) + { + throw new NotImplementedException(); + } + + public void Trace(string message, Exception exception) + { + throw new NotImplementedException(); + } + + public void TraceFormat(string format, params object[] args) + { + messages.Add("TRACE: " + string.Format(format, args)); + } + + public void TraceFormat(Exception exception, string format, params object[] args) + { + throw new NotImplementedException(); + } + + public void TraceFormat(IFormatProvider formatProvider, string format, params object[] args) + { + throw new NotImplementedException(); + } + + public void TraceFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args) + { + throw new NotImplementedException(); + } + public void Debug(string message) { throw new NotImplementedException(); @@ -348,6 +383,11 @@ public ILogger CreateChildLogger(string loggerName) throw new NotImplementedException(); } + public bool IsTraceEnabled + { + get { return true; } + } + public bool IsDebugEnabled { get { return true; } diff --git a/src/Castle.Core/Core/Logging/ConsoleLogger.cs b/src/Castle.Core/Core/Logging/ConsoleLogger.cs index 6aedbab178..15ebdd2c08 100644 --- a/src/Castle.Core/Core/Logging/ConsoleLogger.cs +++ b/src/Castle.Core/Core/Logging/ConsoleLogger.cs @@ -32,7 +32,7 @@ public class ConsoleLogger : LevelFilteredLogger /// set to LoggerLevel.Debug and the Name /// set to String.Empty. /// - public ConsoleLogger() : this(String.Empty, LoggerLevel.Debug) + public ConsoleLogger() : this(String.Empty, LoggerLevel.Trace) { } @@ -50,7 +50,7 @@ public ConsoleLogger(LoggerLevel logLevel) : this(String.Empty, logLevel) /// set to LoggerLevel.Debug. /// /// The logs Name. - public ConsoleLogger(String name) : this(name, LoggerLevel.Debug) + public ConsoleLogger(String name) : this(name, LoggerLevel.Trace) { } diff --git a/src/Castle.Core/Core/Logging/DiagnosticsLogger.cs b/src/Castle.Core/Core/Logging/DiagnosticsLogger.cs index bfd1cb328c..5a48a7f5df 100644 --- a/src/Castle.Core/Core/Logging/DiagnosticsLogger.cs +++ b/src/Castle.Core/Core/Logging/DiagnosticsLogger.cs @@ -46,7 +46,7 @@ public DiagnosticsLogger(string logName) : this(logName, "default") /// /// /// - public DiagnosticsLogger(string logName, string source) : base(LoggerLevel.Debug) + public DiagnosticsLogger(string logName, string source) : base(LoggerLevel.Trace) { // Create the source, if it does not already exist. if (!EventLog.SourceExists(source)) diff --git a/src/Castle.Core/Core/Logging/ILogger.cs b/src/Castle.Core/Core/Logging/ILogger.cs index e47d7f8e29..bf3640ea8a 100644 --- a/src/Castle.Core/Core/Logging/ILogger.cs +++ b/src/Castle.Core/Core/Logging/ILogger.cs @@ -26,6 +26,12 @@ namespace Castle.Core.Logging /// public interface ILogger { + /// + /// Determines if messages of priority "trace" will be logged. + /// + /// True if "trace" messages will be logged. + bool IsTraceEnabled { get; } + /// /// Determines if messages of priority "debug" will be logged. /// @@ -65,6 +71,56 @@ public interface ILogger /// If the name has an empty element name. ILogger CreateChildLogger(String loggerName); + /// + /// Logs a trace message. + /// + /// The message to log + void Trace(String message); + + /// + /// Logs a trace message with lazily constructed message. The message will be constructed only if the is true. + /// + void Trace(Func messageFactory); + + /// + /// Logs a trace message. + /// + /// The exception to log + /// The message to log + void Trace(String message, Exception exception); + + /// + /// Logs a trace message. + /// + /// Format string for the message to log + /// Format arguments for the message to log + void TraceFormat(String format, params Object[] args); + + /// + /// Logs a trace message. + /// + /// The exception to log + /// Format string for the message to log + /// Format arguments for the message to log + void TraceFormat(Exception exception, String format, params Object[] args); + + /// + /// Logs a trace message. + /// + /// The format provider to use + /// Format string for the message to log + /// Format arguments for the message to log + void TraceFormat(IFormatProvider formatProvider, String format, params Object[] args); + + /// + /// Logs a trace message. + /// + /// The exception to log + /// The format provider to use + /// Format string for the message to log + /// Format arguments for the message to log + void TraceFormat(Exception exception, IFormatProvider formatProvider, String format, params Object[] args); + /// /// Logs a debug message. /// diff --git a/src/Castle.Core/Core/Logging/LevelFilteredLogger.cs b/src/Castle.Core/Core/Logging/LevelFilteredLogger.cs index 5094a740ea..13d272f5af 100644 --- a/src/Castle.Core/Core/Logging/LevelFilteredLogger.cs +++ b/src/Castle.Core/Core/Logging/LevelFilteredLogger.cs @@ -103,6 +103,99 @@ public String Name #region ILogger implementation + #region Trace + + /// + /// Logs a trace message. + /// + /// The message to log + public void Trace(string message) + { + if (IsTraceEnabled) + { + Log(LoggerLevel.Trace, message, null); + } + } + + public void Trace(Func messageFactory) + { + if (IsTraceEnabled) + { + Log(LoggerLevel.Trace, messageFactory.Invoke(), null); + } + } + + /// + /// Logs a trace message. + /// + /// The exception to log + /// The message to log + public void Trace(string message, Exception exception) + { + if (IsTraceEnabled) + { + Log(LoggerLevel.Trace, message, exception); + } + } + + /// + /// Logs a trace message. + /// + /// Format string for the message to log + /// Format arguments for the message to log + public void TraceFormat(string format, params object[] args) + { + if (IsTraceEnabled) + { + Log(LoggerLevel.Trace, String.Format(CultureInfo.CurrentCulture, format, args), null); + } + } + + /// + /// Logs a trace message. + /// + /// The exception to log + /// Format string for the message to log + /// Format arguments for the message to log + public void TraceFormat(Exception exception, string format, params object[] args) + { + if (IsTraceEnabled) + { + Log(LoggerLevel.Trace, String.Format(CultureInfo.CurrentCulture, format, args), exception); + } + } + + /// + /// Logs a trace message. + /// + /// The format provider to use + /// Format string for the message to log + /// Format arguments for the message to log + public void TraceFormat(IFormatProvider formatProvider, string format, params object[] args) + { + if (IsTraceEnabled) + { + Log(LoggerLevel.Trace, String.Format(formatProvider, format, args), null); + } + } + + /// + /// Logs a trace message. + /// + /// The exception to log + /// The format provider to use + /// Format string for the message to log + /// Format arguments for the message to log + public void TraceFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args) + { + if (IsTraceEnabled) + { + Log(LoggerLevel.Trace, String.Format(formatProvider, format, args), exception); + } + } + + #endregion + #region Debug /// @@ -638,6 +731,15 @@ public void FatalFormat(Exception exception, IFormatProvider formatProvider, str #endregion + /// + /// Determines if messages of priority "trace" will be logged. + /// + /// true if log level flags include the bit + public bool IsTraceEnabled + { + get { return (Level >= LoggerLevel.Trace); } + } + /// /// Determines if messages of priority "debug" will be logged. /// diff --git a/src/Castle.Core/Core/Logging/LoggerLevel.cs b/src/Castle.Core/Core/Logging/LoggerLevel.cs index 296905c017..9c5173b7ca 100644 --- a/src/Castle.Core/Core/Logging/LoggerLevel.cs +++ b/src/Castle.Core/Core/Logging/LoggerLevel.cs @@ -43,5 +43,9 @@ public enum LoggerLevel /// Debug logging level /// Debug = 5, + /// + /// Trace logging level + /// + Trace = 6 } } \ No newline at end of file diff --git a/src/Castle.Core/Core/Logging/NullLogger.cs b/src/Castle.Core/Core/Logging/NullLogger.cs index 896c8b9cdf..765a5a2d9d 100644 --- a/src/Castle.Core/Core/Logging/NullLogger.cs +++ b/src/Castle.Core/Core/Logging/NullLogger.cs @@ -49,6 +49,15 @@ public IContextStacks ThreadStacks get { return NullContextStacks.Instance; } } + /// + /// No-op. + /// + /// false + public bool IsTraceEnabled + { + get { return false; } + } + /// /// No-op. /// @@ -104,6 +113,67 @@ public ILogger CreateChildLogger(string loggerName) return this; } + /// + /// No-op. + /// + /// Ignored + public void Trace(string message) + { + } + + public void Trace(Func messageFactory) + { + } + + /// + /// No-op. + /// + /// Ignored + /// Ignored + public void Trace(string message, Exception exception) + { + } + + /// + /// No-op. + /// + /// Ignored + /// Ignored + public void TraceFormat(string format, params object[] args) + { + } + + /// + /// No-op. + /// + /// Ignored + /// Ignored + /// Ignored + public void TraceFormat(Exception exception, string format, params object[] args) + { + } + + /// + /// No-op. + /// + /// Ignored + /// Ignored + /// Ignored + public void TraceFormat(IFormatProvider formatProvider, string format, params object[] args) + { + } + + /// + /// No-op. + /// + /// Ignored + /// Ignored + /// Ignored + /// Ignored + public void TraceFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args) + { + } + /// /// No-op. /// diff --git a/src/Castle.Core/Core/Logging/StreamLogger.cs b/src/Castle.Core/Core/Logging/StreamLogger.cs index 894e605f27..3bc231b1d9 100644 --- a/src/Castle.Core/Core/Logging/StreamLogger.cs +++ b/src/Castle.Core/Core/Logging/StreamLogger.cs @@ -124,7 +124,7 @@ protected virtual void Dispose(bool disposing) /// /// The name of the log. /// The StreamWriter the log will write to. - protected StreamLogger(String name, StreamWriter writer) : base(name, LoggerLevel.Debug) + protected StreamLogger(String name, StreamWriter writer) : base(name, LoggerLevel.Trace) { this.writer = writer; writer.AutoFlush = true; diff --git a/src/Castle.Core/Core/Logging/TraceLogger.cs b/src/Castle.Core/Core/Logging/TraceLogger.cs index 74cf5a542d..024c8031e8 100644 --- a/src/Castle.Core/Core/Logging/TraceLogger.cs +++ b/src/Castle.Core/Core/Logging/TraceLogger.cs @@ -191,7 +191,7 @@ private static LoggerLevel MapLoggerLevel(SourceLevels level) switch (level) { case SourceLevels.All: - return LoggerLevel.Debug; + return LoggerLevel.Trace; case SourceLevels.Verbose: return LoggerLevel.Debug; case SourceLevels.Information: @@ -210,6 +210,8 @@ private static SourceLevels MapSourceLevels(LoggerLevel level) { switch (level) { + case LoggerLevel.Trace: + return SourceLevels.All; case LoggerLevel.Debug: return SourceLevels.Verbose; case LoggerLevel.Info: @@ -228,6 +230,7 @@ private static TraceEventType MapTraceEventType(LoggerLevel level) { switch (level) { + case LoggerLevel.Trace: case LoggerLevel.Debug: return TraceEventType.Verbose; case LoggerLevel.Info: diff --git a/src/Castle.Services.Logging.NLogIntegration/NLogLogger.cs b/src/Castle.Services.Logging.NLogIntegration/NLogLogger.cs index 2bff408bff..14d06992b7 100644 --- a/src/Castle.Services.Logging.NLogIntegration/NLogLogger.cs +++ b/src/Castle.Services.Logging.NLogIntegration/NLogLogger.cs @@ -38,6 +38,15 @@ internal NLogLogger() { } + /// + /// Determines if messages of priority "trace" will be logged. + /// + /// True if "trace" messages will be logged. + public bool IsTraceEnabled + { + get { return Logger.IsTraceEnabled; } + } + /// /// Determines if messages of priority "debug" will be logged. /// @@ -113,6 +122,96 @@ public virtual Core.Logging.ILogger CreateChildLogger(String name) return Factory.Create(Logger.Name + "." + name); } + /// + /// Logs a trace message. + /// + /// The message to log + public void Trace(string message) + { + Log(LogLevel.Trace, message); + } + + /// + /// Logs a trace message. + /// + /// Factory constructing lazily the message to log if the level is enabled + public void Trace(Func messageFactory) + { + if (IsTraceEnabled) + { + Log(LogLevel.Debug, messageFactory()); + } + } + + /// + /// Logs a trace message. + /// + /// The exception to log + /// The message to log + public void Trace(string message, Exception exception) + { + if (IsTraceEnabled) + { + Log(LogLevel.Trace, message, exception); + } + } + + /// + /// Logs a trace message. + /// + /// Format string for the message to log + /// Format arguments for the message to log + public void TraceFormat(string format, params object[] args) + { + if (IsTraceEnabled) + { + Log(LogLevel.Trace, format, args); + } + } + + /// + /// Logs a trace message. + /// + /// The exception to log + /// Format string for the message to log + /// Format arguments for the message to log + public void TraceFormat(Exception exception, string format, params object[] args) + { + if (IsTraceEnabled) + { + Log(LogLevel.Trace, exception, format, args); + } + } + + /// + /// Logs a trace message. + /// + /// The format provider to use + /// Format string for the message to log + /// Format arguments for the message to log + public void TraceFormat(IFormatProvider formatProvider, string format, params object[] args) + { + if (IsTraceEnabled) + { + Log(LogLevel.Trace, formatProvider, format, args); + } + } + + /// + /// Logs a trace message. + /// + /// The exception to log + /// The format provider to use + /// Format string for the message to log + /// Format arguments for the message to log + public void TraceFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args) + { + if (IsTraceEnabled) + { + Log(LogLevel.Trace, exception, formatProvider, format, args); + } + } + /// /// Logs a debug message. /// diff --git a/src/Castle.Services.Logging.SerilogIntegration/SerilogLogger.cs b/src/Castle.Services.Logging.SerilogIntegration/SerilogLogger.cs index 66f3b1bfad..d748fce747 100644 --- a/src/Castle.Services.Logging.SerilogIntegration/SerilogLogger.cs +++ b/src/Castle.Services.Logging.SerilogIntegration/SerilogLogger.cs @@ -40,7 +40,12 @@ internal SerilogLogger() { } protected internal SerilogFactory Factory { get; set; } - public bool IsDebugEnabled + public bool IsTraceEnabled + { + get { return Logger.IsEnabled(LogEventLevel.Debug); } + } + + public bool IsDebugEnabled { get { return Logger.IsEnabled(LogEventLevel.Debug); } } @@ -76,7 +81,65 @@ public Castle.Core.Logging.ILogger CreateChildLogger(string loggerName) throw new NotImplementedException("Creating child loggers for Serilog is not supported"); } - public void Debug(string message, Exception exception) + public void Trace(string message, Exception exception) + { + if (IsTraceEnabled) + { + Logger.Debug(exception, message); + } + } + + public void Trace(Func messageFactory) + { + if (IsTraceEnabled) + { + Logger.Debug(messageFactory.Invoke()); + } + } + + public void Trace(string message) + { + if (IsTraceEnabled) + { + Logger.Debug(message); + } + } + + public void TraceFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args) + { + if (IsTraceEnabled) + { + //TODO: This honours the formatProvider rather than passing through args for structured logging + Logger.Debug(exception, string.Format(formatProvider, format, args)); + } + } + + public void TraceFormat(IFormatProvider formatProvider, string format, params object[] args) + { + if (IsTraceEnabled) + { + //TODO: This honours the formatProvider rather than passing through args for structured logging + Logger.Debug(string.Format(formatProvider, format, args)); + } + } + + public void TraceFormat(Exception exception, string format, params object[] args) + { + if (IsTraceEnabled) + { + Logger.Debug(exception, format, args); + } + } + + public void TraceFormat(string format, params object[] args) + { + if (IsTraceEnabled) + { + Logger.Debug(format, args); + } + } + + public void Debug(string message, Exception exception) { if (IsDebugEnabled) { diff --git a/src/Castle.Services.Logging.log4netIntegration/Log4netLogger.cs b/src/Castle.Services.Logging.log4netIntegration/Log4netLogger.cs index dc00699ecc..8deffb3180 100644 --- a/src/Castle.Services.Logging.log4netIntegration/Log4netLogger.cs +++ b/src/Castle.Services.Logging.log4netIntegration/Log4netLogger.cs @@ -46,6 +46,11 @@ internal Log4netLogger(ILog log, Log4netFactory factory) : this(log.Logger, fact { } + public bool IsTraceEnabled + { + get { return Logger.IsEnabledFor(Level.Trace); } + } + public bool IsDebugEnabled { get { return Logger.IsEnabledFor(Level.Debug); } @@ -85,6 +90,62 @@ public virtual Castle.Core.Logging.ILogger CreateChildLogger(String name) return Factory.Create(Logger.Name + "." + name); } + public void Trace(String message) + { + if (IsTraceEnabled) + { + Logger.Log(declaringType, Level.Trace, message, null); + } + } + + public void Trace(Func messageFactory) + { + if (IsTraceEnabled) + { + Logger.Log(declaringType, Level.Trace, messageFactory.Invoke(), null); + } + } + + public void Trace(String message, Exception exception) + { + if (IsTraceEnabled) + { + Logger.Log(declaringType, Level.Trace, message, exception); + } + } + + public void TraceFormat(String format, params Object[] args) + { + if (IsTraceEnabled) + { + Logger.Log(declaringType, Level.Trace, new SystemStringFormat(CultureInfo.InvariantCulture, format, args), null); + } + } + + public void TraceFormat(Exception exception, String format, params Object[] args) + { + if (IsTraceEnabled) + { + Logger.Log(declaringType, Level.Trace, new SystemStringFormat(CultureInfo.InvariantCulture, format, args), exception); + } + } + + public void TraceFormat(IFormatProvider formatProvider, String format, params Object[] args) + { + if (IsTraceEnabled) + { + Logger.Log(declaringType, Level.Trace, new SystemStringFormat(formatProvider, format, args), null); + } + } + + public void TraceFormat(Exception exception, IFormatProvider formatProvider, String format, params Object[] args) + { + if (IsTraceEnabled) + { + Logger.Log(declaringType, Level.Trace, new SystemStringFormat(formatProvider, format, args), exception); + } + } + public void Debug(String message) { if (IsDebugEnabled) From 9a78afd41ba8dd2677423b6e9589da87912d0f05 Mon Sep 17 00:00:00 2001 From: Frode Nilsen Date: Thu, 16 Aug 2018 14:34:25 +0200 Subject: [PATCH 02/15] Unified implementation to same approach --- .../Core/Logging/LevelFilteredLogger.cs | 210 ++++++------------ .../NLogLogger.cs | 177 +++++++++++---- 2 files changed, 201 insertions(+), 186 deletions(-) diff --git a/src/Castle.Core/Core/Logging/LevelFilteredLogger.cs b/src/Castle.Core/Core/Logging/LevelFilteredLogger.cs index 13d272f5af..25fb839011 100644 --- a/src/Castle.Core/Core/Logging/LevelFilteredLogger.cs +++ b/src/Castle.Core/Core/Logging/LevelFilteredLogger.cs @@ -204,22 +204,18 @@ public void TraceFormat(Exception exception, IFormatProvider formatProvider, str /// The message to log public void Debug(string message) { - if (!IsDebugEnabled) + if (IsDebugEnabled) { - return; + Log(LoggerLevel.Debug, message, null); } - - Log(LoggerLevel.Debug, message, null); } public void Debug(Func messageFactory) { - if (!IsDebugEnabled) + if (IsDebugEnabled) { - return; + Log(LoggerLevel.Debug, messageFactory.Invoke(), null); } - - Log(LoggerLevel.Debug, messageFactory.Invoke(), null); } /// @@ -229,12 +225,10 @@ public void Debug(Func messageFactory) /// The message to log public void Debug(string message, Exception exception) { - if (!IsDebugEnabled) + if (IsDebugEnabled) { - return; + Log(LoggerLevel.Debug, message, exception); } - - Log(LoggerLevel.Debug, message, exception); } /// @@ -244,12 +238,10 @@ public void Debug(string message, Exception exception) /// Format arguments for the message to log public void DebugFormat(string format, params object[] args) { - if (!IsDebugEnabled) + if (IsDebugEnabled) { - return; + Log(LoggerLevel.Debug, String.Format(CultureInfo.CurrentCulture, format, args), null); } - - Log(LoggerLevel.Debug, String.Format(CultureInfo.CurrentCulture, format, args), null); } /// @@ -260,12 +252,10 @@ public void DebugFormat(string format, params object[] args) /// Format arguments for the message to log public void DebugFormat(Exception exception, string format, params object[] args) { - if (!IsDebugEnabled) + if (IsDebugEnabled) { - return; + Log(LoggerLevel.Debug, String.Format(CultureInfo.CurrentCulture, format, args), exception); } - - Log(LoggerLevel.Debug, String.Format(CultureInfo.CurrentCulture, format, args), exception); } /// @@ -276,12 +266,10 @@ public void DebugFormat(Exception exception, string format, params object[] args /// Format arguments for the message to log public void DebugFormat(IFormatProvider formatProvider, string format, params object[] args) { - if (!IsDebugEnabled) + if (IsDebugEnabled) { - return; + Log(LoggerLevel.Debug, String.Format(formatProvider, format, args), null); } - - Log(LoggerLevel.Debug, String.Format(formatProvider, format, args), null); } /// @@ -293,12 +281,10 @@ public void DebugFormat(IFormatProvider formatProvider, string format, params ob /// Format arguments for the message to log public void DebugFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args) { - if (!IsDebugEnabled) + if (IsDebugEnabled) { - return; + Log(LoggerLevel.Debug, String.Format(formatProvider, format, args), exception); } - - Log(LoggerLevel.Debug, String.Format(formatProvider, format, args), exception); } #endregion @@ -311,22 +297,18 @@ public void DebugFormat(Exception exception, IFormatProvider formatProvider, str /// The message to log public void Info(string message) { - if (!IsInfoEnabled) + if (IsInfoEnabled) { - return; + Log(LoggerLevel.Info, message, null); } - - Log(LoggerLevel.Info, message, null); } public void Info(Func messageFactory) { - if (!IsInfoEnabled) + if (IsInfoEnabled) { - return; + Log(LoggerLevel.Info, messageFactory.Invoke(), null); } - - Log(LoggerLevel.Info, messageFactory.Invoke(), null); } /// @@ -336,12 +318,10 @@ public void Info(Func messageFactory) /// The message to log public void Info(string message, Exception exception) { - if (!IsInfoEnabled) + if (IsInfoEnabled) { - return; + Log(LoggerLevel.Info, message, exception); } - - Log(LoggerLevel.Info, message, exception); } /// @@ -351,12 +331,10 @@ public void Info(string message, Exception exception) /// Format arguments for the message to log public void InfoFormat(string format, params object[] args) { - if (!IsInfoEnabled) + if (IsInfoEnabled) { - return; + Log(LoggerLevel.Info, String.Format(CultureInfo.CurrentCulture, format, args), null); } - - Log(LoggerLevel.Info, String.Format(CultureInfo.CurrentCulture, format, args), null); } /// @@ -367,12 +345,10 @@ public void InfoFormat(string format, params object[] args) /// Format arguments for the message to log public void InfoFormat(Exception exception, string format, params object[] args) { - if (!IsInfoEnabled) + if (IsInfoEnabled) { - return; + Log(LoggerLevel.Info, String.Format(CultureInfo.CurrentCulture, format, args), exception); } - - Log(LoggerLevel.Info, String.Format(CultureInfo.CurrentCulture, format, args), exception); } /// @@ -383,12 +359,10 @@ public void InfoFormat(Exception exception, string format, params object[] args) /// Format arguments for the message to log public void InfoFormat(IFormatProvider formatProvider, string format, params object[] args) { - if (!IsInfoEnabled) + if (IsInfoEnabled) { - return; + Log(LoggerLevel.Info, String.Format(formatProvider, format, args), null); } - - Log(LoggerLevel.Info, String.Format(formatProvider, format, args), null); } /// @@ -400,12 +374,10 @@ public void InfoFormat(IFormatProvider formatProvider, string format, params obj /// Format arguments for the message to log public void InfoFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args) { - if (!IsInfoEnabled) + if (IsInfoEnabled) { - return; + Log(LoggerLevel.Info, String.Format(formatProvider, format, args), exception); } - - Log(LoggerLevel.Info, String.Format(formatProvider, format, args), exception); } #endregion @@ -418,22 +390,18 @@ public void InfoFormat(Exception exception, IFormatProvider formatProvider, stri /// The message to log public void Warn(string message) { - if (!IsWarnEnabled) + if (IsWarnEnabled) { - return; + Log(LoggerLevel.Warn, message, null); } - - Log(LoggerLevel.Warn, message, null); } public void Warn(Func messageFactory) { - if (!IsWarnEnabled) + if (IsWarnEnabled) { - return; + Log(LoggerLevel.Warn, messageFactory.Invoke(), null); } - - Log(LoggerLevel.Warn, messageFactory.Invoke(), null); } /// @@ -443,12 +411,10 @@ public void Warn(Func messageFactory) /// The message to log public void Warn(string message, Exception exception) { - if (!IsWarnEnabled) + if (IsWarnEnabled) { - return; + Log(LoggerLevel.Warn, message, exception); } - - Log(LoggerLevel.Warn, message, exception); } /// @@ -458,12 +424,10 @@ public void Warn(string message, Exception exception) /// Format arguments for the message to log public void WarnFormat(string format, params object[] args) { - if (!IsWarnEnabled) + if (IsWarnEnabled) { - return; + Log(LoggerLevel.Warn, String.Format(CultureInfo.CurrentCulture, format, args), null); } - - Log(LoggerLevel.Warn, String.Format(CultureInfo.CurrentCulture, format, args), null); } /// @@ -474,12 +438,10 @@ public void WarnFormat(string format, params object[] args) /// Format arguments for the message to log public void WarnFormat(Exception exception, string format, params object[] args) { - if (!IsWarnEnabled) + if (IsWarnEnabled) { - return; + Log(LoggerLevel.Warn, String.Format(CultureInfo.CurrentCulture, format, args), exception); } - - Log(LoggerLevel.Warn, String.Format(CultureInfo.CurrentCulture, format, args), exception); } /// @@ -490,12 +452,10 @@ public void WarnFormat(Exception exception, string format, params object[] args) /// Format arguments for the message to log public void WarnFormat(IFormatProvider formatProvider, string format, params object[] args) { - if (!IsWarnEnabled) + if (IsWarnEnabled) { - return; + Log(LoggerLevel.Warn, String.Format(formatProvider, format, args), null); } - - Log(LoggerLevel.Warn, String.Format(formatProvider, format, args), null); } /// @@ -507,12 +467,10 @@ public void WarnFormat(IFormatProvider formatProvider, string format, params obj /// Format arguments for the message to log public void WarnFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args) { - if (!IsWarnEnabled) + if (IsWarnEnabled) { - return; + Log(LoggerLevel.Warn, String.Format(formatProvider, format, args), exception); } - - Log(LoggerLevel.Warn, String.Format(formatProvider, format, args), exception); } #endregion @@ -525,22 +483,18 @@ public void WarnFormat(Exception exception, IFormatProvider formatProvider, stri /// The message to log public void Error(string message) { - if (!IsErrorEnabled) + if (IsErrorEnabled) { - return; + Log(LoggerLevel.Error, message, null); } - - Log(LoggerLevel.Error, message, null); } public void Error(Func messageFactory) { - if (!IsErrorEnabled) + if (IsErrorEnabled) { - return; + Log(LoggerLevel.Error, messageFactory.Invoke(), null); } - - Log(LoggerLevel.Error, messageFactory.Invoke(), null); } /// @@ -550,12 +504,10 @@ public void Error(Func messageFactory) /// The message to log public void Error(string message, Exception exception) { - if (!IsErrorEnabled) + if (IsErrorEnabled) { - return; + Log(LoggerLevel.Error, message, exception); } - - Log(LoggerLevel.Error, message, exception); } /// @@ -565,12 +517,10 @@ public void Error(string message, Exception exception) /// Format arguments for the message to log public void ErrorFormat(string format, params object[] args) { - if (!IsErrorEnabled) + if (IsErrorEnabled) { - return; + Log(LoggerLevel.Error, String.Format(CultureInfo.CurrentCulture, format, args), null); } - - Log(LoggerLevel.Error, String.Format(CultureInfo.CurrentCulture, format, args), null); } /// @@ -581,12 +531,10 @@ public void ErrorFormat(string format, params object[] args) /// Format arguments for the message to log public void ErrorFormat(Exception exception, string format, params object[] args) { - if (!IsErrorEnabled) + if (IsErrorEnabled) { - return; + Log(LoggerLevel.Error, String.Format(CultureInfo.CurrentCulture, format, args), exception); } - - Log(LoggerLevel.Error, String.Format(CultureInfo.CurrentCulture, format, args), exception); } /// @@ -597,12 +545,10 @@ public void ErrorFormat(Exception exception, string format, params object[] args /// Format arguments for the message to log public void ErrorFormat(IFormatProvider formatProvider, string format, params object[] args) { - if (!IsErrorEnabled) + if (IsErrorEnabled) { - return; + Log(LoggerLevel.Error, String.Format(formatProvider, format, args), null); } - - Log(LoggerLevel.Error, String.Format(formatProvider, format, args), null); } /// @@ -614,12 +560,10 @@ public void ErrorFormat(IFormatProvider formatProvider, string format, params ob /// Format arguments for the message to log public void ErrorFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args) { - if (!IsErrorEnabled) + if (IsErrorEnabled) { - return; + Log(LoggerLevel.Error, String.Format(formatProvider, format, args), exception); } - - Log(LoggerLevel.Error, String.Format(formatProvider, format, args), exception); } #endregion @@ -632,22 +576,18 @@ public void ErrorFormat(Exception exception, IFormatProvider formatProvider, str /// The message to log public void Fatal(string message) { - if (!IsFatalEnabled) + if (IsFatalEnabled) { - return; + Log(LoggerLevel.Fatal, message, null); } - - Log(LoggerLevel.Fatal, message, null); } public void Fatal(Func messageFactory) { - if (!IsFatalEnabled) + if (IsFatalEnabled) { - return; + Log(LoggerLevel.Fatal, messageFactory.Invoke(), null); } - - Log(LoggerLevel.Fatal, messageFactory.Invoke(), null); } /// @@ -657,12 +597,10 @@ public void Fatal(Func messageFactory) /// The message to log public void Fatal(string message, Exception exception) { - if (!IsFatalEnabled) + if (IsFatalEnabled) { - return; + Log(LoggerLevel.Fatal, message, exception); } - - Log(LoggerLevel.Fatal, message, exception); } /// @@ -672,12 +610,10 @@ public void Fatal(string message, Exception exception) /// Format arguments for the message to log public void FatalFormat(string format, params object[] args) { - if (!IsFatalEnabled) + if (IsFatalEnabled) { - return; + Log(LoggerLevel.Fatal, String.Format(CultureInfo.CurrentCulture, format, args), null); } - - Log(LoggerLevel.Fatal, String.Format(CultureInfo.CurrentCulture, format, args), null); } /// @@ -688,12 +624,10 @@ public void FatalFormat(string format, params object[] args) /// Format arguments for the message to log public void FatalFormat(Exception exception, string format, params object[] args) { - if (!IsFatalEnabled) + if (IsFatalEnabled) { - return; + Log(LoggerLevel.Fatal, String.Format(CultureInfo.CurrentCulture, format, args), exception); } - - Log(LoggerLevel.Fatal, String.Format(CultureInfo.CurrentCulture, format, args), exception); } /// @@ -704,12 +638,10 @@ public void FatalFormat(Exception exception, string format, params object[] args /// Format arguments for the message to log public void FatalFormat(IFormatProvider formatProvider, string format, params object[] args) { - if (!IsFatalEnabled) + if (IsFatalEnabled) { - return; + Log(LoggerLevel.Fatal, String.Format(formatProvider, format, args), null); } - - Log(LoggerLevel.Fatal, String.Format(formatProvider, format, args), null); } /// @@ -721,12 +653,10 @@ public void FatalFormat(IFormatProvider formatProvider, string format, params ob /// Format arguments for the message to log public void FatalFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args) { - if (!IsFatalEnabled) + if (IsFatalEnabled) { - return; + Log(LoggerLevel.Fatal, String.Format(formatProvider, format, args), exception); } - - Log(LoggerLevel.Fatal, String.Format(formatProvider, format, args), exception); } #endregion diff --git a/src/Castle.Services.Logging.NLogIntegration/NLogLogger.cs b/src/Castle.Services.Logging.NLogIntegration/NLogLogger.cs index 14d06992b7..77027fa8b5 100644 --- a/src/Castle.Services.Logging.NLogIntegration/NLogLogger.cs +++ b/src/Castle.Services.Logging.NLogIntegration/NLogLogger.cs @@ -218,7 +218,10 @@ public void TraceFormat(Exception exception, IFormatProvider formatProvider, str /// The message to log public void Debug(string message) { - Log(LogLevel.Debug, message); + if (IsDebugEnabled) + { + Log(LogLevel.Debug, message); + } } /// @@ -229,9 +232,8 @@ public void Debug(Func messageFactory) { if (IsDebugEnabled == false) { - return; + Log(LogLevel.Debug, messageFactory()); } - Log(LogLevel.Debug, messageFactory()); } /// @@ -241,7 +243,10 @@ public void Debug(Func messageFactory) /// The message to log public void Debug(string message, Exception exception) { - Log(LogLevel.Debug, message, exception); + if (IsDebugEnabled) + { + Log(LogLevel.Debug, message, exception); + } } /// @@ -251,7 +256,10 @@ public void Debug(string message, Exception exception) /// Format arguments for the message to log public void DebugFormat(string format, params object[] args) { - Log(LogLevel.Debug, format, args); + if (IsDebugEnabled) + { + Log(LogLevel.Debug, format, args); + } } /// @@ -262,7 +270,10 @@ public void DebugFormat(string format, params object[] args) /// Format arguments for the message to log public void DebugFormat(Exception exception, string format, params object[] args) { - Log(LogLevel.Debug, exception, format, args); + if (IsDebugEnabled) + { + Log(LogLevel.Debug, exception, format, args); + } } /// @@ -273,7 +284,10 @@ public void DebugFormat(Exception exception, string format, params object[] args /// Format arguments for the message to log public void DebugFormat(IFormatProvider formatProvider, string format, params object[] args) { - Log(LogLevel.Debug, formatProvider, format, args); + if (IsDebugEnabled) + { + Log(LogLevel.Debug, formatProvider, format, args); + } } /// @@ -285,7 +299,10 @@ public void DebugFormat(IFormatProvider formatProvider, string format, params ob /// Format arguments for the message to log public void DebugFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args) { - Log(LogLevel.Debug, exception, formatProvider, format, args); + if (IsDebugEnabled) + { + Log(LogLevel.Debug, exception, formatProvider, format, args); + } } /// @@ -294,7 +311,10 @@ public void DebugFormat(Exception exception, IFormatProvider formatProvider, str /// The message to log public void Error(string message) { - Log(LogLevel.Error, message); + if (IsErrorEnabled) + { + Log(LogLevel.Error, message); + } } /// @@ -303,11 +323,10 @@ public void Error(string message) /// Factory constructing lazily the message to log if the level is enabled public void Error(Func messageFactory) { - if (IsErrorEnabled == false) + if (IsErrorEnabled) { - return; + Log(LogLevel.Error, messageFactory()); } - Log(LogLevel.Error, messageFactory()); } /// @@ -317,7 +336,10 @@ public void Error(Func messageFactory) /// The message to log public void Error(string message, Exception exception) { - Log(LogLevel.Error, message, exception); + if (IsErrorEnabled) + { + Log(LogLevel.Error, message, exception); + } } /// @@ -327,7 +349,10 @@ public void Error(string message, Exception exception) /// Format arguments for the message to log public void ErrorFormat(string format, params object[] args) { - Log(LogLevel.Error, format, args); + if (IsErrorEnabled) + { + Log(LogLevel.Error, format, args); + } } /// @@ -338,7 +363,10 @@ public void ErrorFormat(string format, params object[] args) /// Format arguments for the message to log public void ErrorFormat(Exception exception, string format, params object[] args) { - Log(LogLevel.Error, exception, format, args); + if (IsErrorEnabled) + { + Log(LogLevel.Error, exception, format, args); + } } /// @@ -349,7 +377,10 @@ public void ErrorFormat(Exception exception, string format, params object[] args /// Format arguments for the message to log public void ErrorFormat(IFormatProvider formatProvider, string format, params object[] args) { - Log(LogLevel.Error, formatProvider, format, args); + if (IsErrorEnabled) + { + Log(LogLevel.Error, formatProvider, format, args); + } } /// @@ -361,7 +392,10 @@ public void ErrorFormat(IFormatProvider formatProvider, string format, params ob /// Format arguments for the message to log public void ErrorFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args) { - Log(LogLevel.Error, exception, formatProvider, format, args); + if (IsErrorEnabled) + { + Log(LogLevel.Error, exception, formatProvider, format, args); + } } /// @@ -370,7 +404,10 @@ public void ErrorFormat(Exception exception, IFormatProvider formatProvider, str /// The message to log public void Fatal(string message) { - Log(LogLevel.Fatal, message); + if (IsFatalEnabled) + { + Log(LogLevel.Fatal, message); + } } /// @@ -379,11 +416,10 @@ public void Fatal(string message) /// Factory constructing lazily the message to log if the level is enabled public void Fatal(Func messageFactory) { - if (IsFatalEnabled == false) + if (IsFatalEnabled) { - return; + Log(LogLevel.Fatal, messageFactory()); } - Log(LogLevel.Fatal, messageFactory()); } /// @@ -393,7 +429,10 @@ public void Fatal(Func messageFactory) /// The message to log public void Fatal(string message, Exception exception) { - Log(LogLevel.Fatal, message, exception); + if (IsFatalEnabled) + { + Log(LogLevel.Fatal, message, exception); + } } /// @@ -403,7 +442,10 @@ public void Fatal(string message, Exception exception) /// Format arguments for the message to log public void FatalFormat(string format, params object[] args) { - Log(LogLevel.Fatal, format, args); + if (IsFatalEnabled) + { + Log(LogLevel.Fatal, format, args); + } } /// @@ -414,7 +456,10 @@ public void FatalFormat(string format, params object[] args) /// Format arguments for the message to log public void FatalFormat(Exception exception, string format, params object[] args) { - Log(LogLevel.Fatal, exception, format, args); + if (IsFatalEnabled) + { + Log(LogLevel.Fatal, exception, format, args); + } } /// @@ -425,7 +470,10 @@ public void FatalFormat(Exception exception, string format, params object[] args /// Format arguments for the message to log public void FatalFormat(IFormatProvider formatProvider, string format, params object[] args) { - Log(LogLevel.Fatal, formatProvider, format, args); + if (IsFatalEnabled) + { + Log(LogLevel.Fatal, formatProvider, format, args); + } } /// @@ -437,7 +485,10 @@ public void FatalFormat(IFormatProvider formatProvider, string format, params ob /// Format arguments for the message to log public void FatalFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args) { - Log(LogLevel.Fatal, exception, formatProvider, format, args); + if (IsFatalEnabled) + { + Log(LogLevel.Fatal, exception, formatProvider, format, args); + } } /// @@ -446,7 +497,10 @@ public void FatalFormat(Exception exception, IFormatProvider formatProvider, str /// The message to log public void Info(string message) { - Log(LogLevel.Info, message); + if (IsInfoEnabled) + { + Log(LogLevel.Info, message); + } } /// @@ -455,11 +509,10 @@ public void Info(string message) /// Factory constructing lazily the message to log if the level is enabled public void Info(Func messageFactory) { - if (IsInfoEnabled == false) + if (IsInfoEnabled) { - return; + Log(LogLevel.Info, messageFactory()); } - Log(LogLevel.Info, messageFactory()); } /// @@ -469,7 +522,10 @@ public void Info(Func messageFactory) /// The message to log public void Info(string message, Exception exception) { - Log(LogLevel.Info, message, exception); + if (IsInfoEnabled) + { + Log(LogLevel.Info, message, exception); + } } /// @@ -479,7 +535,10 @@ public void Info(string message, Exception exception) /// Format arguments for the message to log public void InfoFormat(string format, params object[] args) { - Log(LogLevel.Info, format, args); + if (IsInfoEnabled) + { + Log(LogLevel.Info, format, args); + } } /// @@ -490,7 +549,10 @@ public void InfoFormat(string format, params object[] args) /// Format arguments for the message to log public void InfoFormat(Exception exception, string format, params object[] args) { - Log(LogLevel.Info, exception, format, args); + if (IsInfoEnabled) + { + Log(LogLevel.Info, exception, format, args); + } } /// @@ -501,7 +563,10 @@ public void InfoFormat(Exception exception, string format, params object[] args) /// Format arguments for the message to log public void InfoFormat(IFormatProvider formatProvider, string format, params object[] args) { - Log(LogLevel.Info, formatProvider, format, args); + if (IsInfoEnabled) + { + Log(LogLevel.Info, formatProvider, format, args); + } } /// @@ -513,7 +578,10 @@ public void InfoFormat(IFormatProvider formatProvider, string format, params obj /// Format arguments for the message to log public void InfoFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args) { - Log(LogLevel.Info, exception, formatProvider, format, args); + if (IsInfoEnabled) + { + Log(LogLevel.Info, exception, formatProvider, format, args); + } } /// @@ -522,7 +590,10 @@ public void InfoFormat(Exception exception, IFormatProvider formatProvider, stri /// The message to log public void Warn(string message) { - Log(LogLevel.Warn, message); + if (IsWarnEnabled) + { + Log(LogLevel.Warn, message); + } } /// @@ -531,11 +602,10 @@ public void Warn(string message) /// Factory constructing lazily the message to log if the level is enabled public void Warn(Func messageFactory) { - if (IsWarnEnabled == false) + if (IsWarnEnabled) { - return; + Log(LogLevel.Warn, messageFactory()); } - Log(LogLevel.Warn, messageFactory()); } /// @@ -545,7 +615,10 @@ public void Warn(Func messageFactory) /// The message to log public void Warn(string message, Exception exception) { - Log(LogLevel.Warn, message, exception); + if (IsWarnEnabled) + { + Log(LogLevel.Warn, message, exception); + } } /// @@ -555,7 +628,10 @@ public void Warn(string message, Exception exception) /// Format arguments for the message to log public void WarnFormat(string format, params object[] args) { - Log(LogLevel.Warn, format, args); + if (IsWarnEnabled) + { + Log(LogLevel.Warn, format, args); + } } /// @@ -566,7 +642,10 @@ public void WarnFormat(string format, params object[] args) /// Format arguments for the message to log public void WarnFormat(Exception exception, string format, params object[] args) { - Log(LogLevel.Warn, exception, format, args); + if (IsWarnEnabled) + { + Log(LogLevel.Warn, exception, format, args); + } } /// @@ -577,7 +656,10 @@ public void WarnFormat(Exception exception, string format, params object[] args) /// Format arguments for the message to log public void WarnFormat(IFormatProvider formatProvider, string format, params object[] args) { - Log(LogLevel.Warn, formatProvider, format, args); + if (IsWarnEnabled) + { + Log(LogLevel.Warn, formatProvider, format, args); + } } /// @@ -589,7 +671,10 @@ public void WarnFormat(IFormatProvider formatProvider, string format, params obj /// Format arguments for the message to log public void WarnFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args) { - Log(LogLevel.Warn, exception, formatProvider, format, args); + if (IsWarnEnabled) + { + Log(LogLevel.Warn, exception, formatProvider, format, args); + } } private void Log(LogLevel logLevel, string message) @@ -631,11 +716,11 @@ private void Log(LogLevel logLevel, IFormatProvider formatProvider, string forma }); } - private void Log(LogLevel logLevel, Exception exceptoin, IFormatProvider formatProvider, string format, object[] args) + private void Log(LogLevel logLevel, Exception exception, IFormatProvider formatProvider, string format, object[] args) { Logger.Log(typeof(NLogLogger), new LogEventInfo(logLevel, Logger.Name, format) { - Exception = exceptoin, + Exception = exception, FormatProvider = formatProvider, Parameters = args }); From 434e118c8141670b49e9c6c60ff9f2b8c74fee0b Mon Sep 17 00:00:00 2001 From: Frode Nilsen Date: Fri, 17 Aug 2018 07:49:29 +0200 Subject: [PATCH 03/15] Utilizing Verbose level in Serilog for Trace logging. --- .../SerilogLogger.cs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Castle.Services.Logging.SerilogIntegration/SerilogLogger.cs b/src/Castle.Services.Logging.SerilogIntegration/SerilogLogger.cs index d748fce747..28feb260fa 100644 --- a/src/Castle.Services.Logging.SerilogIntegration/SerilogLogger.cs +++ b/src/Castle.Services.Logging.SerilogIntegration/SerilogLogger.cs @@ -42,7 +42,7 @@ internal SerilogLogger() { } public bool IsTraceEnabled { - get { return Logger.IsEnabled(LogEventLevel.Debug); } + get { return Logger.IsEnabled(LogEventLevel.Verbose); } } public bool IsDebugEnabled @@ -85,7 +85,7 @@ public void Trace(string message, Exception exception) { if (IsTraceEnabled) { - Logger.Debug(exception, message); + Logger.Verbose(exception, message); } } @@ -93,7 +93,7 @@ public void Trace(Func messageFactory) { if (IsTraceEnabled) { - Logger.Debug(messageFactory.Invoke()); + Logger.Verbose(messageFactory.Invoke()); } } @@ -101,7 +101,7 @@ public void Trace(string message) { if (IsTraceEnabled) { - Logger.Debug(message); + Logger.Verbose(message); } } @@ -110,7 +110,7 @@ public void TraceFormat(Exception exception, IFormatProvider formatProvider, str if (IsTraceEnabled) { //TODO: This honours the formatProvider rather than passing through args for structured logging - Logger.Debug(exception, string.Format(formatProvider, format, args)); + Logger.Verbose(exception, string.Format(formatProvider, format, args)); } } @@ -119,7 +119,7 @@ public void TraceFormat(IFormatProvider formatProvider, string format, params ob if (IsTraceEnabled) { //TODO: This honours the formatProvider rather than passing through args for structured logging - Logger.Debug(string.Format(formatProvider, format, args)); + Logger.Verbose(string.Format(formatProvider, format, args)); } } @@ -127,7 +127,7 @@ public void TraceFormat(Exception exception, string format, params object[] args { if (IsTraceEnabled) { - Logger.Debug(exception, format, args); + Logger.Verbose(exception, format, args); } } @@ -135,7 +135,7 @@ public void TraceFormat(string format, params object[] args) { if (IsTraceEnabled) { - Logger.Debug(format, args); + Logger.Verbose(format, args); } } From 7f446f5118b815bfc5d21b05dc8a2b8f9157356f Mon Sep 17 00:00:00 2001 From: Frode Nilsen Date: Mon, 3 Sep 2018 08:03:56 +0200 Subject: [PATCH 04/15] Reversed change in ConsoleLogger, default level set to Debug. --- src/Castle.Core/Core/Logging/ConsoleLogger.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Castle.Core/Core/Logging/ConsoleLogger.cs b/src/Castle.Core/Core/Logging/ConsoleLogger.cs index 15ebdd2c08..6aedbab178 100644 --- a/src/Castle.Core/Core/Logging/ConsoleLogger.cs +++ b/src/Castle.Core/Core/Logging/ConsoleLogger.cs @@ -32,7 +32,7 @@ public class ConsoleLogger : LevelFilteredLogger /// set to LoggerLevel.Debug and the Name /// set to String.Empty. /// - public ConsoleLogger() : this(String.Empty, LoggerLevel.Trace) + public ConsoleLogger() : this(String.Empty, LoggerLevel.Debug) { } @@ -50,7 +50,7 @@ public ConsoleLogger(LoggerLevel logLevel) : this(String.Empty, logLevel) /// set to LoggerLevel.Debug. /// /// The logs Name. - public ConsoleLogger(String name) : this(name, LoggerLevel.Trace) + public ConsoleLogger(String name) : this(name, LoggerLevel.Debug) { } From 6fdb92e42e230dd9252371ad82cba09949e6dfd3 Mon Sep 17 00:00:00 2001 From: Frode Nilsen Date: Mon, 3 Sep 2018 08:04:20 +0200 Subject: [PATCH 05/15] Added ChangeLog entry. --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c5e7b6c4c0..441bc3c5dd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,11 @@ Deprecations: - `Castle.DynamicProxy.Serialization.CacheMappingsAttribute.ApplyTo` (method) - `Castle.DynamicProxy.Serialization.CacheMappingsAttribute.GetDeserializedMappings` (method) +## 4.3.2 (2018-09-03) + +Enhancements: + - Added Trace level to logging (maps to Verbose in Serilog) + ## 4.3.1 (2018-06-21) Enhancements: From b2c0e4aea5c574767194e648b02dcedad082cbf0 Mon Sep 17 00:00:00 2001 From: Frode Nilsen Date: Tue, 4 Sep 2018 07:38:33 +0200 Subject: [PATCH 06/15] Adjustments per comments (no code change). --- CHANGELOG.md | 7 +- .../SerilogLogger.cs | 678 +++++++++--------- 2 files changed, 342 insertions(+), 343 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 441bc3c5dd..aa1b7b4eba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## Unreleased +Enhancements: + - Added Trace level to logging (maps to Verbose in Serilog) + Deprecations: - The API surrounding `Lock` has been deprecated. This consists of the members listed below. Consider using the Base Class Library's `System.Threading.ReaderWriterLockSlim` instead. (@stakx, #391) - `Castle.Core.Internal.Lock` (class) @@ -17,10 +20,6 @@ Deprecations: - `Castle.DynamicProxy.Serialization.CacheMappingsAttribute.ApplyTo` (method) - `Castle.DynamicProxy.Serialization.CacheMappingsAttribute.GetDeserializedMappings` (method) -## 4.3.2 (2018-09-03) - -Enhancements: - - Added Trace level to logging (maps to Verbose in Serilog) ## 4.3.1 (2018-06-21) diff --git a/src/Castle.Services.Logging.SerilogIntegration/SerilogLogger.cs b/src/Castle.Services.Logging.SerilogIntegration/SerilogLogger.cs index 28feb260fa..64affb519f 100644 --- a/src/Castle.Services.Logging.SerilogIntegration/SerilogLogger.cs +++ b/src/Castle.Services.Logging.SerilogIntegration/SerilogLogger.cs @@ -14,31 +14,31 @@ namespace Castle.Services.Logging.SerilogIntegration { - using System; + using System; - using Serilog; - using Serilog.Events; + using Serilog; + using Serilog.Events; #if FEATURE_SERIALIZATION - [Serializable] + [Serializable] #endif - public class SerilogLogger : + public class SerilogLogger : #if FEATURE_APPDOMAIN - MarshalByRefObject, + MarshalByRefObject, #endif - Castle.Core.Logging.ILogger - { - public SerilogLogger(ILogger logger, SerilogFactory factory) - { - Logger = logger; - Factory = factory; - } + Castle.Core.Logging.ILogger + { + public SerilogLogger(ILogger logger, SerilogFactory factory) + { + Logger = logger; + Factory = factory; + } - internal SerilogLogger() { } + internal SerilogLogger() { } - protected internal ILogger Logger { get; set; } + protected internal ILogger Logger { get; set; } - protected internal SerilogFactory Factory { get; set; } + protected internal SerilogFactory Factory { get; set; } public bool IsTraceEnabled { @@ -46,40 +46,40 @@ public bool IsTraceEnabled } public bool IsDebugEnabled - { - get { return Logger.IsEnabled(LogEventLevel.Debug); } - } - - public bool IsErrorEnabled - { - get { return Logger.IsEnabled(LogEventLevel.Error); } - } - - public bool IsFatalEnabled - { - get { return Logger.IsEnabled(LogEventLevel.Fatal); } - } - - public bool IsInfoEnabled - { - get { return Logger.IsEnabled(LogEventLevel.Information); } - } - - public bool IsWarnEnabled - { - get { return Logger.IsEnabled(LogEventLevel.Warning); } - } - - public override string ToString() - { - return Logger.ToString(); - } - - public Castle.Core.Logging.ILogger CreateChildLogger(string loggerName) - { - // Serilog calls these sub loggers. We might be able to do something here but for now I'm going leave it like this. - throw new NotImplementedException("Creating child loggers for Serilog is not supported"); - } + { + get { return Logger.IsEnabled(LogEventLevel.Debug); } + } + + public bool IsErrorEnabled + { + get { return Logger.IsEnabled(LogEventLevel.Error); } + } + + public bool IsFatalEnabled + { + get { return Logger.IsEnabled(LogEventLevel.Fatal); } + } + + public bool IsInfoEnabled + { + get { return Logger.IsEnabled(LogEventLevel.Information); } + } + + public bool IsWarnEnabled + { + get { return Logger.IsEnabled(LogEventLevel.Warning); } + } + + public override string ToString() + { + return Logger.ToString(); + } + + public Castle.Core.Logging.ILogger CreateChildLogger(string loggerName) + { + // Serilog calls these sub loggers. We might be able to do something here but for now I'm going leave it like this. + throw new NotImplementedException("Creating child loggers for Serilog is not supported"); + } public void Trace(string message, Exception exception) { @@ -140,293 +140,293 @@ public void TraceFormat(string format, params object[] args) } public void Debug(string message, Exception exception) - { - if (IsDebugEnabled) - { - Logger.Debug(exception, message); - } - } - - public void Debug(Func messageFactory) - { - if (IsDebugEnabled) - { - Logger.Debug(messageFactory.Invoke()); - } - } - - public void Debug(string message) - { - if (IsDebugEnabled) - { - Logger.Debug(message); - } - } - - public void DebugFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args) - { - if (IsDebugEnabled) - { - //TODO: This honours the formatProvider rather than passing through args for structured logging - Logger.Debug(exception, string.Format(formatProvider, format, args)); - } - } - - public void DebugFormat(IFormatProvider formatProvider, string format, params object[] args) - { - if (IsDebugEnabled) - { - //TODO: This honours the formatProvider rather than passing through args for structured logging - Logger.Debug(string.Format(formatProvider, format, args)); - } - } - - public void DebugFormat(Exception exception, string format, params object[] args) - { - if (IsDebugEnabled) - { - Logger.Debug(exception, format, args); - } - } - - public void DebugFormat(string format, params object[] args) - { - if (IsDebugEnabled) - { - Logger.Debug(format, args); - } - } - - public void Error(string message, Exception exception) - { - if (IsErrorEnabled) - { - Logger.Error(exception, message); - } - } - - public void Error(Func messageFactory) - { - if (IsErrorEnabled) - { - Logger.Error(messageFactory.Invoke()); - } - } - - public void Error(string message) - { - if (IsErrorEnabled) - { - Logger.Error(message); - } - } - - public void ErrorFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args) - { - if (IsErrorEnabled) - { - //TODO: This honours the formatProvider rather than passing through args for structured logging - Logger.Error(exception, string.Format(formatProvider, format, args)); - } - } - - public void ErrorFormat(IFormatProvider formatProvider, string format, params object[] args) - { - if (IsErrorEnabled) - { - //TODO: This honours the formatProvider rather than passing through args for structured logging - Logger.Error(string.Format(formatProvider, format, args)); - } - } - - public void ErrorFormat(Exception exception, string format, params object[] args) - { - if (IsErrorEnabled) - { - Logger.Error(exception, format, args); - } - } - - public void ErrorFormat(string format, params object[] args) - { - if (IsErrorEnabled) - { - Logger.Error(format, args); - } - } - - public void Fatal(string message, Exception exception) - { - if (IsFatalEnabled) - { - Logger.Fatal(exception, message); - } - } - - public void Fatal(Func messageFactory) - { - if (IsFatalEnabled) - { - Logger.Fatal(messageFactory.Invoke()); - } - } - - public void Fatal(string message) - { - if (IsFatalEnabled) - { - Logger.Fatal(message); - } - } - - public void FatalFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args) - { - if (IsFatalEnabled) - { - //TODO: This honours the formatProvider rather than passing through args for structured logging - Logger.Fatal(exception, string.Format(formatProvider, format, args)); - } - } - - public void FatalFormat(IFormatProvider formatProvider, string format, params object[] args) - { - if (IsFatalEnabled) - { - //TODO: This honours the formatProvider rather than passing through args for structured logging - Logger.Fatal(string.Format(formatProvider, format, args)); - } - } - - public void FatalFormat(Exception exception, string format, params object[] args) - { - if (IsFatalEnabled) - { - Logger.Fatal(exception, format, args); - } - } - - public void FatalFormat(string format, params object[] args) - { - if (IsFatalEnabled) - { - Logger.Fatal(format, args); - } - } - - public void Info(string message, Exception exception) - { - if (IsInfoEnabled) - { - Logger.Information(exception, message); - } - } - - public void Info(Func messageFactory) - { - if (IsInfoEnabled) - { - Logger.Information(messageFactory.Invoke()); - } - } - - public void Info(string message) - { - if (IsInfoEnabled) - { - Logger.Information(message); - } - } - - public void InfoFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args) - { - if (IsInfoEnabled) - { - //TODO: This honours the formatProvider rather than passing through args for structured logging - Logger.Information(exception, string.Format(formatProvider, format, args)); - } - } - - public void InfoFormat(IFormatProvider formatProvider, string format, params object[] args) - { - if (IsInfoEnabled) - { - //TODO: This honours the formatProvider rather than passing through args for structured logging - Logger.Information(string.Format(formatProvider, format, args)); - } - } - - public void InfoFormat(Exception exception, string format, params object[] args) - { - if (IsInfoEnabled) - { - Logger.Information(exception, format, args); - } - } - - public void InfoFormat(string format, params object[] args) - { - if (IsInfoEnabled) - { - Logger.Information(format, args); - } - } - - public void Warn(string message, Exception exception) - { - if (IsWarnEnabled) - { - Logger.Warning(exception, message); - } - } - - public void Warn(Func messageFactory) - { - if (IsWarnEnabled) - { - Logger.Warning(messageFactory.Invoke()); - } - } - - public void Warn(string message) - { - if (IsWarnEnabled) - { - Logger.Warning(message); - } - } - - public void WarnFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args) - { - if (IsWarnEnabled) - { - //TODO: This honours the formatProvider rather than passing through args for structured logging - Logger.Warning(exception, string.Format(formatProvider, format, args)); - } - } - - public void WarnFormat(IFormatProvider formatProvider, string format, params object[] args) - { - if (IsWarnEnabled) - { - //TODO: This honours the formatProvider rather than passing through args for structured logging - Logger.Warning(string.Format(formatProvider, format, args)); - } - } - - public void WarnFormat(Exception exception, string format, params object[] args) - { - if (IsWarnEnabled) - { - Logger.Warning(exception, format, args); - } - } - - public void WarnFormat(string format, params object[] args) - { - if (IsWarnEnabled) - { - Logger.Warning(format, args); - } - } - } + { + if (IsDebugEnabled) + { + Logger.Debug(exception, message); + } + } + + public void Debug(Func messageFactory) + { + if (IsDebugEnabled) + { + Logger.Debug(messageFactory.Invoke()); + } + } + + public void Debug(string message) + { + if (IsDebugEnabled) + { + Logger.Debug(message); + } + } + + public void DebugFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args) + { + if (IsDebugEnabled) + { + //TODO: This honours the formatProvider rather than passing through args for structured logging + Logger.Debug(exception, string.Format(formatProvider, format, args)); + } + } + + public void DebugFormat(IFormatProvider formatProvider, string format, params object[] args) + { + if (IsDebugEnabled) + { + //TODO: This honours the formatProvider rather than passing through args for structured logging + Logger.Debug(string.Format(formatProvider, format, args)); + } + } + + public void DebugFormat(Exception exception, string format, params object[] args) + { + if (IsDebugEnabled) + { + Logger.Debug(exception, format, args); + } + } + + public void DebugFormat(string format, params object[] args) + { + if (IsDebugEnabled) + { + Logger.Debug(format, args); + } + } + + public void Error(string message, Exception exception) + { + if (IsErrorEnabled) + { + Logger.Error(exception, message); + } + } + + public void Error(Func messageFactory) + { + if (IsErrorEnabled) + { + Logger.Error(messageFactory.Invoke()); + } + } + + public void Error(string message) + { + if (IsErrorEnabled) + { + Logger.Error(message); + } + } + + public void ErrorFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args) + { + if (IsErrorEnabled) + { + //TODO: This honours the formatProvider rather than passing through args for structured logging + Logger.Error(exception, string.Format(formatProvider, format, args)); + } + } + + public void ErrorFormat(IFormatProvider formatProvider, string format, params object[] args) + { + if (IsErrorEnabled) + { + //TODO: This honours the formatProvider rather than passing through args for structured logging + Logger.Error(string.Format(formatProvider, format, args)); + } + } + + public void ErrorFormat(Exception exception, string format, params object[] args) + { + if (IsErrorEnabled) + { + Logger.Error(exception, format, args); + } + } + + public void ErrorFormat(string format, params object[] args) + { + if (IsErrorEnabled) + { + Logger.Error(format, args); + } + } + + public void Fatal(string message, Exception exception) + { + if (IsFatalEnabled) + { + Logger.Fatal(exception, message); + } + } + + public void Fatal(Func messageFactory) + { + if (IsFatalEnabled) + { + Logger.Fatal(messageFactory.Invoke()); + } + } + + public void Fatal(string message) + { + if (IsFatalEnabled) + { + Logger.Fatal(message); + } + } + + public void FatalFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args) + { + if (IsFatalEnabled) + { + //TODO: This honours the formatProvider rather than passing through args for structured logging + Logger.Fatal(exception, string.Format(formatProvider, format, args)); + } + } + + public void FatalFormat(IFormatProvider formatProvider, string format, params object[] args) + { + if (IsFatalEnabled) + { + //TODO: This honours the formatProvider rather than passing through args for structured logging + Logger.Fatal(string.Format(formatProvider, format, args)); + } + } + + public void FatalFormat(Exception exception, string format, params object[] args) + { + if (IsFatalEnabled) + { + Logger.Fatal(exception, format, args); + } + } + + public void FatalFormat(string format, params object[] args) + { + if (IsFatalEnabled) + { + Logger.Fatal(format, args); + } + } + + public void Info(string message, Exception exception) + { + if (IsInfoEnabled) + { + Logger.Information(exception, message); + } + } + + public void Info(Func messageFactory) + { + if (IsInfoEnabled) + { + Logger.Information(messageFactory.Invoke()); + } + } + + public void Info(string message) + { + if (IsInfoEnabled) + { + Logger.Information(message); + } + } + + public void InfoFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args) + { + if (IsInfoEnabled) + { + //TODO: This honours the formatProvider rather than passing through args for structured logging + Logger.Information(exception, string.Format(formatProvider, format, args)); + } + } + + public void InfoFormat(IFormatProvider formatProvider, string format, params object[] args) + { + if (IsInfoEnabled) + { + //TODO: This honours the formatProvider rather than passing through args for structured logging + Logger.Information(string.Format(formatProvider, format, args)); + } + } + + public void InfoFormat(Exception exception, string format, params object[] args) + { + if (IsInfoEnabled) + { + Logger.Information(exception, format, args); + } + } + + public void InfoFormat(string format, params object[] args) + { + if (IsInfoEnabled) + { + Logger.Information(format, args); + } + } + + public void Warn(string message, Exception exception) + { + if (IsWarnEnabled) + { + Logger.Warning(exception, message); + } + } + + public void Warn(Func messageFactory) + { + if (IsWarnEnabled) + { + Logger.Warning(messageFactory.Invoke()); + } + } + + public void Warn(string message) + { + if (IsWarnEnabled) + { + Logger.Warning(message); + } + } + + public void WarnFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args) + { + if (IsWarnEnabled) + { + //TODO: This honours the formatProvider rather than passing through args for structured logging + Logger.Warning(exception, string.Format(formatProvider, format, args)); + } + } + + public void WarnFormat(IFormatProvider formatProvider, string format, params object[] args) + { + if (IsWarnEnabled) + { + //TODO: This honours the formatProvider rather than passing through args for structured logging + Logger.Warning(string.Format(formatProvider, format, args)); + } + } + + public void WarnFormat(Exception exception, string format, params object[] args) + { + if (IsWarnEnabled) + { + Logger.Warning(exception, format, args); + } + } + + public void WarnFormat(string format, params object[] args) + { + if (IsWarnEnabled) + { + Logger.Warning(format, args); + } + } + } } \ No newline at end of file From cc4315aac4c0f71ed9d91a6cf2ee42fb80d440b7 Mon Sep 17 00:00:00 2001 From: Frode Nilsen Date: Tue, 4 Sep 2018 11:53:27 +0200 Subject: [PATCH 07/15] Revert "Unified implementation to same approach" This reverts commit 9a78afd41ba8dd2677423b6e9589da87912d0f05. --- .../Core/Logging/LevelFilteredLogger.cs | 210 ++++++++++++------ .../NLogLogger.cs | 177 ++++----------- 2 files changed, 186 insertions(+), 201 deletions(-) diff --git a/src/Castle.Core/Core/Logging/LevelFilteredLogger.cs b/src/Castle.Core/Core/Logging/LevelFilteredLogger.cs index 25fb839011..13d272f5af 100644 --- a/src/Castle.Core/Core/Logging/LevelFilteredLogger.cs +++ b/src/Castle.Core/Core/Logging/LevelFilteredLogger.cs @@ -204,18 +204,22 @@ public void TraceFormat(Exception exception, IFormatProvider formatProvider, str /// The message to log public void Debug(string message) { - if (IsDebugEnabled) + if (!IsDebugEnabled) { - Log(LoggerLevel.Debug, message, null); + return; } + + Log(LoggerLevel.Debug, message, null); } public void Debug(Func messageFactory) { - if (IsDebugEnabled) + if (!IsDebugEnabled) { - Log(LoggerLevel.Debug, messageFactory.Invoke(), null); + return; } + + Log(LoggerLevel.Debug, messageFactory.Invoke(), null); } /// @@ -225,10 +229,12 @@ public void Debug(Func messageFactory) /// The message to log public void Debug(string message, Exception exception) { - if (IsDebugEnabled) + if (!IsDebugEnabled) { - Log(LoggerLevel.Debug, message, exception); + return; } + + Log(LoggerLevel.Debug, message, exception); } /// @@ -238,10 +244,12 @@ public void Debug(string message, Exception exception) /// Format arguments for the message to log public void DebugFormat(string format, params object[] args) { - if (IsDebugEnabled) + if (!IsDebugEnabled) { - Log(LoggerLevel.Debug, String.Format(CultureInfo.CurrentCulture, format, args), null); + return; } + + Log(LoggerLevel.Debug, String.Format(CultureInfo.CurrentCulture, format, args), null); } /// @@ -252,10 +260,12 @@ public void DebugFormat(string format, params object[] args) /// Format arguments for the message to log public void DebugFormat(Exception exception, string format, params object[] args) { - if (IsDebugEnabled) + if (!IsDebugEnabled) { - Log(LoggerLevel.Debug, String.Format(CultureInfo.CurrentCulture, format, args), exception); + return; } + + Log(LoggerLevel.Debug, String.Format(CultureInfo.CurrentCulture, format, args), exception); } /// @@ -266,10 +276,12 @@ public void DebugFormat(Exception exception, string format, params object[] args /// Format arguments for the message to log public void DebugFormat(IFormatProvider formatProvider, string format, params object[] args) { - if (IsDebugEnabled) + if (!IsDebugEnabled) { - Log(LoggerLevel.Debug, String.Format(formatProvider, format, args), null); + return; } + + Log(LoggerLevel.Debug, String.Format(formatProvider, format, args), null); } /// @@ -281,10 +293,12 @@ public void DebugFormat(IFormatProvider formatProvider, string format, params ob /// Format arguments for the message to log public void DebugFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args) { - if (IsDebugEnabled) + if (!IsDebugEnabled) { - Log(LoggerLevel.Debug, String.Format(formatProvider, format, args), exception); + return; } + + Log(LoggerLevel.Debug, String.Format(formatProvider, format, args), exception); } #endregion @@ -297,18 +311,22 @@ public void DebugFormat(Exception exception, IFormatProvider formatProvider, str /// The message to log public void Info(string message) { - if (IsInfoEnabled) + if (!IsInfoEnabled) { - Log(LoggerLevel.Info, message, null); + return; } + + Log(LoggerLevel.Info, message, null); } public void Info(Func messageFactory) { - if (IsInfoEnabled) + if (!IsInfoEnabled) { - Log(LoggerLevel.Info, messageFactory.Invoke(), null); + return; } + + Log(LoggerLevel.Info, messageFactory.Invoke(), null); } /// @@ -318,10 +336,12 @@ public void Info(Func messageFactory) /// The message to log public void Info(string message, Exception exception) { - if (IsInfoEnabled) + if (!IsInfoEnabled) { - Log(LoggerLevel.Info, message, exception); + return; } + + Log(LoggerLevel.Info, message, exception); } /// @@ -331,10 +351,12 @@ public void Info(string message, Exception exception) /// Format arguments for the message to log public void InfoFormat(string format, params object[] args) { - if (IsInfoEnabled) + if (!IsInfoEnabled) { - Log(LoggerLevel.Info, String.Format(CultureInfo.CurrentCulture, format, args), null); + return; } + + Log(LoggerLevel.Info, String.Format(CultureInfo.CurrentCulture, format, args), null); } /// @@ -345,10 +367,12 @@ public void InfoFormat(string format, params object[] args) /// Format arguments for the message to log public void InfoFormat(Exception exception, string format, params object[] args) { - if (IsInfoEnabled) + if (!IsInfoEnabled) { - Log(LoggerLevel.Info, String.Format(CultureInfo.CurrentCulture, format, args), exception); + return; } + + Log(LoggerLevel.Info, String.Format(CultureInfo.CurrentCulture, format, args), exception); } /// @@ -359,10 +383,12 @@ public void InfoFormat(Exception exception, string format, params object[] args) /// Format arguments for the message to log public void InfoFormat(IFormatProvider formatProvider, string format, params object[] args) { - if (IsInfoEnabled) + if (!IsInfoEnabled) { - Log(LoggerLevel.Info, String.Format(formatProvider, format, args), null); + return; } + + Log(LoggerLevel.Info, String.Format(formatProvider, format, args), null); } /// @@ -374,10 +400,12 @@ public void InfoFormat(IFormatProvider formatProvider, string format, params obj /// Format arguments for the message to log public void InfoFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args) { - if (IsInfoEnabled) + if (!IsInfoEnabled) { - Log(LoggerLevel.Info, String.Format(formatProvider, format, args), exception); + return; } + + Log(LoggerLevel.Info, String.Format(formatProvider, format, args), exception); } #endregion @@ -390,18 +418,22 @@ public void InfoFormat(Exception exception, IFormatProvider formatProvider, stri /// The message to log public void Warn(string message) { - if (IsWarnEnabled) + if (!IsWarnEnabled) { - Log(LoggerLevel.Warn, message, null); + return; } + + Log(LoggerLevel.Warn, message, null); } public void Warn(Func messageFactory) { - if (IsWarnEnabled) + if (!IsWarnEnabled) { - Log(LoggerLevel.Warn, messageFactory.Invoke(), null); + return; } + + Log(LoggerLevel.Warn, messageFactory.Invoke(), null); } /// @@ -411,10 +443,12 @@ public void Warn(Func messageFactory) /// The message to log public void Warn(string message, Exception exception) { - if (IsWarnEnabled) + if (!IsWarnEnabled) { - Log(LoggerLevel.Warn, message, exception); + return; } + + Log(LoggerLevel.Warn, message, exception); } /// @@ -424,10 +458,12 @@ public void Warn(string message, Exception exception) /// Format arguments for the message to log public void WarnFormat(string format, params object[] args) { - if (IsWarnEnabled) + if (!IsWarnEnabled) { - Log(LoggerLevel.Warn, String.Format(CultureInfo.CurrentCulture, format, args), null); + return; } + + Log(LoggerLevel.Warn, String.Format(CultureInfo.CurrentCulture, format, args), null); } /// @@ -438,10 +474,12 @@ public void WarnFormat(string format, params object[] args) /// Format arguments for the message to log public void WarnFormat(Exception exception, string format, params object[] args) { - if (IsWarnEnabled) + if (!IsWarnEnabled) { - Log(LoggerLevel.Warn, String.Format(CultureInfo.CurrentCulture, format, args), exception); + return; } + + Log(LoggerLevel.Warn, String.Format(CultureInfo.CurrentCulture, format, args), exception); } /// @@ -452,10 +490,12 @@ public void WarnFormat(Exception exception, string format, params object[] args) /// Format arguments for the message to log public void WarnFormat(IFormatProvider formatProvider, string format, params object[] args) { - if (IsWarnEnabled) + if (!IsWarnEnabled) { - Log(LoggerLevel.Warn, String.Format(formatProvider, format, args), null); + return; } + + Log(LoggerLevel.Warn, String.Format(formatProvider, format, args), null); } /// @@ -467,10 +507,12 @@ public void WarnFormat(IFormatProvider formatProvider, string format, params obj /// Format arguments for the message to log public void WarnFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args) { - if (IsWarnEnabled) + if (!IsWarnEnabled) { - Log(LoggerLevel.Warn, String.Format(formatProvider, format, args), exception); + return; } + + Log(LoggerLevel.Warn, String.Format(formatProvider, format, args), exception); } #endregion @@ -483,18 +525,22 @@ public void WarnFormat(Exception exception, IFormatProvider formatProvider, stri /// The message to log public void Error(string message) { - if (IsErrorEnabled) + if (!IsErrorEnabled) { - Log(LoggerLevel.Error, message, null); + return; } + + Log(LoggerLevel.Error, message, null); } public void Error(Func messageFactory) { - if (IsErrorEnabled) + if (!IsErrorEnabled) { - Log(LoggerLevel.Error, messageFactory.Invoke(), null); + return; } + + Log(LoggerLevel.Error, messageFactory.Invoke(), null); } /// @@ -504,10 +550,12 @@ public void Error(Func messageFactory) /// The message to log public void Error(string message, Exception exception) { - if (IsErrorEnabled) + if (!IsErrorEnabled) { - Log(LoggerLevel.Error, message, exception); + return; } + + Log(LoggerLevel.Error, message, exception); } /// @@ -517,10 +565,12 @@ public void Error(string message, Exception exception) /// Format arguments for the message to log public void ErrorFormat(string format, params object[] args) { - if (IsErrorEnabled) + if (!IsErrorEnabled) { - Log(LoggerLevel.Error, String.Format(CultureInfo.CurrentCulture, format, args), null); + return; } + + Log(LoggerLevel.Error, String.Format(CultureInfo.CurrentCulture, format, args), null); } /// @@ -531,10 +581,12 @@ public void ErrorFormat(string format, params object[] args) /// Format arguments for the message to log public void ErrorFormat(Exception exception, string format, params object[] args) { - if (IsErrorEnabled) + if (!IsErrorEnabled) { - Log(LoggerLevel.Error, String.Format(CultureInfo.CurrentCulture, format, args), exception); + return; } + + Log(LoggerLevel.Error, String.Format(CultureInfo.CurrentCulture, format, args), exception); } /// @@ -545,10 +597,12 @@ public void ErrorFormat(Exception exception, string format, params object[] args /// Format arguments for the message to log public void ErrorFormat(IFormatProvider formatProvider, string format, params object[] args) { - if (IsErrorEnabled) + if (!IsErrorEnabled) { - Log(LoggerLevel.Error, String.Format(formatProvider, format, args), null); + return; } + + Log(LoggerLevel.Error, String.Format(formatProvider, format, args), null); } /// @@ -560,10 +614,12 @@ public void ErrorFormat(IFormatProvider formatProvider, string format, params ob /// Format arguments for the message to log public void ErrorFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args) { - if (IsErrorEnabled) + if (!IsErrorEnabled) { - Log(LoggerLevel.Error, String.Format(formatProvider, format, args), exception); + return; } + + Log(LoggerLevel.Error, String.Format(formatProvider, format, args), exception); } #endregion @@ -576,18 +632,22 @@ public void ErrorFormat(Exception exception, IFormatProvider formatProvider, str /// The message to log public void Fatal(string message) { - if (IsFatalEnabled) + if (!IsFatalEnabled) { - Log(LoggerLevel.Fatal, message, null); + return; } + + Log(LoggerLevel.Fatal, message, null); } public void Fatal(Func messageFactory) { - if (IsFatalEnabled) + if (!IsFatalEnabled) { - Log(LoggerLevel.Fatal, messageFactory.Invoke(), null); + return; } + + Log(LoggerLevel.Fatal, messageFactory.Invoke(), null); } /// @@ -597,10 +657,12 @@ public void Fatal(Func messageFactory) /// The message to log public void Fatal(string message, Exception exception) { - if (IsFatalEnabled) + if (!IsFatalEnabled) { - Log(LoggerLevel.Fatal, message, exception); + return; } + + Log(LoggerLevel.Fatal, message, exception); } /// @@ -610,10 +672,12 @@ public void Fatal(string message, Exception exception) /// Format arguments for the message to log public void FatalFormat(string format, params object[] args) { - if (IsFatalEnabled) + if (!IsFatalEnabled) { - Log(LoggerLevel.Fatal, String.Format(CultureInfo.CurrentCulture, format, args), null); + return; } + + Log(LoggerLevel.Fatal, String.Format(CultureInfo.CurrentCulture, format, args), null); } /// @@ -624,10 +688,12 @@ public void FatalFormat(string format, params object[] args) /// Format arguments for the message to log public void FatalFormat(Exception exception, string format, params object[] args) { - if (IsFatalEnabled) + if (!IsFatalEnabled) { - Log(LoggerLevel.Fatal, String.Format(CultureInfo.CurrentCulture, format, args), exception); + return; } + + Log(LoggerLevel.Fatal, String.Format(CultureInfo.CurrentCulture, format, args), exception); } /// @@ -638,10 +704,12 @@ public void FatalFormat(Exception exception, string format, params object[] args /// Format arguments for the message to log public void FatalFormat(IFormatProvider formatProvider, string format, params object[] args) { - if (IsFatalEnabled) + if (!IsFatalEnabled) { - Log(LoggerLevel.Fatal, String.Format(formatProvider, format, args), null); + return; } + + Log(LoggerLevel.Fatal, String.Format(formatProvider, format, args), null); } /// @@ -653,10 +721,12 @@ public void FatalFormat(IFormatProvider formatProvider, string format, params ob /// Format arguments for the message to log public void FatalFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args) { - if (IsFatalEnabled) + if (!IsFatalEnabled) { - Log(LoggerLevel.Fatal, String.Format(formatProvider, format, args), exception); + return; } + + Log(LoggerLevel.Fatal, String.Format(formatProvider, format, args), exception); } #endregion diff --git a/src/Castle.Services.Logging.NLogIntegration/NLogLogger.cs b/src/Castle.Services.Logging.NLogIntegration/NLogLogger.cs index 77027fa8b5..14d06992b7 100644 --- a/src/Castle.Services.Logging.NLogIntegration/NLogLogger.cs +++ b/src/Castle.Services.Logging.NLogIntegration/NLogLogger.cs @@ -218,10 +218,7 @@ public void TraceFormat(Exception exception, IFormatProvider formatProvider, str /// The message to log public void Debug(string message) { - if (IsDebugEnabled) - { - Log(LogLevel.Debug, message); - } + Log(LogLevel.Debug, message); } /// @@ -232,8 +229,9 @@ public void Debug(Func messageFactory) { if (IsDebugEnabled == false) { - Log(LogLevel.Debug, messageFactory()); + return; } + Log(LogLevel.Debug, messageFactory()); } /// @@ -243,10 +241,7 @@ public void Debug(Func messageFactory) /// The message to log public void Debug(string message, Exception exception) { - if (IsDebugEnabled) - { - Log(LogLevel.Debug, message, exception); - } + Log(LogLevel.Debug, message, exception); } /// @@ -256,10 +251,7 @@ public void Debug(string message, Exception exception) /// Format arguments for the message to log public void DebugFormat(string format, params object[] args) { - if (IsDebugEnabled) - { - Log(LogLevel.Debug, format, args); - } + Log(LogLevel.Debug, format, args); } /// @@ -270,10 +262,7 @@ public void DebugFormat(string format, params object[] args) /// Format arguments for the message to log public void DebugFormat(Exception exception, string format, params object[] args) { - if (IsDebugEnabled) - { - Log(LogLevel.Debug, exception, format, args); - } + Log(LogLevel.Debug, exception, format, args); } /// @@ -284,10 +273,7 @@ public void DebugFormat(Exception exception, string format, params object[] args /// Format arguments for the message to log public void DebugFormat(IFormatProvider formatProvider, string format, params object[] args) { - if (IsDebugEnabled) - { - Log(LogLevel.Debug, formatProvider, format, args); - } + Log(LogLevel.Debug, formatProvider, format, args); } /// @@ -299,10 +285,7 @@ public void DebugFormat(IFormatProvider formatProvider, string format, params ob /// Format arguments for the message to log public void DebugFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args) { - if (IsDebugEnabled) - { - Log(LogLevel.Debug, exception, formatProvider, format, args); - } + Log(LogLevel.Debug, exception, formatProvider, format, args); } /// @@ -311,10 +294,7 @@ public void DebugFormat(Exception exception, IFormatProvider formatProvider, str /// The message to log public void Error(string message) { - if (IsErrorEnabled) - { - Log(LogLevel.Error, message); - } + Log(LogLevel.Error, message); } /// @@ -323,10 +303,11 @@ public void Error(string message) /// Factory constructing lazily the message to log if the level is enabled public void Error(Func messageFactory) { - if (IsErrorEnabled) + if (IsErrorEnabled == false) { - Log(LogLevel.Error, messageFactory()); + return; } + Log(LogLevel.Error, messageFactory()); } /// @@ -336,10 +317,7 @@ public void Error(Func messageFactory) /// The message to log public void Error(string message, Exception exception) { - if (IsErrorEnabled) - { - Log(LogLevel.Error, message, exception); - } + Log(LogLevel.Error, message, exception); } /// @@ -349,10 +327,7 @@ public void Error(string message, Exception exception) /// Format arguments for the message to log public void ErrorFormat(string format, params object[] args) { - if (IsErrorEnabled) - { - Log(LogLevel.Error, format, args); - } + Log(LogLevel.Error, format, args); } /// @@ -363,10 +338,7 @@ public void ErrorFormat(string format, params object[] args) /// Format arguments for the message to log public void ErrorFormat(Exception exception, string format, params object[] args) { - if (IsErrorEnabled) - { - Log(LogLevel.Error, exception, format, args); - } + Log(LogLevel.Error, exception, format, args); } /// @@ -377,10 +349,7 @@ public void ErrorFormat(Exception exception, string format, params object[] args /// Format arguments for the message to log public void ErrorFormat(IFormatProvider formatProvider, string format, params object[] args) { - if (IsErrorEnabled) - { - Log(LogLevel.Error, formatProvider, format, args); - } + Log(LogLevel.Error, formatProvider, format, args); } /// @@ -392,10 +361,7 @@ public void ErrorFormat(IFormatProvider formatProvider, string format, params ob /// Format arguments for the message to log public void ErrorFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args) { - if (IsErrorEnabled) - { - Log(LogLevel.Error, exception, formatProvider, format, args); - } + Log(LogLevel.Error, exception, formatProvider, format, args); } /// @@ -404,10 +370,7 @@ public void ErrorFormat(Exception exception, IFormatProvider formatProvider, str /// The message to log public void Fatal(string message) { - if (IsFatalEnabled) - { - Log(LogLevel.Fatal, message); - } + Log(LogLevel.Fatal, message); } /// @@ -416,10 +379,11 @@ public void Fatal(string message) /// Factory constructing lazily the message to log if the level is enabled public void Fatal(Func messageFactory) { - if (IsFatalEnabled) + if (IsFatalEnabled == false) { - Log(LogLevel.Fatal, messageFactory()); + return; } + Log(LogLevel.Fatal, messageFactory()); } /// @@ -429,10 +393,7 @@ public void Fatal(Func messageFactory) /// The message to log public void Fatal(string message, Exception exception) { - if (IsFatalEnabled) - { - Log(LogLevel.Fatal, message, exception); - } + Log(LogLevel.Fatal, message, exception); } /// @@ -442,10 +403,7 @@ public void Fatal(string message, Exception exception) /// Format arguments for the message to log public void FatalFormat(string format, params object[] args) { - if (IsFatalEnabled) - { - Log(LogLevel.Fatal, format, args); - } + Log(LogLevel.Fatal, format, args); } /// @@ -456,10 +414,7 @@ public void FatalFormat(string format, params object[] args) /// Format arguments for the message to log public void FatalFormat(Exception exception, string format, params object[] args) { - if (IsFatalEnabled) - { - Log(LogLevel.Fatal, exception, format, args); - } + Log(LogLevel.Fatal, exception, format, args); } /// @@ -470,10 +425,7 @@ public void FatalFormat(Exception exception, string format, params object[] args /// Format arguments for the message to log public void FatalFormat(IFormatProvider formatProvider, string format, params object[] args) { - if (IsFatalEnabled) - { - Log(LogLevel.Fatal, formatProvider, format, args); - } + Log(LogLevel.Fatal, formatProvider, format, args); } /// @@ -485,10 +437,7 @@ public void FatalFormat(IFormatProvider formatProvider, string format, params ob /// Format arguments for the message to log public void FatalFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args) { - if (IsFatalEnabled) - { - Log(LogLevel.Fatal, exception, formatProvider, format, args); - } + Log(LogLevel.Fatal, exception, formatProvider, format, args); } /// @@ -497,10 +446,7 @@ public void FatalFormat(Exception exception, IFormatProvider formatProvider, str /// The message to log public void Info(string message) { - if (IsInfoEnabled) - { - Log(LogLevel.Info, message); - } + Log(LogLevel.Info, message); } /// @@ -509,10 +455,11 @@ public void Info(string message) /// Factory constructing lazily the message to log if the level is enabled public void Info(Func messageFactory) { - if (IsInfoEnabled) + if (IsInfoEnabled == false) { - Log(LogLevel.Info, messageFactory()); + return; } + Log(LogLevel.Info, messageFactory()); } /// @@ -522,10 +469,7 @@ public void Info(Func messageFactory) /// The message to log public void Info(string message, Exception exception) { - if (IsInfoEnabled) - { - Log(LogLevel.Info, message, exception); - } + Log(LogLevel.Info, message, exception); } /// @@ -535,10 +479,7 @@ public void Info(string message, Exception exception) /// Format arguments for the message to log public void InfoFormat(string format, params object[] args) { - if (IsInfoEnabled) - { - Log(LogLevel.Info, format, args); - } + Log(LogLevel.Info, format, args); } /// @@ -549,10 +490,7 @@ public void InfoFormat(string format, params object[] args) /// Format arguments for the message to log public void InfoFormat(Exception exception, string format, params object[] args) { - if (IsInfoEnabled) - { - Log(LogLevel.Info, exception, format, args); - } + Log(LogLevel.Info, exception, format, args); } /// @@ -563,10 +501,7 @@ public void InfoFormat(Exception exception, string format, params object[] args) /// Format arguments for the message to log public void InfoFormat(IFormatProvider formatProvider, string format, params object[] args) { - if (IsInfoEnabled) - { - Log(LogLevel.Info, formatProvider, format, args); - } + Log(LogLevel.Info, formatProvider, format, args); } /// @@ -578,10 +513,7 @@ public void InfoFormat(IFormatProvider formatProvider, string format, params obj /// Format arguments for the message to log public void InfoFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args) { - if (IsInfoEnabled) - { - Log(LogLevel.Info, exception, formatProvider, format, args); - } + Log(LogLevel.Info, exception, formatProvider, format, args); } /// @@ -590,10 +522,7 @@ public void InfoFormat(Exception exception, IFormatProvider formatProvider, stri /// The message to log public void Warn(string message) { - if (IsWarnEnabled) - { - Log(LogLevel.Warn, message); - } + Log(LogLevel.Warn, message); } /// @@ -602,10 +531,11 @@ public void Warn(string message) /// Factory constructing lazily the message to log if the level is enabled public void Warn(Func messageFactory) { - if (IsWarnEnabled) + if (IsWarnEnabled == false) { - Log(LogLevel.Warn, messageFactory()); + return; } + Log(LogLevel.Warn, messageFactory()); } /// @@ -615,10 +545,7 @@ public void Warn(Func messageFactory) /// The message to log public void Warn(string message, Exception exception) { - if (IsWarnEnabled) - { - Log(LogLevel.Warn, message, exception); - } + Log(LogLevel.Warn, message, exception); } /// @@ -628,10 +555,7 @@ public void Warn(string message, Exception exception) /// Format arguments for the message to log public void WarnFormat(string format, params object[] args) { - if (IsWarnEnabled) - { - Log(LogLevel.Warn, format, args); - } + Log(LogLevel.Warn, format, args); } /// @@ -642,10 +566,7 @@ public void WarnFormat(string format, params object[] args) /// Format arguments for the message to log public void WarnFormat(Exception exception, string format, params object[] args) { - if (IsWarnEnabled) - { - Log(LogLevel.Warn, exception, format, args); - } + Log(LogLevel.Warn, exception, format, args); } /// @@ -656,10 +577,7 @@ public void WarnFormat(Exception exception, string format, params object[] args) /// Format arguments for the message to log public void WarnFormat(IFormatProvider formatProvider, string format, params object[] args) { - if (IsWarnEnabled) - { - Log(LogLevel.Warn, formatProvider, format, args); - } + Log(LogLevel.Warn, formatProvider, format, args); } /// @@ -671,10 +589,7 @@ public void WarnFormat(IFormatProvider formatProvider, string format, params obj /// Format arguments for the message to log public void WarnFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args) { - if (IsWarnEnabled) - { - Log(LogLevel.Warn, exception, formatProvider, format, args); - } + Log(LogLevel.Warn, exception, formatProvider, format, args); } private void Log(LogLevel logLevel, string message) @@ -716,11 +631,11 @@ private void Log(LogLevel logLevel, IFormatProvider formatProvider, string forma }); } - private void Log(LogLevel logLevel, Exception exception, IFormatProvider formatProvider, string format, object[] args) + private void Log(LogLevel logLevel, Exception exceptoin, IFormatProvider formatProvider, string format, object[] args) { Logger.Log(typeof(NLogLogger), new LogEventInfo(logLevel, Logger.Name, format) { - Exception = exception, + Exception = exceptoin, FormatProvider = formatProvider, Parameters = args }); From fcec1fd704aa314957cb6144db22237f2c1ff485 Mon Sep 17 00:00:00 2001 From: Frode Nilsen Date: Wed, 5 Sep 2018 09:29:15 +0200 Subject: [PATCH 08/15] Fixed faults in test comments. --- .../Logging/LevelFilteredLoggerTests.cs | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Castle.Core.Tests/Core.Tests/Logging/LevelFilteredLoggerTests.cs b/src/Castle.Core.Tests/Core.Tests/Logging/LevelFilteredLoggerTests.cs index 065823e7bc..445c0d4a82 100644 --- a/src/Castle.Core.Tests/Core.Tests/Logging/LevelFilteredLoggerTests.cs +++ b/src/Castle.Core.Tests/Core.Tests/Logging/LevelFilteredLoggerTests.cs @@ -40,12 +40,12 @@ public void LevelTrace() { logger.Level = LoggerLevel.Trace; - Assert.IsTrue(logger.IsTraceEnabled, "LevelFilteredLogger.IsTraceEnabled is not returning true when the level is Debug"); - Assert.IsTrue(logger.IsDebugEnabled, "LevelFilteredLogger.IsDebugEnabled is not returning true when the level is Debug"); - Assert.IsTrue(logger.IsInfoEnabled, "LevelFilteredLogger.IsInfoEnabled is not returning true when the level is Debug"); - Assert.IsTrue(logger.IsWarnEnabled, "LevelFilteredLogger.IsWarnEnabled is not returning true when the level is Debug"); - Assert.IsTrue(logger.IsErrorEnabled, "LevelFilteredLogger.IsErrorEnabled is not returning true when the level is Debug"); - Assert.IsTrue(logger.IsFatalEnabled, "LevelFilteredLogger.IsFatalErrorEnabled is not returning true when the level is Debug"); + Assert.IsTrue(logger.IsTraceEnabled, "LevelFilteredLogger.IsTraceEnabled is not returning true when the level is Trace"); + Assert.IsTrue(logger.IsDebugEnabled, "LevelFilteredLogger.IsDebugEnabled is not returning true when the level is Trace"); + Assert.IsTrue(logger.IsInfoEnabled, "LevelFilteredLogger.IsInfoEnabled is not returning true when the level is Trace"); + Assert.IsTrue(logger.IsWarnEnabled, "LevelFilteredLogger.IsWarnEnabled is not returning true when the level is Trace"); + Assert.IsTrue(logger.IsErrorEnabled, "LevelFilteredLogger.IsErrorEnabled is not returning true when the level is Trace"); + Assert.IsTrue(logger.IsFatalEnabled, "LevelFilteredLogger.IsFatalErrorEnabled is not returning true when the level is Trace"); } [Test] @@ -66,7 +66,7 @@ public void LevelInfo() { logger.Level = LoggerLevel.Info; - Assert.IsFalse(logger.IsTraceEnabled, "LevelFilteredLogger.IsTraceEnabled is not returning false when the level is Debug"); + Assert.IsFalse(logger.IsTraceEnabled, "LevelFilteredLogger.IsTraceEnabled is not returning false when the level is Info"); Assert.IsFalse(logger.IsDebugEnabled, "LevelFilteredLogger.IsDebugEnabled is not returning false when the level is Info"); Assert.IsTrue(logger.IsWarnEnabled, "LevelFilteredLogger.IsWarnEnabled is not returning true when the level is Info"); Assert.IsTrue(logger.IsInfoEnabled, "LevelFilteredLogger.IsInfoEnabled is not returning true when the level is Info"); @@ -79,7 +79,7 @@ public void LevelWarn() { logger.Level = LoggerLevel.Warn; - Assert.IsFalse(logger.IsTraceEnabled, "LevelFilteredLogger.IsTraceEnabled is not returning false when the level is Debug"); + Assert.IsFalse(logger.IsTraceEnabled, "LevelFilteredLogger.IsTraceEnabled is not returning false when the level is Warn"); Assert.IsFalse(logger.IsDebugEnabled, "LevelFilteredLogger.IsDebugEnabled is not returning false when the level is Warn"); Assert.IsFalse(logger.IsInfoEnabled, "LevelFilteredLogger.IsInfoEnabled is not returning false when the level is Warn"); Assert.IsTrue(logger.IsWarnEnabled, "LevelFilteredLogger.IsWarnEnabled is not returning true when the level is Warn"); @@ -92,7 +92,7 @@ public void LevelError() { logger.Level = LoggerLevel.Error; - Assert.IsFalse(logger.IsTraceEnabled, "LevelFilteredLogger.IsTraceEnabled is not returning false when the level is Debug"); + Assert.IsFalse(logger.IsTraceEnabled, "LevelFilteredLogger.IsTraceEnabled is not returning false when the level is Error"); Assert.IsFalse(logger.IsDebugEnabled, "LevelFilteredLogger.IsDebugEnabled is not returning false when the level is Error"); Assert.IsFalse(logger.IsInfoEnabled, "LevelFilteredLogger.IsInfoEnabled is not returning false when the level is Error"); Assert.IsFalse(logger.IsWarnEnabled, "LevelFilteredLogger.IsWarnEnabled is not returning false when the level is Error"); @@ -105,7 +105,7 @@ public void LevelFatal() { logger.Level = LoggerLevel.Fatal; - Assert.IsFalse(logger.IsTraceEnabled, "LevelFilteredLogger.IsTraceEnabled is not returning false when the level is Debug"); + Assert.IsFalse(logger.IsTraceEnabled, "LevelFilteredLogger.IsTraceEnabled is not returning false when the level is Fatal"); Assert.IsFalse(logger.IsDebugEnabled, "LevelFilteredLogger.IsDebugEnabled is not returning false when the level is Fatal"); Assert.IsFalse(logger.IsInfoEnabled, "LevelFilteredLogger.IsInfoEnabled is not returning false when the level is Fatal"); Assert.IsFalse(logger.IsWarnEnabled, "LevelFilteredLogger.IsWarnEnabled is not returning false when the level is Fatal"); From 42c54106a4c84a5df496d78c106c1906a8a7eba6 Mon Sep 17 00:00:00 2001 From: Frode Nilsen Date: Wed, 5 Sep 2018 09:30:59 +0200 Subject: [PATCH 09/15] Adjusted ChangeLog --- CHANGELOG.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index aa1b7b4eba..0ea51854c8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ ## Unreleased Enhancements: - - Added Trace level to logging (maps to Verbose in Serilog) + - Added Trace level to logging, maps to Verbose in Serilog. (@pi3k14, #404) Deprecations: - The API surrounding `Lock` has been deprecated. This consists of the members listed below. Consider using the Base Class Library's `System.Threading.ReaderWriterLockSlim` instead. (@stakx, #391) @@ -20,7 +20,6 @@ Deprecations: - `Castle.DynamicProxy.Serialization.CacheMappingsAttribute.ApplyTo` (method) - `Castle.DynamicProxy.Serialization.CacheMappingsAttribute.GetDeserializedMappings` (method) - ## 4.3.1 (2018-06-21) Enhancements: From 799d5b3aa5ce535c539eef1a424cb0b61aa40125 Mon Sep 17 00:00:00 2001 From: Frode Nilsen Date: Wed, 5 Sep 2018 09:31:28 +0200 Subject: [PATCH 10/15] Altered to more appropriate exception type --- .../DynamicProxy.Tests/LoggingTestCase.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Castle.Core.Tests/DynamicProxy.Tests/LoggingTestCase.cs b/src/Castle.Core.Tests/DynamicProxy.Tests/LoggingTestCase.cs index 70192b0309..78ca4df786 100644 --- a/src/Castle.Core.Tests/DynamicProxy.Tests/LoggingTestCase.cs +++ b/src/Castle.Core.Tests/DynamicProxy.Tests/LoggingTestCase.cs @@ -170,17 +170,17 @@ public bool RecordedMessage(LoggerLevel level, string message) public void Trace(string message) { - throw new NotImplementedException(); + throw new NotSupportedException(); } public void Trace(Func messageFactory) { - throw new NotImplementedException(); + throw new NotSupportedException(); } public void Trace(string message, Exception exception) { - throw new NotImplementedException(); + throw new NotSupportedException(); } public void TraceFormat(string format, params object[] args) @@ -190,17 +190,17 @@ public void TraceFormat(string format, params object[] args) public void TraceFormat(Exception exception, string format, params object[] args) { - throw new NotImplementedException(); + throw new NotSupportedException(); } public void TraceFormat(IFormatProvider formatProvider, string format, params object[] args) { - throw new NotImplementedException(); + throw new NotSupportedException(); } public void TraceFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args) { - throw new NotImplementedException(); + throw new NotSupportedException(); } public void Debug(string message) From d2925dbd78a88f1ec9d9be2b97bfbbddad5fe210 Mon Sep 17 00:00:00 2001 From: Frode Nilsen Date: Wed, 5 Sep 2018 09:31:52 +0200 Subject: [PATCH 11/15] Added missing XML comment --- src/Castle.Core/Core/Logging/LevelFilteredLogger.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Castle.Core/Core/Logging/LevelFilteredLogger.cs b/src/Castle.Core/Core/Logging/LevelFilteredLogger.cs index 13d272f5af..40a6be8f0b 100644 --- a/src/Castle.Core/Core/Logging/LevelFilteredLogger.cs +++ b/src/Castle.Core/Core/Logging/LevelFilteredLogger.cs @@ -117,6 +117,10 @@ public void Trace(string message) } } + /// + /// Logs a trace message. + /// + /// A functor to create the message public void Trace(Func messageFactory) { if (IsTraceEnabled) From ebba12edfc21b613c8e9a8eff050bb2538b4a034 Mon Sep 17 00:00:00 2001 From: Frode Nilsen Date: Wed, 5 Sep 2018 09:32:48 +0200 Subject: [PATCH 12/15] Reversed change of indentation character --- .../SerilogLogger.cs | 818 +++++++++--------- 1 file changed, 409 insertions(+), 409 deletions(-) diff --git a/src/Castle.Services.Logging.SerilogIntegration/SerilogLogger.cs b/src/Castle.Services.Logging.SerilogIntegration/SerilogLogger.cs index 64affb519f..34fe9ea70d 100644 --- a/src/Castle.Services.Logging.SerilogIntegration/SerilogLogger.cs +++ b/src/Castle.Services.Logging.SerilogIntegration/SerilogLogger.cs @@ -14,419 +14,419 @@ namespace Castle.Services.Logging.SerilogIntegration { - using System; + using System; - using Serilog; - using Serilog.Events; + using Serilog; + using Serilog.Events; #if FEATURE_SERIALIZATION - [Serializable] + [Serializable] #endif - public class SerilogLogger : + public class SerilogLogger : #if FEATURE_APPDOMAIN - MarshalByRefObject, + MarshalByRefObject, #endif - Castle.Core.Logging.ILogger - { - public SerilogLogger(ILogger logger, SerilogFactory factory) - { - Logger = logger; - Factory = factory; - } - - internal SerilogLogger() { } - - protected internal ILogger Logger { get; set; } - - protected internal SerilogFactory Factory { get; set; } - - public bool IsTraceEnabled - { - get { return Logger.IsEnabled(LogEventLevel.Verbose); } - } - - public bool IsDebugEnabled - { - get { return Logger.IsEnabled(LogEventLevel.Debug); } - } - - public bool IsErrorEnabled - { - get { return Logger.IsEnabled(LogEventLevel.Error); } - } - - public bool IsFatalEnabled - { - get { return Logger.IsEnabled(LogEventLevel.Fatal); } - } - - public bool IsInfoEnabled - { - get { return Logger.IsEnabled(LogEventLevel.Information); } - } - - public bool IsWarnEnabled - { - get { return Logger.IsEnabled(LogEventLevel.Warning); } - } - - public override string ToString() - { - return Logger.ToString(); - } - - public Castle.Core.Logging.ILogger CreateChildLogger(string loggerName) - { - // Serilog calls these sub loggers. We might be able to do something here but for now I'm going leave it like this. - throw new NotImplementedException("Creating child loggers for Serilog is not supported"); - } - - public void Trace(string message, Exception exception) - { - if (IsTraceEnabled) - { - Logger.Verbose(exception, message); - } - } - - public void Trace(Func messageFactory) - { - if (IsTraceEnabled) - { - Logger.Verbose(messageFactory.Invoke()); - } - } - - public void Trace(string message) - { - if (IsTraceEnabled) - { - Logger.Verbose(message); - } - } - - public void TraceFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args) - { - if (IsTraceEnabled) - { - //TODO: This honours the formatProvider rather than passing through args for structured logging - Logger.Verbose(exception, string.Format(formatProvider, format, args)); - } - } - - public void TraceFormat(IFormatProvider formatProvider, string format, params object[] args) - { - if (IsTraceEnabled) - { - //TODO: This honours the formatProvider rather than passing through args for structured logging - Logger.Verbose(string.Format(formatProvider, format, args)); - } - } - - public void TraceFormat(Exception exception, string format, params object[] args) - { - if (IsTraceEnabled) - { - Logger.Verbose(exception, format, args); - } - } - - public void TraceFormat(string format, params object[] args) - { - if (IsTraceEnabled) - { - Logger.Verbose(format, args); - } - } - - public void Debug(string message, Exception exception) - { - if (IsDebugEnabled) - { - Logger.Debug(exception, message); - } - } - - public void Debug(Func messageFactory) - { - if (IsDebugEnabled) - { - Logger.Debug(messageFactory.Invoke()); - } - } - - public void Debug(string message) - { - if (IsDebugEnabled) - { - Logger.Debug(message); - } - } - - public void DebugFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args) - { - if (IsDebugEnabled) - { - //TODO: This honours the formatProvider rather than passing through args for structured logging - Logger.Debug(exception, string.Format(formatProvider, format, args)); - } - } - - public void DebugFormat(IFormatProvider formatProvider, string format, params object[] args) - { - if (IsDebugEnabled) - { - //TODO: This honours the formatProvider rather than passing through args for structured logging - Logger.Debug(string.Format(formatProvider, format, args)); - } - } - - public void DebugFormat(Exception exception, string format, params object[] args) - { - if (IsDebugEnabled) - { - Logger.Debug(exception, format, args); - } - } - - public void DebugFormat(string format, params object[] args) - { - if (IsDebugEnabled) - { - Logger.Debug(format, args); - } - } - - public void Error(string message, Exception exception) - { - if (IsErrorEnabled) - { - Logger.Error(exception, message); - } - } - - public void Error(Func messageFactory) - { - if (IsErrorEnabled) - { - Logger.Error(messageFactory.Invoke()); - } - } - - public void Error(string message) - { - if (IsErrorEnabled) - { - Logger.Error(message); - } - } - - public void ErrorFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args) - { - if (IsErrorEnabled) - { - //TODO: This honours the formatProvider rather than passing through args for structured logging - Logger.Error(exception, string.Format(formatProvider, format, args)); - } - } - - public void ErrorFormat(IFormatProvider formatProvider, string format, params object[] args) - { - if (IsErrorEnabled) - { - //TODO: This honours the formatProvider rather than passing through args for structured logging - Logger.Error(string.Format(formatProvider, format, args)); - } - } - - public void ErrorFormat(Exception exception, string format, params object[] args) - { - if (IsErrorEnabled) - { - Logger.Error(exception, format, args); - } - } - - public void ErrorFormat(string format, params object[] args) - { - if (IsErrorEnabled) - { - Logger.Error(format, args); - } - } - - public void Fatal(string message, Exception exception) - { - if (IsFatalEnabled) - { - Logger.Fatal(exception, message); - } - } - - public void Fatal(Func messageFactory) - { - if (IsFatalEnabled) - { - Logger.Fatal(messageFactory.Invoke()); - } - } - - public void Fatal(string message) - { - if (IsFatalEnabled) - { - Logger.Fatal(message); - } - } - - public void FatalFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args) - { - if (IsFatalEnabled) - { - //TODO: This honours the formatProvider rather than passing through args for structured logging - Logger.Fatal(exception, string.Format(formatProvider, format, args)); - } - } - - public void FatalFormat(IFormatProvider formatProvider, string format, params object[] args) - { - if (IsFatalEnabled) - { - //TODO: This honours the formatProvider rather than passing through args for structured logging - Logger.Fatal(string.Format(formatProvider, format, args)); - } - } - - public void FatalFormat(Exception exception, string format, params object[] args) - { - if (IsFatalEnabled) - { - Logger.Fatal(exception, format, args); - } - } - - public void FatalFormat(string format, params object[] args) - { - if (IsFatalEnabled) - { - Logger.Fatal(format, args); - } - } - - public void Info(string message, Exception exception) - { - if (IsInfoEnabled) - { - Logger.Information(exception, message); - } - } - - public void Info(Func messageFactory) - { - if (IsInfoEnabled) - { - Logger.Information(messageFactory.Invoke()); - } - } - - public void Info(string message) - { - if (IsInfoEnabled) - { - Logger.Information(message); - } - } - - public void InfoFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args) - { - if (IsInfoEnabled) - { - //TODO: This honours the formatProvider rather than passing through args for structured logging - Logger.Information(exception, string.Format(formatProvider, format, args)); - } - } - - public void InfoFormat(IFormatProvider formatProvider, string format, params object[] args) - { - if (IsInfoEnabled) - { - //TODO: This honours the formatProvider rather than passing through args for structured logging - Logger.Information(string.Format(formatProvider, format, args)); - } - } - - public void InfoFormat(Exception exception, string format, params object[] args) - { - if (IsInfoEnabled) - { - Logger.Information(exception, format, args); - } - } - - public void InfoFormat(string format, params object[] args) - { - if (IsInfoEnabled) - { - Logger.Information(format, args); - } - } - - public void Warn(string message, Exception exception) - { - if (IsWarnEnabled) - { - Logger.Warning(exception, message); - } - } - - public void Warn(Func messageFactory) - { - if (IsWarnEnabled) - { - Logger.Warning(messageFactory.Invoke()); - } - } - - public void Warn(string message) - { - if (IsWarnEnabled) - { - Logger.Warning(message); - } - } - - public void WarnFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args) - { - if (IsWarnEnabled) - { - //TODO: This honours the formatProvider rather than passing through args for structured logging - Logger.Warning(exception, string.Format(formatProvider, format, args)); - } - } - - public void WarnFormat(IFormatProvider formatProvider, string format, params object[] args) - { - if (IsWarnEnabled) - { - //TODO: This honours the formatProvider rather than passing through args for structured logging - Logger.Warning(string.Format(formatProvider, format, args)); - } - } - - public void WarnFormat(Exception exception, string format, params object[] args) - { - if (IsWarnEnabled) - { - Logger.Warning(exception, format, args); - } - } - - public void WarnFormat(string format, params object[] args) - { - if (IsWarnEnabled) - { - Logger.Warning(format, args); - } - } - } + Castle.Core.Logging.ILogger + { + public SerilogLogger(ILogger logger, SerilogFactory factory) + { + Logger = logger; + Factory = factory; + } + + internal SerilogLogger() { } + + protected internal ILogger Logger { get; set; } + + protected internal SerilogFactory Factory { get; set; } + + public bool IsTraceEnabled + { + get { return Logger.IsEnabled(LogEventLevel.Verbose); } + } + + public bool IsDebugEnabled + { + get { return Logger.IsEnabled(LogEventLevel.Debug); } + } + + public bool IsErrorEnabled + { + get { return Logger.IsEnabled(LogEventLevel.Error); } + } + + public bool IsFatalEnabled + { + get { return Logger.IsEnabled(LogEventLevel.Fatal); } + } + + public bool IsInfoEnabled + { + get { return Logger.IsEnabled(LogEventLevel.Information); } + } + + public bool IsWarnEnabled + { + get { return Logger.IsEnabled(LogEventLevel.Warning); } + } + + public override string ToString() + { + return Logger.ToString(); + } + + public Castle.Core.Logging.ILogger CreateChildLogger(string loggerName) + { + // Serilog calls these sub loggers. We might be able to do something here but for now I'm going leave it like this. + throw new NotImplementedException("Creating child loggers for Serilog is not supported"); + } + + public void Trace(string message, Exception exception) + { + if (IsTraceEnabled) + { + Logger.Verbose(exception, message); + } + } + + public void Trace(Func messageFactory) + { + if (IsTraceEnabled) + { + Logger.Verbose(messageFactory.Invoke()); + } + } + + public void Trace(string message) + { + if (IsTraceEnabled) + { + Logger.Verbose(message); + } + } + + public void TraceFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args) + { + if (IsTraceEnabled) + { + //TODO: This honours the formatProvider rather than passing through args for structured logging + Logger.Verbose(exception, string.Format(formatProvider, format, args)); + } + } + + public void TraceFormat(IFormatProvider formatProvider, string format, params object[] args) + { + if (IsTraceEnabled) + { + //TODO: This honours the formatProvider rather than passing through args for structured logging + Logger.Verbose(string.Format(formatProvider, format, args)); + } + } + + public void TraceFormat(Exception exception, string format, params object[] args) + { + if (IsTraceEnabled) + { + Logger.Verbose(exception, format, args); + } + } + + public void TraceFormat(string format, params object[] args) + { + if (IsTraceEnabled) + { + Logger.Verbose(format, args); + } + } + + public void Debug(string message, Exception exception) + { + if (IsDebugEnabled) + { + Logger.Debug(exception, message); + } + } + + public void Debug(Func messageFactory) + { + if (IsDebugEnabled) + { + Logger.Debug(messageFactory.Invoke()); + } + } + + public void Debug(string message) + { + if (IsDebugEnabled) + { + Logger.Debug(message); + } + } + + public void DebugFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args) + { + if (IsDebugEnabled) + { + //TODO: This honours the formatProvider rather than passing through args for structured logging + Logger.Debug(exception, string.Format(formatProvider, format, args)); + } + } + + public void DebugFormat(IFormatProvider formatProvider, string format, params object[] args) + { + if (IsDebugEnabled) + { + //TODO: This honours the formatProvider rather than passing through args for structured logging + Logger.Debug(string.Format(formatProvider, format, args)); + } + } + + public void DebugFormat(Exception exception, string format, params object[] args) + { + if (IsDebugEnabled) + { + Logger.Debug(exception, format, args); + } + } + + public void DebugFormat(string format, params object[] args) + { + if (IsDebugEnabled) + { + Logger.Debug(format, args); + } + } + + public void Error(string message, Exception exception) + { + if (IsErrorEnabled) + { + Logger.Error(exception, message); + } + } + + public void Error(Func messageFactory) + { + if (IsErrorEnabled) + { + Logger.Error(messageFactory.Invoke()); + } + } + + public void Error(string message) + { + if (IsErrorEnabled) + { + Logger.Error(message); + } + } + + public void ErrorFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args) + { + if (IsErrorEnabled) + { + //TODO: This honours the formatProvider rather than passing through args for structured logging + Logger.Error(exception, string.Format(formatProvider, format, args)); + } + } + + public void ErrorFormat(IFormatProvider formatProvider, string format, params object[] args) + { + if (IsErrorEnabled) + { + //TODO: This honours the formatProvider rather than passing through args for structured logging + Logger.Error(string.Format(formatProvider, format, args)); + } + } + + public void ErrorFormat(Exception exception, string format, params object[] args) + { + if (IsErrorEnabled) + { + Logger.Error(exception, format, args); + } + } + + public void ErrorFormat(string format, params object[] args) + { + if (IsErrorEnabled) + { + Logger.Error(format, args); + } + } + + public void Fatal(string message, Exception exception) + { + if (IsFatalEnabled) + { + Logger.Fatal(exception, message); + } + } + + public void Fatal(Func messageFactory) + { + if (IsFatalEnabled) + { + Logger.Fatal(messageFactory.Invoke()); + } + } + + public void Fatal(string message) + { + if (IsFatalEnabled) + { + Logger.Fatal(message); + } + } + + public void FatalFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args) + { + if (IsFatalEnabled) + { + //TODO: This honours the formatProvider rather than passing through args for structured logging + Logger.Fatal(exception, string.Format(formatProvider, format, args)); + } + } + + public void FatalFormat(IFormatProvider formatProvider, string format, params object[] args) + { + if (IsFatalEnabled) + { + //TODO: This honours the formatProvider rather than passing through args for structured logging + Logger.Fatal(string.Format(formatProvider, format, args)); + } + } + + public void FatalFormat(Exception exception, string format, params object[] args) + { + if (IsFatalEnabled) + { + Logger.Fatal(exception, format, args); + } + } + + public void FatalFormat(string format, params object[] args) + { + if (IsFatalEnabled) + { + Logger.Fatal(format, args); + } + } + + public void Info(string message, Exception exception) + { + if (IsInfoEnabled) + { + Logger.Information(exception, message); + } + } + + public void Info(Func messageFactory) + { + if (IsInfoEnabled) + { + Logger.Information(messageFactory.Invoke()); + } + } + + public void Info(string message) + { + if (IsInfoEnabled) + { + Logger.Information(message); + } + } + + public void InfoFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args) + { + if (IsInfoEnabled) + { + //TODO: This honours the formatProvider rather than passing through args for structured logging + Logger.Information(exception, string.Format(formatProvider, format, args)); + } + } + + public void InfoFormat(IFormatProvider formatProvider, string format, params object[] args) + { + if (IsInfoEnabled) + { + //TODO: This honours the formatProvider rather than passing through args for structured logging + Logger.Information(string.Format(formatProvider, format, args)); + } + } + + public void InfoFormat(Exception exception, string format, params object[] args) + { + if (IsInfoEnabled) + { + Logger.Information(exception, format, args); + } + } + + public void InfoFormat(string format, params object[] args) + { + if (IsInfoEnabled) + { + Logger.Information(format, args); + } + } + + public void Warn(string message, Exception exception) + { + if (IsWarnEnabled) + { + Logger.Warning(exception, message); + } + } + + public void Warn(Func messageFactory) + { + if (IsWarnEnabled) + { + Logger.Warning(messageFactory.Invoke()); + } + } + + public void Warn(string message) + { + if (IsWarnEnabled) + { + Logger.Warning(message); + } + } + + public void WarnFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args) + { + if (IsWarnEnabled) + { + //TODO: This honours the formatProvider rather than passing through args for structured logging + Logger.Warning(exception, string.Format(formatProvider, format, args)); + } + } + + public void WarnFormat(IFormatProvider formatProvider, string format, params object[] args) + { + if (IsWarnEnabled) + { + //TODO: This honours the formatProvider rather than passing through args for structured logging + Logger.Warning(string.Format(formatProvider, format, args)); + } + } + + public void WarnFormat(Exception exception, string format, params object[] args) + { + if (IsWarnEnabled) + { + Logger.Warning(exception, format, args); + } + } + + public void WarnFormat(string format, params object[] args) + { + if (IsWarnEnabled) + { + Logger.Warning(format, args); + } + } + } } \ No newline at end of file From a11455b1a2385765f515975c1f4964f1a082a4fc Mon Sep 17 00:00:00 2001 From: Frode Nilsen Date: Wed, 5 Sep 2018 09:34:27 +0200 Subject: [PATCH 13/15] Removed unnecessary level checks --- .../NLogLogger.cs | 20 +++-------- .../SerilogLogger.cs | 34 +++++-------------- .../Log4netLogger.cs | 30 ++++------------ 3 files changed, 18 insertions(+), 66 deletions(-) diff --git a/src/Castle.Services.Logging.NLogIntegration/NLogLogger.cs b/src/Castle.Services.Logging.NLogIntegration/NLogLogger.cs index 14d06992b7..150ef819b7 100644 --- a/src/Castle.Services.Logging.NLogIntegration/NLogLogger.cs +++ b/src/Castle.Services.Logging.NLogIntegration/NLogLogger.cs @@ -150,10 +150,7 @@ public void Trace(Func messageFactory) /// The message to log public void Trace(string message, Exception exception) { - if (IsTraceEnabled) - { - Log(LogLevel.Trace, message, exception); - } + Log(LogLevel.Trace, message, exception); } /// @@ -163,10 +160,7 @@ public void Trace(string message, Exception exception) /// Format arguments for the message to log public void TraceFormat(string format, params object[] args) { - if (IsTraceEnabled) - { - Log(LogLevel.Trace, format, args); - } + Log(LogLevel.Trace, format, args); } /// @@ -191,10 +185,7 @@ public void TraceFormat(Exception exception, string format, params object[] args /// Format arguments for the message to log public void TraceFormat(IFormatProvider formatProvider, string format, params object[] args) { - if (IsTraceEnabled) - { - Log(LogLevel.Trace, formatProvider, format, args); - } + Log(LogLevel.Trace, formatProvider, format, args); } /// @@ -206,10 +197,7 @@ public void TraceFormat(IFormatProvider formatProvider, string format, params ob /// Format arguments for the message to log public void TraceFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args) { - if (IsTraceEnabled) - { - Log(LogLevel.Trace, exception, formatProvider, format, args); - } + Log(LogLevel.Trace, exception, formatProvider, format, args); } /// diff --git a/src/Castle.Services.Logging.SerilogIntegration/SerilogLogger.cs b/src/Castle.Services.Logging.SerilogIntegration/SerilogLogger.cs index 34fe9ea70d..e589f812b8 100644 --- a/src/Castle.Services.Logging.SerilogIntegration/SerilogLogger.cs +++ b/src/Castle.Services.Logging.SerilogIntegration/SerilogLogger.cs @@ -83,10 +83,7 @@ public Castle.Core.Logging.ILogger CreateChildLogger(string loggerName) public void Trace(string message, Exception exception) { - if (IsTraceEnabled) - { - Logger.Verbose(exception, message); - } + Logger.Verbose(exception, message); } public void Trace(Func messageFactory) @@ -99,44 +96,29 @@ public void Trace(Func messageFactory) public void Trace(string message) { - if (IsTraceEnabled) - { - Logger.Verbose(message); - } + Logger.Verbose(message); } public void TraceFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args) { - if (IsTraceEnabled) - { - //TODO: This honours the formatProvider rather than passing through args for structured logging - Logger.Verbose(exception, string.Format(formatProvider, format, args)); - } + //TODO: This honours the formatProvider rather than passing through args for structured logging + Logger.Verbose(exception, string.Format(formatProvider, format, args)); } public void TraceFormat(IFormatProvider formatProvider, string format, params object[] args) { - if (IsTraceEnabled) - { - //TODO: This honours the formatProvider rather than passing through args for structured logging - Logger.Verbose(string.Format(formatProvider, format, args)); - } + //TODO: This honours the formatProvider rather than passing through args for structured logging + Logger.Verbose(string.Format(formatProvider, format, args)); } public void TraceFormat(Exception exception, string format, params object[] args) { - if (IsTraceEnabled) - { - Logger.Verbose(exception, format, args); - } + Logger.Verbose(exception, format, args); } public void TraceFormat(string format, params object[] args) { - if (IsTraceEnabled) - { - Logger.Verbose(format, args); - } + Logger.Verbose(format, args); } public void Debug(string message, Exception exception) diff --git a/src/Castle.Services.Logging.log4netIntegration/Log4netLogger.cs b/src/Castle.Services.Logging.log4netIntegration/Log4netLogger.cs index 8deffb3180..4d62c30505 100644 --- a/src/Castle.Services.Logging.log4netIntegration/Log4netLogger.cs +++ b/src/Castle.Services.Logging.log4netIntegration/Log4netLogger.cs @@ -92,10 +92,7 @@ public virtual Castle.Core.Logging.ILogger CreateChildLogger(String name) public void Trace(String message) { - if (IsTraceEnabled) - { - Logger.Log(declaringType, Level.Trace, message, null); - } + Logger.Log(declaringType, Level.Trace, message, null); } public void Trace(Func messageFactory) @@ -108,42 +105,27 @@ public void Trace(Func messageFactory) public void Trace(String message, Exception exception) { - if (IsTraceEnabled) - { - Logger.Log(declaringType, Level.Trace, message, exception); - } + Logger.Log(declaringType, Level.Trace, message, exception); } public void TraceFormat(String format, params Object[] args) { - if (IsTraceEnabled) - { - Logger.Log(declaringType, Level.Trace, new SystemStringFormat(CultureInfo.InvariantCulture, format, args), null); - } + Logger.Log(declaringType, Level.Trace, new SystemStringFormat(CultureInfo.InvariantCulture, format, args), null); } public void TraceFormat(Exception exception, String format, params Object[] args) { - if (IsTraceEnabled) - { - Logger.Log(declaringType, Level.Trace, new SystemStringFormat(CultureInfo.InvariantCulture, format, args), exception); - } + Logger.Log(declaringType, Level.Trace, new SystemStringFormat(CultureInfo.InvariantCulture, format, args), exception); } public void TraceFormat(IFormatProvider formatProvider, String format, params Object[] args) { - if (IsTraceEnabled) - { - Logger.Log(declaringType, Level.Trace, new SystemStringFormat(formatProvider, format, args), null); - } + Logger.Log(declaringType, Level.Trace, new SystemStringFormat(formatProvider, format, args), null); } public void TraceFormat(Exception exception, IFormatProvider formatProvider, String format, params Object[] args) { - if (IsTraceEnabled) - { - Logger.Log(declaringType, Level.Trace, new SystemStringFormat(formatProvider, format, args), exception); - } + Logger.Log(declaringType, Level.Trace, new SystemStringFormat(formatProvider, format, args), exception); } public void Debug(String message) From 6ae6b9af3f961167af6ff4c7be1bab9752c62228 Mon Sep 17 00:00:00 2001 From: Frode Nilsen Date: Wed, 5 Sep 2018 13:04:02 +0200 Subject: [PATCH 14/15] Adjusted use of log level guards to follow rules in each implementation. --- .../NLogLogger.cs | 7 ++--- .../SerilogLogger.cs | 29 +++++++++++++----- .../Log4netLogger.cs | 30 +++++++++++++++---- 3 files changed, 48 insertions(+), 18 deletions(-) diff --git a/src/Castle.Services.Logging.NLogIntegration/NLogLogger.cs b/src/Castle.Services.Logging.NLogIntegration/NLogLogger.cs index 150ef819b7..7309e9d03e 100644 --- a/src/Castle.Services.Logging.NLogIntegration/NLogLogger.cs +++ b/src/Castle.Services.Logging.NLogIntegration/NLogLogger.cs @@ -139,7 +139,7 @@ public void Trace(Func messageFactory) { if (IsTraceEnabled) { - Log(LogLevel.Debug, messageFactory()); + Log(LogLevel.Trace, messageFactory()); } } @@ -171,10 +171,7 @@ public void TraceFormat(string format, params object[] args) /// Format arguments for the message to log public void TraceFormat(Exception exception, string format, params object[] args) { - if (IsTraceEnabled) - { - Log(LogLevel.Trace, exception, format, args); - } + Log(LogLevel.Trace, exception, format, args); } /// diff --git a/src/Castle.Services.Logging.SerilogIntegration/SerilogLogger.cs b/src/Castle.Services.Logging.SerilogIntegration/SerilogLogger.cs index e589f812b8..6c1498d3c3 100644 --- a/src/Castle.Services.Logging.SerilogIntegration/SerilogLogger.cs +++ b/src/Castle.Services.Logging.SerilogIntegration/SerilogLogger.cs @@ -96,29 +96,44 @@ public void Trace(Func messageFactory) public void Trace(string message) { - Logger.Verbose(message); + if (IsTraceEnabled) + { + Logger.Verbose(message); + } } public void TraceFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args) { - //TODO: This honours the formatProvider rather than passing through args for structured logging - Logger.Verbose(exception, string.Format(formatProvider, format, args)); + if (IsTraceEnabled) + { + //TODO: This honours the formatProvider rather than passing through args for structured logging + Logger.Verbose(exception, string.Format(formatProvider, format, args)); + } } public void TraceFormat(IFormatProvider formatProvider, string format, params object[] args) { - //TODO: This honours the formatProvider rather than passing through args for structured logging - Logger.Verbose(string.Format(formatProvider, format, args)); + if (IsTraceEnabled) + { + //TODO: This honours the formatProvider rather than passing through args for structured logging + Logger.Verbose(string.Format(formatProvider, format, args)); + } } public void TraceFormat(Exception exception, string format, params object[] args) { - Logger.Verbose(exception, format, args); + if (IsTraceEnabled) + { + Logger.Verbose(exception, format, args); + } } public void TraceFormat(string format, params object[] args) { - Logger.Verbose(format, args); + if (IsTraceEnabled) + { + Logger.Verbose(format, args); + } } public void Debug(string message, Exception exception) diff --git a/src/Castle.Services.Logging.log4netIntegration/Log4netLogger.cs b/src/Castle.Services.Logging.log4netIntegration/Log4netLogger.cs index 4d62c30505..8deffb3180 100644 --- a/src/Castle.Services.Logging.log4netIntegration/Log4netLogger.cs +++ b/src/Castle.Services.Logging.log4netIntegration/Log4netLogger.cs @@ -92,7 +92,10 @@ public virtual Castle.Core.Logging.ILogger CreateChildLogger(String name) public void Trace(String message) { - Logger.Log(declaringType, Level.Trace, message, null); + if (IsTraceEnabled) + { + Logger.Log(declaringType, Level.Trace, message, null); + } } public void Trace(Func messageFactory) @@ -105,27 +108,42 @@ public void Trace(Func messageFactory) public void Trace(String message, Exception exception) { - Logger.Log(declaringType, Level.Trace, message, exception); + if (IsTraceEnabled) + { + Logger.Log(declaringType, Level.Trace, message, exception); + } } public void TraceFormat(String format, params Object[] args) { - Logger.Log(declaringType, Level.Trace, new SystemStringFormat(CultureInfo.InvariantCulture, format, args), null); + if (IsTraceEnabled) + { + Logger.Log(declaringType, Level.Trace, new SystemStringFormat(CultureInfo.InvariantCulture, format, args), null); + } } public void TraceFormat(Exception exception, String format, params Object[] args) { - Logger.Log(declaringType, Level.Trace, new SystemStringFormat(CultureInfo.InvariantCulture, format, args), exception); + if (IsTraceEnabled) + { + Logger.Log(declaringType, Level.Trace, new SystemStringFormat(CultureInfo.InvariantCulture, format, args), exception); + } } public void TraceFormat(IFormatProvider formatProvider, String format, params Object[] args) { - Logger.Log(declaringType, Level.Trace, new SystemStringFormat(formatProvider, format, args), null); + if (IsTraceEnabled) + { + Logger.Log(declaringType, Level.Trace, new SystemStringFormat(formatProvider, format, args), null); + } } public void TraceFormat(Exception exception, IFormatProvider formatProvider, String format, params Object[] args) { - Logger.Log(declaringType, Level.Trace, new SystemStringFormat(formatProvider, format, args), exception); + if (IsTraceEnabled) + { + Logger.Log(declaringType, Level.Trace, new SystemStringFormat(formatProvider, format, args), exception); + } } public void Debug(String message) From fecfa85436df52b3760d6e9993375f4ee22e8a01 Mon Sep 17 00:00:00 2001 From: Frode Nilsen Date: Wed, 5 Sep 2018 14:32:14 +0200 Subject: [PATCH 15/15] Spaces for tabs --- .../SerilogLogger.cs | 44 +++++++++---------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/src/Castle.Services.Logging.SerilogIntegration/SerilogLogger.cs b/src/Castle.Services.Logging.SerilogIntegration/SerilogLogger.cs index 6c1498d3c3..5e968d2c31 100644 --- a/src/Castle.Services.Logging.SerilogIntegration/SerilogLogger.cs +++ b/src/Castle.Services.Logging.SerilogIntegration/SerilogLogger.cs @@ -96,44 +96,44 @@ public void Trace(Func messageFactory) public void Trace(string message) { - if (IsTraceEnabled) - { - Logger.Verbose(message); - } + if (IsTraceEnabled) + { + Logger.Verbose(message); + } } public void TraceFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args) { - if (IsTraceEnabled) - { - //TODO: This honours the formatProvider rather than passing through args for structured logging - Logger.Verbose(exception, string.Format(formatProvider, format, args)); - } + if (IsTraceEnabled) + { + //TODO: This honours the formatProvider rather than passing through args for structured logging + Logger.Verbose(exception, string.Format(formatProvider, format, args)); + } } public void TraceFormat(IFormatProvider formatProvider, string format, params object[] args) { - if (IsTraceEnabled) - { - //TODO: This honours the formatProvider rather than passing through args for structured logging - Logger.Verbose(string.Format(formatProvider, format, args)); - } + if (IsTraceEnabled) + { + //TODO: This honours the formatProvider rather than passing through args for structured logging + Logger.Verbose(string.Format(formatProvider, format, args)); + } } public void TraceFormat(Exception exception, string format, params object[] args) { - if (IsTraceEnabled) - { - Logger.Verbose(exception, format, args); - } + if (IsTraceEnabled) + { + Logger.Verbose(exception, format, args); + } } public void TraceFormat(string format, params object[] args) { - if (IsTraceEnabled) - { - Logger.Verbose(format, args); - } + if (IsTraceEnabled) + { + Logger.Verbose(format, args); + } } public void Debug(string message, Exception exception)