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

Add missing handling for LoggingEvent properties #7

Merged

harmonized how event properties and contexts are handled.

  • Loading branch information
Asbjørn Hansen
Asbjørn Hansen committed Mar 9, 2017
commit 37baefdbe87009d3981afbec4dfc2160c239cf7c
@@ -49,6 +49,21 @@ public virtual string ToJson(string renderedLog, DateTime timeStamp)
return ParseRenderedLog(renderedLog, timeStamp);
}

private static bool TryGetPropertyValue(object property, out object propertyValue)
{
var fixedProperty = property as IFixingRequired;
if (fixedProperty != null && fixedProperty.GetFixedObject() != null)
{
propertyValue = fixedProperty.GetFixedObject();
}
else
{
propertyValue = property;
}

return propertyValue != null;
}

/// <summary>
/// Returns the exception information. Also takes care of the InnerException.
/// </summary>
@@ -92,7 +107,7 @@ private string GetMessageAndObjectInfo(LoggingEvent loggingEvent, out object obj
{
var message = string.Empty;
objInfo = null;
var bytesLengthAllowdToLoggly = EVENT_SIZE;
var bytesLengthAllowedToLoggly = EVENT_SIZE;

if (loggingEvent.MessageObject != null)
{
@@ -103,9 +118,9 @@ private string GetMessageAndObjectInfo(LoggingEvent loggingEvent, out object obj
{
message = loggingEvent.MessageObject.ToString();
var messageSizeInBytes = Encoding.Default.GetByteCount(message);
if (messageSizeInBytes > bytesLengthAllowdToLoggly)
if (messageSizeInBytes > bytesLengthAllowedToLoggly)
{
message = message.Substring(0, bytesLengthAllowdToLoggly);
message = message.Substring(0, bytesLengthAllowedToLoggly);
}
}
else
@@ -181,90 +196,59 @@ private string PreParse(LoggingEvent loggingEvent)
loggingInfo.exception = exceptionInfo;
}

var properties = (IDictionary<string, object>) loggingInfo;

//handling loggingevent properties
if (loggingEvent.Properties.Count > 0)
{
var properties = (IDictionary<string, object>)loggingInfo;
foreach (DictionaryEntry property in loggingEvent.Properties)
{
var fixedProperty = property.Value as IFixingRequired;
object propertyValue;
if (fixedProperty != null && fixedProperty.GetFixedObject() != null)
if (TryGetPropertyValue(property.Value, out propertyValue))
{
propertyValue = fixedProperty.GetFixedObject();
properties[(string) property.Key] = propertyValue;
}
else
{
propertyValue = property.Value;
}

properties[(string)property.Key] = propertyValue;
}
}

//handling threadcontext properties
var threadContextProperties = ThreadContext.Properties.GetKeys();
if (threadContextProperties != null && threadContextProperties.Any())
{
var p = (IDictionary<string, object>)loggingInfo;
foreach (var key in threadContextProperties)
{
var fixingRequired = ThreadContext.Properties[key] as IFixingRequired;
if (fixingRequired != null && fixingRequired.GetFixedObject() != null)
{
p[key] = fixingRequired.GetFixedObject();
}
else
object propertyValue;
if (TryGetPropertyValue(ThreadContext.Properties[key], out propertyValue))
{
p[key] = ThreadContext.Properties[key].ToString();
properties[key] = propertyValue;
}
}
}

//handling logicalthreadcontext properties
if (Config.LogicalThreadContextKeys != null)
{
var ltp = (IDictionary<string, object>)loggingInfo;
var logicalThreadContextProperties = Config.LogicalThreadContextKeys.Split(',');
foreach (var key in logicalThreadContextProperties)
{
if (LogicalThreadContext.Properties[key] == null)
{
continue;
}

var fixingRequired = LogicalThreadContext.Properties[key] as IFixingRequired;
if (fixingRequired != null && fixingRequired.GetFixedObject() != null)
{
ltp[key] = fixingRequired.GetFixedObject();
}
else
object propertyValue;
if (TryGetPropertyValue(LogicalThreadContext.Properties[key], out propertyValue))
{
ltp[key] = LogicalThreadContext.Properties[key].ToString();
properties[key] = propertyValue;
}
}
}

//handling globalcontext properties
if (Config.GlobalContextKeys != null)
{
var gcp = (IDictionary<string, object>)loggingInfo;
var globalContextProperties = Config.GlobalContextKeys.Split(',');
foreach (var key in globalContextProperties)
{
if (GlobalContext.Properties[key] == null)
{
continue;
}

var fixingRequired = GlobalContext.Properties[key] as IFixingRequired;
if (fixingRequired != null && fixingRequired.GetFixedObject() != null)
{
gcp[key] = fixingRequired.GetFixedObject();
}
else
object propertyValue;
if (TryGetPropertyValue(GlobalContext.Properties[key], out propertyValue))
{
gcp[key] = GlobalContext.Properties[key].ToString();
properties[key] = propertyValue;
}
}
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.