Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add trace log level #404

Merged
merged 15 commits into from
Dec 21, 2018
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

Enhancements:
- 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)
- `Castle.Core.Internal.Lock` (class)
Expand Down
28 changes: 28 additions & 0 deletions src/Castle.Core.Tests/Core.Tests/Logging/ConsoleLoggerTestCase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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");
Expand All @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 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]
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");
Expand All @@ -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 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");
Expand All @@ -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 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");
Expand All @@ -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 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");
Expand All @@ -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 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");
Expand All @@ -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");
Expand Down
26 changes: 25 additions & 1 deletion src/Castle.Core.Tests/Core.Tests/Logging/StreamLoggerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
40 changes: 40 additions & 0 deletions src/Castle.Core.Tests/DynamicProxy.Tests/LoggingTestCase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 NotSupportedException();
}

public void Trace(Func<string> messageFactory)
{
throw new NotSupportedException();
}

public void Trace(string message, Exception exception)
{
throw new NotSupportedException();
}

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 NotSupportedException();
}

public void TraceFormat(IFormatProvider formatProvider, string format, params object[] args)
{
throw new NotSupportedException();
}

public void TraceFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args)
{
throw new NotSupportedException();
}

public void Debug(string message)
{
throw new NotImplementedException();
Expand Down Expand Up @@ -348,6 +383,11 @@ public ILogger CreateChildLogger(string loggerName)
throw new NotImplementedException();
}

public bool IsTraceEnabled
{
get { return true; }
}

public bool IsDebugEnabled
{
get { return true; }
Expand Down
2 changes: 1 addition & 1 deletion src/Castle.Core/Core/Logging/DiagnosticsLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public DiagnosticsLogger(string logName) : this(logName, "default")
/// </summary>
/// <param name = "logName"><see cref = "EventLog.Log" /></param>
/// <param name = "source"><see cref = "EventLog.Source" /></param>
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))
Expand Down
56 changes: 56 additions & 0 deletions src/Castle.Core/Core/Logging/ILogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ namespace Castle.Core.Logging
/// </remarks>
public interface ILogger
{
/// <summary>
/// Determines if messages of priority "trace" will be logged.
/// </summary>
/// <value>True if "trace" messages will be logged.</value>
bool IsTraceEnabled { get; }

/// <summary>
/// Determines if messages of priority "debug" will be logged.
/// </summary>
Expand Down Expand Up @@ -65,6 +71,56 @@ public interface ILogger
/// <exception cref = "System.ArgumentException">If the name has an empty element name.</exception>
ILogger CreateChildLogger(String loggerName);

/// <summary>
/// Logs a trace message.
/// </summary>
/// <param name = "message">The message to log</param>
void Trace(String message);

/// <summary>
/// Logs a trace message with lazily constructed message. The message will be constructed only if the <see cref = "IsTraceEnabled" /> is true.
/// </summary>
void Trace(Func<string> messageFactory);

/// <summary>
/// Logs a trace message.
/// </summary>
/// <param name = "exception">The exception to log</param>
/// <param name = "message">The message to log</param>
void Trace(String message, Exception exception);

/// <summary>
/// Logs a trace message.
/// </summary>
/// <param name = "format">Format string for the message to log</param>
/// <param name = "args">Format arguments for the message to log</param>
void TraceFormat(String format, params Object[] args);

/// <summary>
/// Logs a trace message.
/// </summary>
/// <param name = "exception">The exception to log</param>
/// <param name = "format">Format string for the message to log</param>
/// <param name = "args">Format arguments for the message to log</param>
void TraceFormat(Exception exception, String format, params Object[] args);

/// <summary>
/// Logs a trace message.
/// </summary>
/// <param name = "formatProvider">The format provider to use</param>
/// <param name = "format">Format string for the message to log</param>
/// <param name = "args">Format arguments for the message to log</param>
void TraceFormat(IFormatProvider formatProvider, String format, params Object[] args);

/// <summary>
/// Logs a trace message.
/// </summary>
/// <param name = "exception">The exception to log</param>
/// <param name = "formatProvider">The format provider to use</param>
/// <param name = "format">Format string for the message to log</param>
/// <param name = "args">Format arguments for the message to log</param>
void TraceFormat(Exception exception, IFormatProvider formatProvider, String format, params Object[] args);

/// <summary>
/// Logs a debug message.
/// </summary>
Expand Down
Loading