Skip to content
Merged
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
8 changes: 4 additions & 4 deletions Src/CoreConsoleApp/CoreConsoleApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@

<ItemGroup>
<PackageReference Include="log4net" Version="2.0.7" />
<PackageReference Include="NLog" Version="5.0.0-beta05" />
<PackageReference Include="NLog" Version="4.5.0-rc02" />
<PackageReference Include="Serilog" Version="2.4.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="1.2.0-preview1-22736" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="1.2.0-preview1-22736" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.2.0-preview1-22736" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.1.1" />
</ItemGroup>

</Project>
4 changes: 2 additions & 2 deletions Src/CoreWebApp/CoreWebApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.0.2" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.0.2" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="1.0.2" />
<PackageReference Include="NLog" Version="5.0.0-beta05" />
<PackageReference Include="NLog.Extensions.Logging" Version="1.0.0-rtm-beta2" />
<PackageReference Include="NLog" Version="4.5.0-rc02" />
<PackageReference Include="NLog.Extensions.Logging" Version="1.0.0-rtm-rc4" />
<PackageReference Include="Serilog" Version="2.4.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="1.4.0" />
<PackageReference Include="Serilog.Extensions.Logging.File" Version="1.0.0" />
Expand Down
6 changes: 3 additions & 3 deletions Src/NLog.Targets.Stackify/NLog.Targets.Stackify.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<AssemblyTitle>NLog.Targets.Stackify</AssemblyTitle>
<VersionPrefix>2.0.0</VersionPrefix>
<TargetFrameworks>netstandard1.3;net40;net45</TargetFrameworks>
<TargetFrameworks>netstandard1.5;net40;net45</TargetFrameworks>
<AssemblyName>NLog.Targets.Stackify</AssemblyName>
<PackageId>NLog.Targets.Stackify</PackageId>
<PackageTags>stackify;errors;logs</PackageTags>
Expand All @@ -27,8 +27,8 @@
<ProjectReference Include="..\StackifyLib\StackifyLib.csproj" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.3' ">
<PackageReference Include="NLog" Version="5.0.0-beta05" />
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.5' ">
<PackageReference Include="NLog" Version="4.5.0-rc02" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net40' ">
Expand Down
117 changes: 61 additions & 56 deletions Src/NLog.Targets.Stackify/StackifyTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
#if NET45 || NET40
using System.Runtime.Remoting.Messaging;
#endif
using System.Security;
using System.Text;
using System.Threading.Tasks;
using StackifyLib;
using System.Diagnostics;
using StackifyLib.Internal.Logs;
Expand All @@ -31,6 +28,8 @@ public class StackifyTarget : TargetWithLayout
public string callContextKeys { get; set; }
public bool? logMethodNames { get; set; }
public bool? logAllParams { get; set; }
public bool? logAllProperties { get; set; }
public bool? logLastParameter { get; set; }

private List<string> _GlobalContextKeys = new List<string>();
private List<string> _MappedContextKeys = new List<string>();
Expand Down Expand Up @@ -99,32 +98,26 @@ protected override void Write(LogEventInfo logEvent)
{
StackifyAPILogger.Log(ex.ToString());
}

}


private Dictionary<string, object> GetDiagnosticContextProperties()
{


Dictionary<string, object> properties = new Dictionary<string, object>();


string ndc = NLog.NestedDiagnosticsContext.TopMessage;

if (!String.IsNullOrEmpty(ndc))
{
properties["ndc"] = ndc;
}


if (!_HasContextKeys)
{
return properties;
}

// GlobalDiagnosticsContext

foreach (string gdcKey in _GlobalContextKeys)
{
if (NLog.GlobalDiagnosticsContext.Contains(gdcKey))
Expand All @@ -137,8 +130,8 @@ private Dictionary<string, object> GetDiagnosticContextProperties()
}
}
}
// MappedDiagnosticsContext

// MappedDiagnosticsContext
foreach (string mdcKey in _MappedContextKeys)
{
if (NLog.MappedDiagnosticsContext.Contains(mdcKey))
Expand Down Expand Up @@ -170,7 +163,6 @@ private Dictionary<string, object> GetDiagnosticContextProperties()

internal LogMsg Translate(LogEventInfo loggingEvent)
{

if (loggingEvent == null)
return null;

Expand All @@ -180,14 +172,11 @@ internal LogMsg Translate(LogEventInfo loggingEvent)

StackifyLib.Models.LogMsg msg = new LogMsg();


if (loggingEvent.Level != null)
{
msg.Level = loggingEvent.Level.Name;
}



if (loggingEvent.HasStackTrace && loggingEvent.UserStackFrame != null)
{
var frame = loggingEvent.UserStackFrame;
Expand All @@ -201,10 +190,8 @@ internal LogMsg Translate(LogEventInfo loggingEvent)
msg.SrcLine = frame.GetFileLineNumber();
}
}

}


