Skip to content

Commit

Permalink
Merge pull request #17 from bernarden/feature/global-tags
Browse files Browse the repository at this point in the history
Adds global parameters.
  • Loading branch information
bernarden authored Jun 11, 2018
2 parents d68938c + 96e018e commit 18c4b2e
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,18 @@ public class AppInsightsAbstractLogger : AbstractLoggerBase, IAppInsightsAbstrac
/// <param name="telemetryClient">The Application Insights client.</param>
/// <param name="minimalLoggingLevel">The minimal logging level.</param>
public AppInsightsAbstractLogger(TelemetryClient telemetryClient, LoggingLevel minimalLoggingLevel = LoggingLevel.Verbose)
: base(minimalLoggingLevel)
: base(new AbstractLoggerSettings { MinimalLoggingLevel = minimalLoggingLevel })
{
_telemetryClient = telemetryClient ?? throw new ArgumentNullException(nameof(telemetryClient));
}

/// <summary>
/// Initializes a new instance of the <see cref="AppInsightsAbstractLogger"/> class.
/// </summary>
/// <param name="telemetryClient">The Application Insights client.</param>
/// <param name="settings">The logger's settings.</param>
public AppInsightsAbstractLogger(TelemetryClient telemetryClient, AbstractLoggerSettings settings)
: base(settings ?? throw new ArgumentNullException(nameof(settings)))
{
_telemetryClient = telemetryClient ?? throw new ArgumentNullException(nameof(telemetryClient));
}
Expand All @@ -42,11 +53,12 @@ public override void TraceMessage(string message, LoggingLevel loggingLevel, IEn
return;
}

var allParameters = GetGlobalAndLocalLoggingParameters(parameters);
var traceTelemetry = new TraceTelemetry(message)
{
SeverityLevel = LoggingLevelMapper.ConvertLoggingLevelToSeverityLevel(loggingLevel)
};
AddParametersToProperties(traceTelemetry, parameters);
AddParametersToProperties(traceTelemetry, allParameters);
_telemetryClient.Track(traceTelemetry);
}

Expand All @@ -63,11 +75,12 @@ public override void TraceException(Exception exception, LoggingLevel loggingLev
return;
}

var allParameters = GetGlobalAndLocalLoggingParameters(parameters);
var exceptionTelemetry = new ExceptionTelemetry(exception)
{
SeverityLevel = LoggingLevelMapper.ConvertLoggingLevelToSeverityLevel(loggingLevel)
};
AddParametersToProperties(exceptionTelemetry, parameters);
AddParametersToProperties(exceptionTelemetry, allParameters);
_telemetryClient.Track(exceptionTelemetry);
}

Expand Down
11 changes: 10 additions & 1 deletion Source/Vima.LoggingAbstractor.Console/ConsoleAbstractLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,16 @@ public class ConsoleAbstractLogger : AbstractLoggerBase, IConsoleAbstractLogger
/// </summary>
/// <param name="minimalLoggingLevel">The minimal logging level.</param>
public ConsoleAbstractLogger(LoggingLevel minimalLoggingLevel = LoggingLevel.Verbose)
: base(minimalLoggingLevel)
: base(new AbstractLoggerSettings { MinimalLoggingLevel = minimalLoggingLevel })
{
}

/// <summary>
/// Initializes a new instance of the <see cref="ConsoleAbstractLogger"/> class.
/// </summary>
/// <param name="settings">The logger's settings.</param>
public ConsoleAbstractLogger(AbstractLoggerSettings settings)
: base(settings ?? throw new ArgumentNullException(nameof(settings)))
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ namespace Vima.LoggingAbstractor.Core.Tests
public class TestAbstractLoggerBase : AbstractLoggerBase
{
public TestAbstractLoggerBase(LoggingLevel minimalLoggingLevel = LoggingLevel.Verbose)
: base(minimalLoggingLevel)
: base(new AbstractLoggerSettings { MinimalLoggingLevel = minimalLoggingLevel })
{
}

public TestAbstractLoggerBase(AbstractLoggerSettings settings)
: base(settings)
{
}

Expand Down
21 changes: 16 additions & 5 deletions Source/Vima.LoggingAbstractor.Core/AbstractLoggerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ namespace Vima.LoggingAbstractor.Core
/// </summary>
public abstract class AbstractLoggerBase : IAbstractLogger
{
private readonly LoggingLevel _minimalLoggingLevel;
private readonly AbstractLoggerSettings _settings;

/// <summary>
/// Initializes a new instance of the <see cref="AbstractLoggerBase"/> class.
/// </summary>
/// <param name="minimalLoggingLevel">The minimal logging level.</param>
protected AbstractLoggerBase(LoggingLevel minimalLoggingLevel)
/// <param name="settings">The logger's settings.</param>
protected AbstractLoggerBase(AbstractLoggerSettings settings)
{
_minimalLoggingLevel = minimalLoggingLevel;
_settings = settings ?? throw new ArgumentNullException(nameof(settings));
}

/// <summary>
Expand Down Expand Up @@ -87,7 +87,18 @@ protected bool ShouldBeTraced(LoggingLevel loggingLevel)
return false;
}

return loggingLevel >= _minimalLoggingLevel;
return loggingLevel >= _settings.MinimalLoggingLevel;
}

/// <summary>
/// Combines global and specified local parameters.
/// </summary>
/// <param name="localParameters">The logging parameters that are explicitly specified by the user on logging call.</param>
/// <returns>All parameters to be logged.</returns>
protected IEnumerable<ILoggingParameter> GetGlobalAndLocalLoggingParameters(IEnumerable<ILoggingParameter> localParameters)
{
var globalAndLocalParameters = localParameters.Concat(_settings.GlobalLoggingParameters);
return globalAndLocalParameters;
}
}
}
33 changes: 33 additions & 0 deletions Source/Vima.LoggingAbstractor.Core/AbstractLoggerSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.Collections.Generic;
using Vima.LoggingAbstractor.Core.Parameters;

