Skip to content

Commit

Permalink
#59 Logging; #47 Settings
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkusMeyer13 committed Feb 18, 2020
1 parent ee4e9c2 commit 9958b08
Show file tree
Hide file tree
Showing 17 changed files with 679 additions and 338 deletions.
27 changes: 27 additions & 0 deletions PlanB.Butler.Services/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) PlanB. GmbH. All Rights Reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

namespace PlanB.Butler.Services
{
/// <summary>
/// Constants.
/// </summary>
internal static class Constants
{
/// <summary>
/// Gets or sets the name of the butler correlation trace.
/// </summary>
/// <value>
/// The name of the butler correlation trace.
/// </value>
internal static string ButlerCorrelationTraceName { get; set; }

/// <summary>
/// Gets or sets the butler correlation trace header.
/// </summary>
/// <value>
/// The butler correlation trace header.
/// </value>
internal static string ButlerCorrelationTraceHeader { get; set; }
}
}
30 changes: 0 additions & 30 deletions PlanB.Butler.Services/ConvertByteArrayToString.cs

This file was deleted.

113 changes: 113 additions & 0 deletions PlanB.Butler.Services/Extensions/LoggerExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// Copyright (c) PlanB. GmbH. All Rights Reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

using System;
using System.Collections.Generic;

using Microsoft.Extensions.Logging;

namespace PlanB.Butler.Services.Extensions
{
/// <summary>
/// The ILogger Extensions.
/// </summary>
public static class LoggerExtension
{
/// <summary>
/// Informations the specified event identifier.
/// </summary>
/// <param name="log">The log.</param>
/// <param name="butlerCorrelationId">The Butler correlation identifier.</param>
/// <param name="message">The message.</param>
/// <param name="trace">The trace.</param>
public static void LogInformation(this ILogger log, Guid butlerCorrelationId, string message, IDictionary<string, string> trace)
{
if (log == null)
{
return;
}

EventId eventId = new EventId(butlerCorrelationId.GetHashCode(), Constants.ButlerCorrelationTraceName);

var state = new Dictionary<string, object>
{
{ Constants.ButlerCorrelationTraceName, butlerCorrelationId },
{ "Message", message },
};

var rator = trace.GetEnumerator();
while (rator.MoveNext())
{
if (!state.ContainsKey(rator.Current.Key))
{
state.Add(rator.Current.Key, rator.Current.Value);
}
}

log.Log(LogLevel.Information, eventId, state, null, Formatter);
}

/// <summary>
/// Logs the error.
/// </summary>
/// <param name="log">The log.</param>
/// <param name="correlationId">The correlation identifier.</param>
/// <param name="message">The message.</param>
/// <param name="trace">The trace.</param>
/// <param name="ex">The exeception.</param>
public static void LogError(this ILogger log, Guid correlationId, string message, IDictionary<string, string> trace, Exception ex = null)
{
if (log == null)
{
return;
}

EventId eventId = new EventId(correlationId.GetHashCode(), Constants.ButlerCorrelationTraceName);

var state = new Dictionary<string, object>
{
{ Constants.ButlerCorrelationTraceName, correlationId },
{ "Message", message },
};

var rator = trace.GetEnumerator();
while (rator.MoveNext())
{
if (!state.ContainsKey(rator.Current.Key))
{
state.Add(rator.Current.Key, rator.Current.Value);
}
}

if (ex == null)
{
ex = new Exception(message);
}

log.Log(LogLevel.Error, eventId, state, ex, Formatter);
}

/// <summary>
/// Style of logging.
/// </summary>
/// <typeparam name="T">State.</typeparam>
/// <param name="state">State Param.</param>
/// <param name="ex">Exception.</param>
/// <returns>Message.</returns>
internal static string Formatter<T>(T state, Exception ex)
{
if (ex != null)
{
return ex.ToString();
}

Dictionary<string, object> stateDictionary = state as Dictionary<string, object>;
if (stateDictionary != null && stateDictionary.TryGetValue("Message", out var message))
{
return message?.ToString() ?? string.Empty;
}

return string.Empty;
}
}
}
87 changes: 87 additions & 0 deletions PlanB.Butler.Services/Extensions/NewtonsoftExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright (c) PlanB. GmbH. All Rights Reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

using System.Collections.Generic;

using Newtonsoft.Json.Linq;

namespace PlanB.Butler.Services.Extensions
{
/// <summary>
/// NewtonsoftExtension.
/// </summary>
public static class NewtonsoftExtension
{
/// <summary>
/// Sanitizes a JObject.
/// </summary>
/// <param name="obj">JObject.</param>
/// <param name="removeMetaTags">Indicates whether 'id' and 'lastUpdate' fields should be removed.</param>
public static void Sanitize(this JObject obj, bool removeMetaTags = false)
{
List<JToken> metaData = new List<JToken>();
foreach (JToken token in obj.Descendants())
{
try
{
JProperty propertyToken = (JProperty)token;
if (propertyToken.Name.StartsWith("_"))
{
metaData.Add(token);
}

if (removeMetaTags)
{
if (propertyToken.Name.Equals("id") || propertyToken.Name.Equals("lastUpdate"))
{
metaData.Add(token);
}
}
}

// Some Properties may not get Serialized, just skip them
catch
{
}
}

metaData.ForEach(n => n.Remove());
}

/// <summary>
/// Sanitizes a JArray.
/// </summary>
/// <param name="obj">JArray.</param>
/// <param name="removeDocumentMetaData">Indicates whether 'id' and 'lastUpdate' should be removed.</param>
public static void Sanitize(this JArray obj, bool removeDocumentMetaData = false)
{
List<JToken> metaData = new List<JToken>();
foreach (JToken token in obj.Descendants())
{
try
{
JProperty propertyToken = (JProperty)token;
if (propertyToken.Name.StartsWith("_"))
{
metaData.Add(token);
}

if (removeDocumentMetaData)
{
if (propertyToken.Name.Equals("id") || propertyToken.Name.Equals("lastUpdate"))
{
metaData.Add(token);
}
}
}

// Some Properties may not get Serialized, just skip them
catch
{
}
}

metaData.ForEach(n => n.Remove());
}
}
}
36 changes: 0 additions & 36 deletions PlanB.Butler.Services/Finance.cs

This file was deleted.

Loading

0 comments on commit 9958b08

Please sign in to comment.