Skip to content
This repository was archived by the owner on Dec 13, 2018. It is now read-only.

Commit

Permalink
fixed the bug in Logger, added a fallback for null formatters, added …
Browse files Browse the repository at this point in the history
…tests
  • Loading branch information
sonjakhan committed Oct 7, 2014
1 parent 600539a commit 44c6278
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 10 deletions.
2 changes: 2 additions & 0 deletions samples/SampleApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public void Main(string[] args)
catch (Exception ex)
{
_logger.WriteError("Unexpected error starting application", ex);
_logger.Write(TraceType.Critical, 0, "unexpected error", ex, null);
_logger.Write(TraceType.Critical, 0, null, null, null);
}

using (_logger.BeginScope("Main"))
Expand Down
17 changes: 16 additions & 1 deletion src/Microsoft.Framework.Logging.NLog/NLogLoggerProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,24 @@ public void Write(
Func<object, Exception, string> formatter)
{
var logLevel = GetLogLevel(eventType);
var message = "";
if (formatter != null)
{
var message = formatter(state, exception);
message = formatter(state, exception);
}
else
{
if (state != null)
{
message += state;
}
if (exception != null)
{
message += "\r\n" + exception;
}
}
if (message != String.Empty)
{
var eventInfo = LogEventInfo.Create(logLevel, _logger.Name, message, exception);
eventInfo.Properties["EventId"] = eventId;
_logger.Log(eventInfo);
Expand Down
24 changes: 22 additions & 2 deletions src/Microsoft.Framework.Logging/DiagnosticsLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,29 @@ public void Write(TraceType traceType, int eventId, object state, Exception exce
{
var eventType = GetEventType(traceType);

if (formatter != null && _traceSource.Switch.ShouldTrace(eventType))
if (!_traceSource.Switch.ShouldTrace(eventType))
{
_traceSource.TraceEvent(eventType, eventId, formatter(state, exception));
return;
}
var message = "";
if (formatter != null)
{
message = formatter(state, exception);
}
else
{
if (state != null)
{
message += state;
}
if (exception != null)
{
message += "\r\n" + exception;
}
}
if (message != String.Empty)
{
_traceSource.TraceEvent(eventType, eventId, message);
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/Microsoft.Framework.Logging/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ public bool IsEnabled(TraceType eventType)
var count = _loggers.Length;
for (var index = 0; index != count; index++)
{
if (!_loggers[index].IsEnabled(eventType))
if (_loggers[index].IsEnabled(eventType))
{
return false;
return true;
}
}
return true;
return false;
}

public IDisposable BeginScope(object state)
Expand Down
26 changes: 22 additions & 4 deletions test/Microsoft.Framework.Logging.Test/DiagnosticsLoggerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,11 @@

namespace Microsoft.Framework.Logging.Test
{
/// <summary>
/// Summary description for DiagnosticsLoggerTest
/// </summary>
public class DiagnosticsLoggerTest
{
#if ASPNET50
[Fact]
public static void DiagnosticsLoggerIsEnabledReturnsCorrectValue()
public static void IsEnabledReturnsCorrectValue()
{
SourceSwitch testSwitch = new SourceSwitch("TestSwitch", "Level will be set to warning for this test");
testSwitch.Level = SourceLevels.Warning;
Expand All @@ -33,6 +30,27 @@ public static void DiagnosticsLoggerIsEnabledReturnsCorrectValue()
Assert.Equal(false, logger.IsEnabled(TraceType.Information));
Assert.Equal(false, logger.IsEnabled(TraceType.Verbose));
}

[Theory]
[InlineData(SourceLevels.Warning, SourceLevels.Information, true)]
[InlineData(SourceLevels.Information, SourceLevels.Information, true)]
[InlineData(SourceLevels.Information, SourceLevels.Warning, true)]
[InlineData(SourceLevels.Warning, SourceLevels.Warning, false)]
public static void MultipleLoggers_IsEnabledReturnsCorrectValue(SourceLevels first, SourceLevels second, bool expected)
{
SourceSwitch firstSwitch = new SourceSwitch("FirstSwitch", "First Test Switch");
firstSwitch.Level = first;

SourceSwitch secondSwitch = new SourceSwitch("SecondSwitch", "Second Test Switch");
secondSwitch.Level = second;

var factory = new LoggerFactory();
ILogger logger = factory.Create("Test");

factory.AddProvider(new DiagnosticsLoggerProvider(firstSwitch, new ConsoleTraceListener()));
factory.AddProvider(new DiagnosticsLoggerProvider(secondSwitch, new ConsoleTraceListener()));
Assert.Equal(expected, logger.IsEnabled(TraceType.Information));
}
}
#endif
}

0 comments on commit 44c6278

Please sign in to comment.