-
Notifications
You must be signed in to change notification settings - Fork 245
Issue #3: Refactoring ILogger #31
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we ought to have a fallback? Like maybe call There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
There was a problem hiding this comment.
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. MaybeIsEnableFor
is better?There was a problem hiding this comment.
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 thanIsEnabledFor
😄