//if it wasn't set above for some reason we will do it this way as a fallback
if (string.IsNullOrEmpty(msg.SrcMethod))
{
Expand Down Expand Up @@ -240,47 +227,33 @@ internal LogMsg Translate(LogEventInfo loggingEvent)
msg.Msg = (formattedMessage ?? "").Trim();

object debugObject = null;
Dictionary<string, object> args = new Dictionary<string, object>();

if ((loggingEvent.Parameters != null) && (loggingEvent.Parameters.Length > 0))
if ((logAllProperties ?? true) && loggingEvent.Properties.Count > 0)
{

for (int i = 0; i < loggingEvent.Parameters.Length; i++)
Dictionary<string, object> args = new Dictionary<string, object>();
foreach (KeyValuePair<object, object> eventProperty in loggingEvent.Properties)
{
var item = loggingEvent.Parameters[i];

if (item == null)
string propertyKey = eventProperty.Key.ToString();
if (!string.IsNullOrEmpty(propertyKey))
{
continue;
}
else if (item is Exception)
{
if (loggingEvent.Exception == null)
{
loggingEvent.Exception = (Exception)item;
}
}
else if (item.ToString() == msg.Msg)
{
//ignore it.
}
else if (logAllParams ?? true)
{
args["arg" + i] = loggingEvent.Parameters[i];
debugObject = item;
}
else
{
debugObject = item;
args[propertyKey] = eventProperty.Value;
}
}

if ((logAllParams ?? true) && args != null && args.Count > 1)
if ((logAllParams ?? false) && loggingEvent.Parameters != null && loggingEvent.Parameters.Length > 0)
{
debugObject = CaptureParameters(loggingEvent, msg.Msg, args);
}
else
{
debugObject = args;
}
}

else if (loggingEvent.Parameters != null && loggingEvent.Parameters.Length > 0)
{
Dictionary<string, object> args = (logAllParams ?? true) ? new Dictionary<string, object>() : null;
debugObject = CaptureParameters(loggingEvent, msg.Msg, args);
}

StackifyError error = null;

Expand All @@ -300,13 +273,6 @@ internal LogMsg Translate(LogEventInfo loggingEvent)
diags.Remove("transid");
}








if (debugObject != null)
{
msg.data = StackifyLib.Utils.HelperFunctions.SerializeDebugData(debugObject, true, diags);
Expand All @@ -315,7 +281,6 @@ internal LogMsg Translate(LogEventInfo loggingEvent)
{
msg.data = StackifyLib.Utils.HelperFunctions.SerializeDebugData(null, false, diags);
}


if (msg.Msg != null && error != null)
{
Expand Down Expand Up @@ -357,11 +322,51 @@ internal LogMsg Translate(LogEventInfo loggingEvent)
msg.Msg += " #errorgoverned";
}


return msg;
}

private object CaptureParameters(LogEventInfo loggingEvent, string logMessage, Dictionary<string, object> args)
{
object debugObject = null;
if (args != null || (logLastParameter ?? true))
{
for (int i = 0; i < loggingEvent.Parameters.Length; i++)
{
var item = loggingEvent.Parameters[i];

if (item == null)
{
continue;
}
else if (item is Exception)
{
if (loggingEvent.Exception == null)
{
loggingEvent.Exception = (Exception)item;
}
}
else if (item.ToString() == logMessage)
{
//ignore it.
}
else if (args != null)
{
args["arg" + i] = loggingEvent.Parameters[i];
debugObject = item;
}
else
{
debugObject = item;
}
}

if (args != null && args.Count > 1)
{
debugObject = args;
}
}


return debugObject;
}
}
}
24 changes: 10 additions & 14 deletions Src/StackifyLib/Utils/HelperFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class HelperFunctions
{
static List<string> _BadTypes = new List<string>() { "log4net.Util.SystemStringFormat", "System.Object[]" };
static JsonSerializer serializer = new JsonSerializer { ReferenceLoopHandling = ReferenceLoopHandling.Ignore };
static JsonSerializerSettings serializerSettings = new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore, ReferenceLoopHandling = ReferenceLoopHandling.Ignore };

/// <summary>
/// Trying to serialize something that the user passed in. Sometimes this is used to serialize what we know is additional debug and sometimes it is the primary logged item. This is why the serializeSimpleTypes exists. For additional debug stuff we always serialize it. For the primary logged object we won't because it doesn't make any sense to put a string in the json as well as the main message. It's meant for objects.
Expand Down Expand Up @@ -126,21 +127,22 @@ public static string SerializeDebugData(object logObject, bool serializeSimpleTy
}
else
{

if (!typeInfo.ContainsGenericParameters)
{
jObject.Add("objectType", type.FullName);
}
#if NET451 || NET45 || NET40
else
{

#if NET451 || NET45 || NET40
var genericArgs = typeInfo.GetGenericArguments();

if (genericArgs.Any())
#else
var genericArgs = typeInfo.IsGenericTypeDefinition ?
type.GetTypeInfo().GenericTypeParameters :
type.GetTypeInfo().GenericTypeArguments;
#endif
if (genericArgs != null && genericArgs.Length > 0)
{
var childtype = genericArgs.First();

#if NET40
var childtypeinfo = childtype;
#else
Expand All @@ -161,7 +163,6 @@ public static string SerializeDebugData(object logObject, bool serializeSimpleTy
jObject.Add("objectType", type.FullName);
}
}
#endif
}
}
else if (token is JValue)
Expand All @@ -185,7 +186,7 @@ public static string SerializeDebugData(object logObject, bool serializeSimpleTy
}

string data = null;
if (properties != null && properties.Any())
if (properties != null && properties.Count > 0)
{

if (jObject == null)
Expand Down Expand Up @@ -221,12 +222,7 @@ public static string SerializeDebugData(object logObject, bool serializeSimpleTy

if (jObject != null)
{
return JsonConvert.SerializeObject(jObject,
new JsonSerializerSettings()
{
NullValueHandling = NullValueHandling.Ignore,
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
});
return JsonConvert.SerializeObject(jObject, serializerSettings);
}

return null;
Expand Down