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

A first pass at implementing logging scopes #57

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
56 changes: 46 additions & 10 deletions UKHO.Logging.EventHubLogProvider/EventHubLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,23 @@

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;

using Microsoft.Extensions.Logging;

using UKHO.Logging.EventHubLogProvider.AzureStorageEventLogging.Interfaces;

namespace UKHO.Logging.EventHubLogProvider
{
internal class EventHubLogger : ILogger
{
private const string OriginalFormatPropertyName = "{OriginalFormat}";
private readonly Action<IDictionary<string, object>> additionalValuesProvider;
private readonly string categoryName;
private readonly string environment;
private readonly IEventHubLog eventHubLog;
private readonly LogLevel minimumLogLevel;
private readonly string environment;
private readonly string categoryName;
private readonly string system;
private readonly string service;
private readonly string nodeName;
private readonly Action<IDictionary<string, object>> additionalValuesProvider;
private readonly List<object> scopes;
private readonly string service;
private readonly string system;

internal EventHubLogger(IEventHubLog eventHubLog,
string categoryName,
Expand All @@ -48,6 +46,7 @@ internal EventHubLogger(IEventHubLog eventHubLog,
system = options.System;
service = options.Service;
nodeName = options.NodeName;
scopes = new List<object>();
additionalValuesProvider = options.AdditionalValuesProvider;
}

Expand All @@ -73,6 +72,26 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
result["LoggingErrorException"] = e;
}

// Allow scopes to override additional values
foreach (var scope in scopes)
{
switch (scope)
{
case KeyValuePair<string, object> kv:
result[kv.Key] = kv.Value;
break;
case IEnumerable<KeyValuePair<string, object>> list:
{
foreach (var kv in list)
{
result[kv.Key] = kv.Value;
}

break;
}
}
}

var messageTemplate = "";
if (state is IEnumerable<KeyValuePair<string, object>> structure)
{
Expand Down Expand Up @@ -130,10 +149,27 @@ public bool IsEnabled(LogLevel logLevel)
return logLevel >= minimumLogLevel;
}

[ExcludeFromCodeCoverage] // nothing to test
public IDisposable BeginScope<TState>(TState state)
{
return new EmptyDisposable();
scopes.Add(state);
return new Scope<TState>(scopes, state);
}

private class Scope<TState> : IDisposable
{
private readonly List<object> scopes;
private readonly TState state;

public Scope(List<object> scopes, TState state)
{
this.scopes = scopes;
this.state = state;
}

public void Dispose()
{
scopes.Remove(state);
}
}
}
}
36 changes: 29 additions & 7 deletions UKHO.Logging.EventHubLogProviderTest/EventHubLoggerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;

using FakeItEasy;

using Microsoft.Extensions.Logging;

using NUnit.Framework;

using UKHO.Logging.EventHubLogProvider;

namespace UKHO.Logging.EventHubLogProviderTest
Expand Down Expand Up @@ -234,10 +238,10 @@ public void TestLoggerLogsAllTheIndividualParamsForStructuredDataUsingFormattedL
var formattedLogEntry = "Message with {Property1} and {Property2} and a escaped C# keyword name {@var}";
var structuredData = new Dictionary<string, object>
{
{"Property1", "Value 1" },
{"Property2", "Value 2" },
{"@var", "Var value" },
{"{OriginalFormat}", formattedLogEntry },
{ "Property1", "Value 1" },
{ "Property2", "Value 2" },
{ "@var", "Var value" },
{ "{OriginalFormat}", formattedLogEntry },
};
eventHubLogger.Log(LogLevel.Information, 123, structuredData, null, (s, e) => string.Join(",", s.Select(kv => $"{kv.Key}:{kv.Value}")));

Expand All @@ -259,9 +263,9 @@ public void TestLoggerLogsAllTheIndividualParamsForStructuredDataUsingFormattedL
var formattedLogEntry = "Message with {Property1} and {Property1} and a escaped C# keyword name {@var}";
var structuredData = new Dictionary<string, object>
{
{"Property1", new[] { "Value 1", "Value 2" } },
{"@var", "Var value" },
{"{OriginalFormat}", formattedLogEntry },
{ "Property1", new[] { "Value 1", "Value 2" } },
{ "@var", "Var value" },
{ "{OriginalFormat}", formattedLogEntry },
};
eventHubLogger.Log(LogLevel.Information, 123, structuredData, null, (s, e) => string.Join(",", s.Select(kv => $"{kv.Key}:{kv.Value}")));

Expand All @@ -272,6 +276,24 @@ public void TestLoggerLogsAllTheIndividualParamsForStructuredDataUsingFormattedL
Assert.AreEqual("Message with {Property1} and {Property1} and a escaped C# keyword name {@var}", loggedEntry.MessageTemplate);
}

[Test]
public void TestLoggerLogsKeyValueScope()
{
LogEntry loggedEntry = null;
A.CallTo(() => fakeEventHubLog.Log(A<LogEntry>.Ignored)).Invokes((LogEntry l) => loggedEntry = l);

var eventHubLogger = CreateTestEventHubLogger(LogLevel.Information, LogLevel.Information, "UKHO.TestClass", fakeEventHubLog);

var scope = new KeyValuePair<string, object>("Test Scope Key", "Test Scope Value");

using (eventHubLogger.BeginScope(scope))
{
eventHubLogger.Log(LogLevel.Information, 123, "Simple Message", null, (s, e) => s);
}

Assert.Contains(scope, loggedEntry.LogProperties);
}

[Test]
public void TestLoggerLogsRequiredEnvironmentParameters()
{
Expand Down