Skip to content

Commit 87007d8

Browse files
authored
MicrosoftILoggerTarget - Reduce code complexity (#510)
1 parent c17101e commit 87007d8

1 file changed

Lines changed: 19 additions & 10 deletions

File tree

src/NLog.Extensions.Logging/Targets/MicrosoftILoggerTarget.cs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,30 +88,39 @@ protected override void Write(LogEventInfo logEvent)
8888
if (!ilogger.IsEnabled(logLevel))
8989
return;
9090

91+
var layoutMessage = RenderLogEvent(Layout, logEvent);
92+
93+
IDictionary<string, object> contextProperties = null;
94+
if (ContextProperties.Count > 0 || IncludeMdlc || IncludeMdc || IncludeGdc)
95+
{
96+
contextProperties = GetContextProperties(logEvent);
97+
if (contextProperties?.Count == 0)
98+
contextProperties = null;
99+
}
100+
101+
var eventId = RenderEventId(logEvent);
102+
ilogger.Log(logLevel, eventId, new LogState(logEvent, layoutMessage, contextProperties), logEvent.Exception, (s, ex) => LogStateFormatter(s));
103+
}
104+
105+
private EventId RenderEventId(LogEventInfo logEvent)
106+
{
91107
var eventId = default(EventId);
108+
92109
if (EventId != null)
93110
{
94111
var eventIdValue = RenderLogEvent(EventId, logEvent);
95112
if (!string.IsNullOrEmpty(eventIdValue) && int.TryParse(eventIdValue, out int eventIdParsed) && eventIdParsed != 0)
96113
eventId = new EventId(eventIdParsed);
97114
}
115+
98116
if (EventName != null)
99117
{
100118
var eventNameValue = RenderLogEvent(EventName, logEvent);
101119
if (!string.IsNullOrEmpty(eventNameValue))
102120
eventId = new EventId(eventId.Id, eventNameValue);
103121
}
104122

105-
var layoutMessage = RenderLogEvent(Layout, logEvent);
106-
IDictionary<string, object> contextProperties = null;
107-
if (ContextProperties.Count > 0 || IncludeMdlc || IncludeMdc || IncludeGdc)
108-
{
109-
contextProperties = GetContextProperties(logEvent);
110-
if (contextProperties?.Count == 0)
111-
contextProperties = null;
112-
}
113-
114-
ilogger.Log(ConvertToLogLevel(logEvent.Level), eventId, new LogState(logEvent, layoutMessage, contextProperties), logEvent.Exception, (s, ex) => LogStateFormatter(s));
123+
return eventId;
115124
}
116125

117126
private Microsoft.Extensions.Logging.ILogger CreateFromLoggerFactory(LogEventInfo logEvent)

0 commit comments

Comments
 (0)