namespace Vima.LoggingAbstractor.Core
{
/// <summary>
/// Represents an instance of settings.
/// </summary>
public class AbstractLoggerSettings
{
private IEnumerable<ILoggingParameter> _globalLoggingParameters;

/// <summary>
/// Gets or sets the minimal logging lever required for a log to go through.
/// </summary>
/// <value>
/// The minimal logging lever required for a log to go through.
/// </value>
public LoggingLevel MinimalLoggingLevel { get; set; }

/// <summary>
/// Gets or sets the global logging parameters that are applied to every log.
/// </summary>
/// <value>
/// The global logging parameters.
/// </value>
public IEnumerable<ILoggingParameter> GlobalLoggingParameters
{
get => _globalLoggingParameters ?? (_globalLoggingParameters = new List<ILoggingParameter>());
set => _globalLoggingParameters = value;
}
}
}
19 changes: 15 additions & 4 deletions Source/Vima.LoggingAbstractor.Raygun/RaygunAbstractLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,21 @@ public class RaygunAbstractLogger : AbstractLoggerBase, IRaygunAbstractLogger
/// <summary>
/// Initializes a new instance of the <see cref="RaygunAbstractLogger"/> class.
/// </summary>
/// <param name="raygunClient">The raygun client.</param>
/// <param name="raygunClient">The Raygun client.</param>
/// <param name="minimalLoggingLevel">The minimal logging level.</param>
public RaygunAbstractLogger(RaygunClient raygunClient, LoggingLevel minimalLoggingLevel = LoggingLevel.Verbose)
: base(minimalLoggingLevel)
: base(new AbstractLoggerSettings { MinimalLoggingLevel = minimalLoggingLevel })
{
_raygunClient = raygunClient ?? throw new ArgumentNullException(nameof(raygunClient));
}

/// <summary>
/// Initializes a new instance of the <see cref="RaygunAbstractLogger"/> class.
/// </summary>
/// <param name="raygunClient">The Raygun client.</param>
/// <param name="settings">The logger's settings.</param>
public RaygunAbstractLogger(RaygunClient raygunClient, AbstractLoggerSettings settings)
: base(settings ?? throw new ArgumentNullException(nameof(settings)))
{
_raygunClient = raygunClient ?? throw new ArgumentNullException(nameof(raygunClient));
}
Expand All @@ -41,7 +52,7 @@ public override void TraceMessage(string message, LoggingLevel loggingLevel, IEn
return;
}

IEnumerable<ILoggingParameter> loggingParameters = parameters.ToList();
IEnumerable<ILoggingParameter> loggingParameters = GetGlobalAndLocalLoggingParameters(parameters).ToList();
var messageException = new RaygunMessageException(message);
var data = ExtractDataValues(loggingParameters);
var tags = ExtractTags(loggingParameters, loggingLevel);
Expand All @@ -61,7 +72,7 @@ public override void TraceException(Exception exception, LoggingLevel loggingLev
return;
}

IEnumerable<ILoggingParameter> loggingParameters = parameters.ToList();
IEnumerable<ILoggingParameter> loggingParameters = GetGlobalAndLocalLoggingParameters(parameters).ToList();
var data = ExtractDataValues(loggingParameters);
var tags = ExtractTags(loggingParameters, loggingLevel);
_raygunClient.Send(exception, tags, data);
Expand Down
17 changes: 14 additions & 3 deletions Source/Vima.LoggingAbstractor.Sentry/SentryAbstractLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,18 @@ public class SentryAbstractLogger : AbstractLoggerBase, ISentryAbstractLogger
/// <param name="ravenClient">The raven client.</param>
/// <param name="minimalLoggingLevel">The minimal logging level.</param>
public SentryAbstractLogger(RavenClient ravenClient, LoggingLevel minimalLoggingLevel = LoggingLevel.Verbose)
: base(minimalLoggingLevel)
: base(new AbstractLoggerSettings { MinimalLoggingLevel = minimalLoggingLevel })
{
_ravenClient = ravenClient ?? throw new ArgumentNullException(nameof(ravenClient));
}

/// <summary>
/// Initializes a new instance of the <see cref="SentryAbstractLogger"/> class.
/// </summary>
/// <param name="ravenClient">The raven client.</param>
/// <param name="settings">The logger's settings.</param>
public SentryAbstractLogger(RavenClient ravenClient, AbstractLoggerSettings settings)
: base(settings ?? throw new ArgumentNullException(nameof(settings)))
{
_ravenClient = ravenClient ?? throw new ArgumentNullException(nameof(ravenClient));
}
Expand All @@ -42,7 +53,7 @@ public override void TraceMessage(string message, LoggingLevel loggingLevel, IEn
return;
}

IEnumerable<ILoggingParameter> loggingParameters = parameters.ToList();
IEnumerable<ILoggingParameter> loggingParameters = GetGlobalAndLocalLoggingParameters(parameters).ToList();
Dictionary<string, string> tags = GenerateTags(loggingParameters);
_ravenClient.Capture(new SentryEvent(message)
{
Expand All @@ -65,7 +76,7 @@ public override void TraceException(Exception exception, LoggingLevel loggingLev
return;
}

IEnumerable<ILoggingParameter> loggingParameters = parameters.ToList();
IEnumerable<ILoggingParameter> loggingParameters = GetGlobalAndLocalLoggingParameters(parameters).ToList();
Dictionary<string, string> tags = GenerateTags(loggingParameters);
_ravenClient.Capture(new SentryEvent(exception)
{
Expand Down

0 comments on commit 18c4b2e

Please sign in to comment.