Skip to content

Commit

Permalink
Fixed Exception StackTrace formatting to handle missing StackFrames (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
snakefoot authored and russcam committed Jun 1, 2021
1 parent 1242026 commit b7b9895
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/Elastic.Apm.NLog/Elastic.Apm.NLog.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFrameworks>netstandard2.0;net461</TargetFrameworks>
<Title>Elastic APM NLog Layout Renderers</Title>
<Description>Enrich NLog log messages with APM TraceId and TransactionId.</Description>
<IsPackable>True</IsPackable>
Expand Down
16 changes: 10 additions & 6 deletions src/Elastic.CommonSchema.NLog/EcsLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using NLog;
using NLog.Config;
Expand Down Expand Up @@ -193,8 +191,11 @@ private static string CatchError(Exception error)
fullText.WriteLine($"Source: {error.TargetSite?.DeclaringType?.AssemblyQualifiedName}");
fullText.WriteLine($"Message: {error.Message}");
fullText.WriteLine($"Trace: {error.StackTrace}");
fullText.WriteLine($"Location: {frame.GetFileName()}");
fullText.WriteLine($"Method: {frame.GetMethod()} ({frame.GetFileLineNumber()}, {frame.GetFileColumnNumber()})");
if (frame != null)
{
fullText.WriteLine($"Location: {frame.GetFileName()}");
fullText.WriteLine($"Method: {frame.GetMethod()} ({frame.GetFileLineNumber()}, {frame.GetFileColumnNumber()})");
}

var exception = error.InnerException;
while (exception != null)
Expand All @@ -205,8 +206,11 @@ private static string CatchError(Exception error)
fullText.WriteLine($"\tSource: {exception.TargetSite?.DeclaringType?.AssemblyQualifiedName}");
fullText.WriteLine($"\tMessage: {exception.Message}");
fullText.WriteLine($"\tTrace: {exception.StackTrace}");
fullText.WriteLine($"\tLocation: {frame.GetFileName()}");
fullText.WriteLine($"\tMethod: {frame.GetMethod()} ({frame.GetFileLineNumber()}, {frame.GetFileColumnNumber()})");
if (frame != null)
{
fullText.WriteLine($"\tLocation: {frame.GetFileName()}");
fullText.WriteLine($"\tMethod: {frame.GetMethod()} ({frame.GetFileLineNumber()}, {frame.GetFileColumnNumber()})");
}

exception = exception.InnerException;
}
Expand Down
54 changes: 53 additions & 1 deletion src/Elastic.CommonSchema.NLog/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ filesystem target and [Elastic Filebeat](https://www.elastic.co/downloads/beats/
</extensions>
<targets>
<target name="console" type="console">
<layout xsi:type="EcsLayout" />
<layout xsi:type="EcsLayout">
<metadata name="MyProperty" layout="MyPropertyValue" /> <!-- repeated, optional -->
<label name="MyLabel" layout="MyLabelValue" /> <!-- repeated, optional -->
<tag layout="MyTagValue" /> <!-- repeated, optional -->
</layout>
</target>
</targets>
<rules>
Expand All @@ -41,6 +45,54 @@ filesystem target and [Elastic Filebeat](https://www.elastic.co/downloads/beats/
</nlog>
```

## EcsLayout Parameter Options

* **Metadata Options**
- _IncludeAllProperties_ - Include LogEvent properties as metadata. Default: `true`
- _IncludeMdlc_ - Include NLog Scope Context Properties as metadata. Default: `false`
- _ExcludeProperties_ - Comma separated string with names which properties to exclude.

* **Event Options**
- _EventAction_ -
- _EventCategory_ -
- _EventId_ -
- _EventKind_ -
- _EventSeverity_ -

* **Agent Options**
- _AgentId_ -
- _AgentName_ -
- _AgentType_ -
- _AgentVersion_ -

* **Process Options**
- _ProcessExecutable_ - Default: `${processname:FullName=true}`
- _ProcessId_ - Default: `${processid}`
- _ProcessName_ - Default: `${processname:FullName=false}`
- _ProcessThreadId_ - Default: `${threadid}`
- _ProcessTitle_ - Default: `${processinfo:MainWindowTitle}`

* **Server Options**
- _ServerAddress_ -
- _ServerIp_ -
- _ServerUser_ - Default: `${environment-user}`

* **Host Options**
- _HostId_ -
- _HostIp_ - Default: `${local-ip:cachedSeconds=60}`
- _HostName_ - Default: `${machinename}`

* **Log Origin Options**
- _LogOriginCallSiteMethod_ - Default: `${exception:format=method}`
- _LogOriginCallSiteFile_ - Default: `${exception:format=source}`
- _LogOriginCallSiteLine_ -

* **Trace Options**
- _ApmTraceId_ - Default: `${ElasticApmTraceId}`

* **Transaction Options**
- _ApmTransactionId_ - Default: `${ElasticApmTransactionId}`

## Example output from EcsLayout
An example of the output is given below:

Expand Down
16 changes: 10 additions & 6 deletions src/Elastic.CommonSchema.Serilog/LogEventConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -337,9 +337,11 @@ private static string CatchErrors(IReadOnlyCollection<Exception> errors)
fullText.WriteLine($"Source: {error.TargetSite?.DeclaringType?.AssemblyQualifiedName}");
fullText.WriteLine($"Message: {error.Message}");
fullText.WriteLine($"Trace: {error.StackTrace}");
fullText.WriteLine($"Location: {frame.GetFileName()}");
fullText.WriteLine(
$"Method: {frame.GetMethod()} ({frame.GetFileLineNumber()}, {frame.GetFileColumnNumber()})");
if (frame != null)
{
fullText.WriteLine($"Location: {frame.GetFileName()}");
fullText.WriteLine($"Method: {frame.GetMethod()} ({frame.GetFileLineNumber()}, {frame.GetFileColumnNumber()})");
}

var exception = error.InnerException;
while (exception != null)
Expand All @@ -350,9 +352,11 @@ private static string CatchErrors(IReadOnlyCollection<Exception> errors)
fullText.WriteLine($"\tSource: {exception.TargetSite?.DeclaringType?.AssemblyQualifiedName}");
fullText.WriteLine($"\tMessage: {exception.Message}");
fullText.WriteLine($"\tTrace: {exception.StackTrace}");
fullText.WriteLine($"\tLocation: {frame.GetFileName()}");
fullText.WriteLine(
$"\tMethod: {frame.GetMethod()} ({frame.GetFileLineNumber()}, {frame.GetFileColumnNumber()})");
if (frame != null)
{
fullText.WriteLine($"\tLocation: {frame.GetFileName()}");
fullText.WriteLine($"\tMethod: {frame.GetMethod()} ({frame.GetFileLineNumber()}, {frame.GetFileColumnNumber()})");
}

exception = exception.InnerException;
}
Expand Down

0 comments on commit b7b9895

Please sign in to comment.