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

Update timestamp format to GELF 1.1 #6

Closed
wants to merge 3 commits into from
Closed
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ msbuild.wrn

# Visual Studio 2015
.vs/
/NLog.Web.AspNetCore.Targets.Gelf.ConsoleRunner/Logs/internal-nlog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,9 @@
<ItemGroup>
<ProjectReference Include="..\NLog.Web.AspNetCore.Targets.Gelf\NLog.Web.AspNetCore.Targets.Gelf.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="nlog.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
8 changes: 4 additions & 4 deletions NLog.Web.AspNetCore.Targets.Gelf.ConsoleRunner/nlog.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true"
throwExceptions="false"
internalLogLevel="Off"
internalLogFile="c:\temp\internal-nlog.txt">
internalLogLevel="Trace"
internalLogFile="Logs\internal-nlog.txt">
<extensions>
<add assembly="NLog.Web.AspNetCore"/>
<add assembly="NLog.Web.AspNetCore.Targets.Gelf"/>
</extensions>
<targets>
<target xsi:type="File" name="debugFile" filename="C:\@Logs\${shortdate}-${level}-${applicationName}.txt" layout="${longdate}|${level:upperCase=true}|${logger}|${aspnet-Request-Method}|url: ${aspnet-Request-Url}${aspnet-Request-QueryString}|${message}" concurrentWrites="false" />
<target xsi:type="Gelf" name="graylog" endpoint="udp://192.168.99.100:12201" facility="console-runner" SendLastFormatParameter="true" />
<target xsi:type="File" name="debugFile" filename="Logs\${shortdate}${applicationName}.txt" layout="${longdate}|${level:upperCase=true}|${logger}|${aspnet-Request-Method}|url: ${aspnet-Request-Url}${aspnet-Request-QueryString}|${message}" concurrentWrites="false" />
<target xsi:type="Gelf" name="graylog" endpoint="udp://192.168.1.153:12201" facility="console-runner" SendLastFormatParameter="true" />
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="debugFile, graylog" />
Expand Down
42 changes: 29 additions & 13 deletions NLog.Web.AspNetCore.Targets.Gelf/Target/GelfConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public JObject GetGelfJson(LogEventInfo logEventInfo, string facility)

logEventInfo.Properties.Add("ExceptionSource", logEventInfo.Exception.Source);
logEventInfo.Properties.Add("ExceptionMessage", exceptionDetail);
logEventInfo.Properties.Add("ExceptionType", logEventInfo.Exception?.GetType().FullName);
logEventInfo.Properties.Add("StackTrace", stackDetail);
}

Expand All @@ -48,7 +49,7 @@ public JObject GetGelfJson(LogEventInfo logEventInfo, string facility)
Host = Dns.GetHostName(),
ShortMessage = shortMessage,
FullMessage = logEventMessage,
Timestamp = logEventInfo.TimeStamp,
Timestamp = new DateTimeOffset(logEventInfo.TimeStamp).ToUnixTimeMilliseconds() /1000.0,
Level = GetSeverityLevel(logEventInfo.Level),
//Spec says: facility must be set by the client to "GELF" if empty
Facility = (string.IsNullOrEmpty(facility) ? "GELF" : facility),
Expand Down Expand Up @@ -131,28 +132,32 @@ private static void AddAdditionalField(IDictionary<string, JToken> jObject, KeyV
/// <returns></returns>
private static int GetSeverityLevel(LogLevel level)
{
if (level == LogLevel.Debug)
if (level == LogLevel.Trace)
{
return 7;
}
if (level == LogLevel.Fatal)
if (level == LogLevel.Debug)
{
return 2;
return 7;
}
if (level == LogLevel.Info)
{
return 6;
}
if (level == LogLevel.Trace)
{
return 6;
}
if (level == LogLevel.Warn)
{
return 4;
}
if (level == LogLevel.Error)
{
return 3;
}
if (level == LogLevel.Fatal)
{
return 2;
}

return 3; //LogLevel.Error
return 7; //LogLevel.Off ?
}

/// <summary>
Expand All @@ -171,17 +176,28 @@ private void GetExceptionMessages(Exception ex, out string exceptionDetail, out
int counter = 0;
do
{
exceptionSb.Append(nestedException.Message + " - ");
exceptionSb.Append(nestedException.GetType().FullName)
.Append(" => ")
.Append(nestedException.Message);

if(nestedException.InnerException != null)
exceptionSb.Append(" - ");

if (nestedException.StackTrace != null)
stackSb.Append(nestedException.StackTrace + "--- Inner exception stack trace ---");
{
stackSb.Append(nestedException.StackTrace);
if (nestedException.InnerException != null)
stackSb.Append("--- Inner exception stack trace ---");
}

nestedException = nestedException.InnerException;
counter++;
}
while (nestedException != null && counter < 11);

exceptionDetail = exceptionSb.ToString().Substring(0, exceptionSb.Length - 3);
exceptionDetail = exceptionSb.ToString();
if (stackSb.Length > 0)
stackDetail = stackSb.ToString().Substring(0, stackSb.Length - 35);
stackDetail = stackSb.ToString();
}
}
}
2 changes: 1 addition & 1 deletion NLog.Web.AspNetCore.Targets.Gelf/Target/GelfMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class GelfMessage
public string ShortMessage { get; set; }

[JsonProperty("timestamp")]
public DateTime Timestamp { get; set; }
public double Timestamp { get; set; }

[JsonProperty("version")]
public string Version { get; set; }
Expand Down