-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ee4e9c2
commit 9958b08
Showing
17 changed files
with
679 additions
and
338 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.