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

Issue #3: Refactoring ILogger #31

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 79 additions & 1 deletion Logging.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.21901.1
VisualStudioVersion = 14.0.22115.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.Framework.Logging", "src\Microsoft.Framework.Logging\Microsoft.Framework.Logging.kproj", "{19D1B6C5-8A62-4387-8816-C54874D1DF5F}"
EndProject
Expand All @@ -17,8 +17,16 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{8C1F
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "SampleApp", "samples\SampleApp\SampleApp.kproj", "{550E0247-0BDD-4016-A29B-250F075686FD}"
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.Framework.Logging.Console", "src\Microsoft.Framework.Logging.Console\Microsoft.Framework.Logging.Console.kproj", "{75A4DE6D-BBAA-4D59-829D-94009E759A18}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
aspnet50|Any CPU = aspnet50|Any CPU
aspnet50|Mixed Platforms = aspnet50|Mixed Platforms
aspnet50|x86 = aspnet50|x86
aspnetcore50|Any CPU = aspnetcore50|Any CPU
aspnetcore50|Mixed Platforms = aspnetcore50|Mixed Platforms
aspnetcore50|x86 = aspnetcore50|x86
Debug|Any CPU = Debug|Any CPU
Debug|Mixed Platforms = Debug|Mixed Platforms
Debug|x86 = Debug|x86
Expand All @@ -27,6 +35,16 @@ Global
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{19D1B6C5-8A62-4387-8816-C54874D1DF5F}.aspnet50|Any CPU.ActiveCfg = Release|Any CPU
{19D1B6C5-8A62-4387-8816-C54874D1DF5F}.aspnet50|Any CPU.Build.0 = Release|Any CPU
{19D1B6C5-8A62-4387-8816-C54874D1DF5F}.aspnet50|Mixed Platforms.ActiveCfg = Release|Any CPU
{19D1B6C5-8A62-4387-8816-C54874D1DF5F}.aspnet50|Mixed Platforms.Build.0 = Release|Any CPU
{19D1B6C5-8A62-4387-8816-C54874D1DF5F}.aspnet50|x86.ActiveCfg = Release|Any CPU
{19D1B6C5-8A62-4387-8816-C54874D1DF5F}.aspnetcore50|Any CPU.ActiveCfg = Release|Any CPU
{19D1B6C5-8A62-4387-8816-C54874D1DF5F}.aspnetcore50|Any CPU.Build.0 = Release|Any CPU
{19D1B6C5-8A62-4387-8816-C54874D1DF5F}.aspnetcore50|Mixed Platforms.ActiveCfg = Release|Any CPU
{19D1B6C5-8A62-4387-8816-C54874D1DF5F}.aspnetcore50|Mixed Platforms.Build.0 = Release|Any CPU
{19D1B6C5-8A62-4387-8816-C54874D1DF5F}.aspnetcore50|x86.ActiveCfg = Release|Any CPU
{19D1B6C5-8A62-4387-8816-C54874D1DF5F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{19D1B6C5-8A62-4387-8816-C54874D1DF5F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{19D1B6C5-8A62-4387-8816-C54874D1DF5F}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
Expand All @@ -37,6 +55,16 @@ Global
{19D1B6C5-8A62-4387-8816-C54874D1DF5F}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{19D1B6C5-8A62-4387-8816-C54874D1DF5F}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{19D1B6C5-8A62-4387-8816-C54874D1DF5F}.Release|x86.ActiveCfg = Release|Any CPU
{96B1D6A8-7E40-43C7-813F-898DC8192DDE}.aspnet50|Any CPU.ActiveCfg = Release|Any CPU
{96B1D6A8-7E40-43C7-813F-898DC8192DDE}.aspnet50|Any CPU.Build.0 = Release|Any CPU
{96B1D6A8-7E40-43C7-813F-898DC8192DDE}.aspnet50|Mixed Platforms.ActiveCfg = Release|Any CPU
{96B1D6A8-7E40-43C7-813F-898DC8192DDE}.aspnet50|Mixed Platforms.Build.0 = Release|Any CPU
{96B1D6A8-7E40-43C7-813F-898DC8192DDE}.aspnet50|x86.ActiveCfg = Release|Any CPU
{96B1D6A8-7E40-43C7-813F-898DC8192DDE}.aspnetcore50|Any CPU.ActiveCfg = Release|Any CPU
{96B1D6A8-7E40-43C7-813F-898DC8192DDE}.aspnetcore50|Any CPU.Build.0 = Release|Any CPU
{96B1D6A8-7E40-43C7-813F-898DC8192DDE}.aspnetcore50|Mixed Platforms.ActiveCfg = Release|Any CPU
{96B1D6A8-7E40-43C7-813F-898DC8192DDE}.aspnetcore50|Mixed Platforms.Build.0 = Release|Any CPU
{96B1D6A8-7E40-43C7-813F-898DC8192DDE}.aspnetcore50|x86.ActiveCfg = Release|Any CPU
{96B1D6A8-7E40-43C7-813F-898DC8192DDE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{96B1D6A8-7E40-43C7-813F-898DC8192DDE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{96B1D6A8-7E40-43C7-813F-898DC8192DDE}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
Expand All @@ -47,6 +75,16 @@ Global
{96B1D6A8-7E40-43C7-813F-898DC8192DDE}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{96B1D6A8-7E40-43C7-813F-898DC8192DDE}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{96B1D6A8-7E40-43C7-813F-898DC8192DDE}.Release|x86.ActiveCfg = Release|Any CPU
{8221FA95-4B1A-44BF-925F-8AC1A317CC7C}.aspnet50|Any CPU.ActiveCfg = Release|Any CPU
{8221FA95-4B1A-44BF-925F-8AC1A317CC7C}.aspnet50|Any CPU.Build.0 = Release|Any CPU
{8221FA95-4B1A-44BF-925F-8AC1A317CC7C}.aspnet50|Mixed Platforms.ActiveCfg = Release|Any CPU
{8221FA95-4B1A-44BF-925F-8AC1A317CC7C}.aspnet50|Mixed Platforms.Build.0 = Release|Any CPU
{8221FA95-4B1A-44BF-925F-8AC1A317CC7C}.aspnet50|x86.ActiveCfg = Release|Any CPU
{8221FA95-4B1A-44BF-925F-8AC1A317CC7C}.aspnetcore50|Any CPU.ActiveCfg = Release|Any CPU
{8221FA95-4B1A-44BF-925F-8AC1A317CC7C}.aspnetcore50|Any CPU.Build.0 = Release|Any CPU
{8221FA95-4B1A-44BF-925F-8AC1A317CC7C}.aspnetcore50|Mixed Platforms.ActiveCfg = Release|Any CPU
{8221FA95-4B1A-44BF-925F-8AC1A317CC7C}.aspnetcore50|Mixed Platforms.Build.0 = Release|Any CPU
{8221FA95-4B1A-44BF-925F-8AC1A317CC7C}.aspnetcore50|x86.ActiveCfg = Release|Any CPU
{8221FA95-4B1A-44BF-925F-8AC1A317CC7C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8221FA95-4B1A-44BF-925F-8AC1A317CC7C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8221FA95-4B1A-44BF-925F-8AC1A317CC7C}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
Expand All @@ -57,6 +95,16 @@ Global
{8221FA95-4B1A-44BF-925F-8AC1A317CC7C}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{8221FA95-4B1A-44BF-925F-8AC1A317CC7C}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{8221FA95-4B1A-44BF-925F-8AC1A317CC7C}.Release|x86.ActiveCfg = Release|Any CPU
{718CBC9D-1E65-447D-A64A-7AC467FB5D6A}.aspnet50|Any CPU.ActiveCfg = aspnet50|Any CPU
{718CBC9D-1E65-447D-A64A-7AC467FB5D6A}.aspnet50|Any CPU.Build.0 = aspnet50|Any CPU
{718CBC9D-1E65-447D-A64A-7AC467FB5D6A}.aspnet50|Mixed Platforms.ActiveCfg = aspnet50|Any CPU
{718CBC9D-1E65-447D-A64A-7AC467FB5D6A}.aspnet50|Mixed Platforms.Build.0 = aspnet50|Any CPU
{718CBC9D-1E65-447D-A64A-7AC467FB5D6A}.aspnet50|x86.ActiveCfg = aspnet50|Any CPU
{718CBC9D-1E65-447D-A64A-7AC467FB5D6A}.aspnetcore50|Any CPU.ActiveCfg = aspnet50|Any CPU
{718CBC9D-1E65-447D-A64A-7AC467FB5D6A}.aspnetcore50|Any CPU.Build.0 = aspnet50|Any CPU
{718CBC9D-1E65-447D-A64A-7AC467FB5D6A}.aspnetcore50|Mixed Platforms.ActiveCfg = aspnet50|Any CPU
{718CBC9D-1E65-447D-A64A-7AC467FB5D6A}.aspnetcore50|Mixed Platforms.Build.0 = aspnet50|Any CPU
{718CBC9D-1E65-447D-A64A-7AC467FB5D6A}.aspnetcore50|x86.ActiveCfg = aspnet50|Any CPU
{718CBC9D-1E65-447D-A64A-7AC467FB5D6A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{718CBC9D-1E65-447D-A64A-7AC467FB5D6A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{718CBC9D-1E65-447D-A64A-7AC467FB5D6A}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
Expand All @@ -67,6 +115,16 @@ Global
{718CBC9D-1E65-447D-A64A-7AC467FB5D6A}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{718CBC9D-1E65-447D-A64A-7AC467FB5D6A}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{718CBC9D-1E65-447D-A64A-7AC467FB5D6A}.Release|x86.ActiveCfg = Release|Any CPU
{550E0247-0BDD-4016-A29B-250F075686FD}.aspnet50|Any CPU.ActiveCfg = aspnetcore50|Any CPU
{550E0247-0BDD-4016-A29B-250F075686FD}.aspnet50|Any CPU.Build.0 = aspnetcore50|Any CPU
{550E0247-0BDD-4016-A29B-250F075686FD}.aspnet50|Mixed Platforms.ActiveCfg = aspnetcore50|Any CPU
{550E0247-0BDD-4016-A29B-250F075686FD}.aspnet50|Mixed Platforms.Build.0 = aspnetcore50|Any CPU
{550E0247-0BDD-4016-A29B-250F075686FD}.aspnet50|x86.ActiveCfg = aspnetcore50|Any CPU
{550E0247-0BDD-4016-A29B-250F075686FD}.aspnetcore50|Any CPU.ActiveCfg = aspnetcore50|Any CPU
{550E0247-0BDD-4016-A29B-250F075686FD}.aspnetcore50|Any CPU.Build.0 = aspnetcore50|Any CPU
{550E0247-0BDD-4016-A29B-250F075686FD}.aspnetcore50|Mixed Platforms.ActiveCfg = aspnetcore50|Any CPU
{550E0247-0BDD-4016-A29B-250F075686FD}.aspnetcore50|Mixed Platforms.Build.0 = aspnetcore50|Any CPU
{550E0247-0BDD-4016-A29B-250F075686FD}.aspnetcore50|x86.ActiveCfg = aspnetcore50|Any CPU
{550E0247-0BDD-4016-A29B-250F075686FD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{550E0247-0BDD-4016-A29B-250F075686FD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{550E0247-0BDD-4016-A29B-250F075686FD}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
Expand All @@ -77,6 +135,26 @@ Global
{550E0247-0BDD-4016-A29B-250F075686FD}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{550E0247-0BDD-4016-A29B-250F075686FD}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{550E0247-0BDD-4016-A29B-250F075686FD}.Release|x86.ActiveCfg = Release|Any CPU
{75A4DE6D-BBAA-4D59-829D-94009E759A18}.aspnet50|Any CPU.ActiveCfg = aspnet50|Any CPU
{75A4DE6D-BBAA-4D59-829D-94009E759A18}.aspnet50|Any CPU.Build.0 = aspnet50|Any CPU
{75A4DE6D-BBAA-4D59-829D-94009E759A18}.aspnet50|Mixed Platforms.ActiveCfg = aspnet50|Any CPU
{75A4DE6D-BBAA-4D59-829D-94009E759A18}.aspnet50|Mixed Platforms.Build.0 = aspnet50|Any CPU
{75A4DE6D-BBAA-4D59-829D-94009E759A18}.aspnet50|x86.ActiveCfg = aspnet50|Any CPU
{75A4DE6D-BBAA-4D59-829D-94009E759A18}.aspnetcore50|Any CPU.ActiveCfg = aspnetcore50|Any CPU
{75A4DE6D-BBAA-4D59-829D-94009E759A18}.aspnetcore50|Any CPU.Build.0 = aspnetcore50|Any CPU
{75A4DE6D-BBAA-4D59-829D-94009E759A18}.aspnetcore50|Mixed Platforms.ActiveCfg = aspnetcore50|Any CPU
{75A4DE6D-BBAA-4D59-829D-94009E759A18}.aspnetcore50|Mixed Platforms.Build.0 = aspnetcore50|Any CPU
{75A4DE6D-BBAA-4D59-829D-94009E759A18}.aspnetcore50|x86.ActiveCfg = aspnetcore50|Any CPU
{75A4DE6D-BBAA-4D59-829D-94009E759A18}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{75A4DE6D-BBAA-4D59-829D-94009E759A18}.Debug|Any CPU.Build.0 = Debug|Any CPU
{75A4DE6D-BBAA-4D59-829D-94009E759A18}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{75A4DE6D-BBAA-4D59-829D-94009E759A18}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{75A4DE6D-BBAA-4D59-829D-94009E759A18}.Debug|x86.ActiveCfg = Debug|Any CPU
{75A4DE6D-BBAA-4D59-829D-94009E759A18}.Release|Any CPU.ActiveCfg = Release|Any CPU
{75A4DE6D-BBAA-4D59-829D-94009E759A18}.Release|Any CPU.Build.0 = Release|Any CPU
{75A4DE6D-BBAA-4D59-829D-94009E759A18}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{75A4DE6D-BBAA-4D59-829D-94009E759A18}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{75A4DE6D-BBAA-4D59-829D-94009E759A18}.Release|x86.ActiveCfg = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
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
10 changes: 7 additions & 3 deletions src/Microsoft.Framework.Logging.Interfaces/ILogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,21 @@ public interface ILogger
{
/// <summary>
/// Aggregates most logging patterns to a single method. This must be compatible with the Func representation in the OWIN environment.
///
/// To check IsEnabled call WriteCore with only TraceEventType and check the return value, no event will be written.
/// </summary>
/// <param name="eventType"></param>
/// <param name="eventId"></param>
/// <param name="state"></param>
/// <param name="exception"></param>
/// <param name="formatter"></param>
/// <returns></returns>
bool WriteCore(TraceType eventType, int eventId, object state, Exception exception, Func<object, Exception, string> formatter);
void Write(TraceType eventType, int eventId, object state, Exception exception, Func<object, Exception, string> formatter);

/// <summary>
/// Checks if the given TraceEventType is enabled.
/// </summary>
/// <param name="eventType"></param>
/// <returns></returns>
bool IsEnabled(TraceType eventType);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IsEnabled sounds more like a property name. Maybe IsEnableFor is better?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think IsEnabled is fine for this, though maybe not ideal. But better than IsEnabledFor 😄


/// <summary>
/// Begins a logical operation scope.
Expand Down
34 changes: 24 additions & 10 deletions src/Microsoft.Framework.Logging.NLog/NLogLoggerProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,41 @@ public Logger(global::NLog.Logger logger)
_logger = logger;
}

public bool WriteCore(
public void Write(
TraceType eventType,
int eventId,
object state,
Exception exception,
Func<object, Exception, string> formatter)
{
var logLevel = GetLogLevel(eventType);
if (!_logger.IsEnabled(logLevel))
var message = string.Empty;
if (formatter != null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lodejard What does it mean if the formatter is null after this refactoring?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we ought to have a fallback? Like maybe call ToString on state (if it's not null), and then get some data from the exception, such as its Message and/or StackTrace?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or throw argument null exception? @Eilon's suggestion wouldn't be surprising either, but if the form a tter is null you're calling the wrong overload...

{
return false;
message = formatter(state, exception);
}
if (formatter == null)
else
{
return true;
if (state != null)
{
message += state;
}
if (exception != null)
{
message += Environment.NewLine + exception;
}
}
var message = formatter(state, exception);
var eventInfo = LogEventInfo.Create(logLevel, _logger.Name, message, exception);
eventInfo.Properties["EventId"] = eventId;
_logger.Log(eventInfo);
return true;
if (!string.IsNullOrEmpty(message))
{
var eventInfo = LogEventInfo.Create(logLevel, _logger.Name, message, exception);
eventInfo.Properties["EventId"] = eventId;
_logger.Log(eventInfo);
}
}

public bool IsEnabled(TraceType eventType)
{
return _logger.IsEnabled(GetLogLevel(eventType));
}

private LogLevel GetLogLevel(TraceType eventType)
Expand Down
35 changes: 27 additions & 8 deletions src/Microsoft.Framework.Logging/DiagnosticsLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,38 @@ public DiagnosticsLogger(TraceSource traceSource)
_traceSource = traceSource;
}

public bool WriteCore(TraceType traceType, int eventId, object state, Exception exception, Func<object, Exception, string> formatter)
public void Write(TraceType traceType, int eventId, object state, Exception exception, Func<object, Exception, string> formatter)
{
var eventType = GetEventType(traceType);

if (!_traceSource.Switch.ShouldTrace(eventType))
if (!IsEnabled(traceType))
{
return;
}
var message = string.Empty;
if (formatter != null)
{
message = formatter(state, exception);
}
else
{
return false;
if (state != null)
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comments above.

message += state;
}
if (exception != null)
{
message += Environment.NewLine + exception;
}
}
else if (formatter != null)
if (!string.IsNullOrEmpty(message))
{
_traceSource.TraceEvent(eventType, eventId, formatter(state, exception));
_traceSource.TraceEvent(GetEventType(traceType), eventId, message);
}
return true;
}

public bool IsEnabled(TraceType traceType)
{
var eventType = GetEventType(traceType);
return _traceSource.Switch.ShouldTrace(eventType);
}

private static TraceEventType GetEventType(TraceType traceType)
Expand Down
16 changes: 9 additions & 7 deletions src/Microsoft.Framework.Logging/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,24 @@ public Logger(LoggerFactory loggerFactory, string name)
}
}

public bool WriteCore(TraceType eventType, int eventId, object state, Exception exception, Func<object, Exception, string> formatter)
public void Write(TraceType eventType, int eventId, object state, Exception exception, Func<object, Exception, string> formatter)
{
var result = false;
var count = _loggers.Length;
for (var index = 0; index != count; index++)
foreach (var logger in _loggers)
{
result |= _loggers[index].WriteCore(eventType, eventId, state, exception, formatter);
logger.Write(eventType, eventId, state, exception, formatter);
}
return result;
}

public bool IsEnabled(TraceType eventType)
{
return _loggers.Any(l => l.IsEnabled(eventType));
}

public IDisposable BeginScope(object state)
{
var count = _loggers.Length;
var scope = new Scope(count);
for (var index = 0; index != count; index++)
for (var index = 0; index < count; index++)
{
scope.SetDisposable(index, _loggers[index].BeginScope(state));
}
Expand Down
Loading