Skip to content

Commit

Permalink
Change method trace string to have full namespace, method name and pa…
Browse files Browse the repository at this point in the history
…rameters.
  • Loading branch information
StrangeWill committed Aug 21, 2017
1 parent c6d010b commit 79135be
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 20 deletions.
12 changes: 11 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
# 0.6.0
# 0.6.2
<sup>Released: 2017/8/20</sup>

- Change method trace string to have full namespace, method name and parameters.

# 0.6.1
<sup>Released: 2017/8/20</sup>

- Fixed a bug where .NET Core Service extensions were excluded from the library.

# 0.6.0

## Features
<sup>Released: 2017/8/20</sup>
Expand Down
40 changes: 25 additions & 15 deletions src/RollbarDotNet/Builder/ExceptionBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,35 +1,37 @@
namespace RollbarDotNet.Builder
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Payloads;
using Trace = Payloads.Trace;
using Exception = System.Exception;

public class ExceptionBuilder : IExceptionBuilder
{
public void Execute(Payload payload, System.Exception exception)
public void Execute(Payload payload, Exception exception)
{
if(payload == null)
if (payload == null)
{
throw new System.ArgumentNullException(nameof(payload));
throw new ArgumentNullException(nameof(payload));
}

if(exception == null)
if (exception == null)
{
throw new System.ArgumentNullException(nameof(exception));
throw new ArgumentNullException(nameof(exception));
}

var traceChain = new List<Trace>();
var traceChain = new List<Payloads.Trace>();
this.BuildTraceList(exception, traceChain);
if(traceChain.Count > 0)
if (traceChain.Count > 0)
{
payload.Data.Body.TraceChain = traceChain;
}
}

protected void BuildTraceList(System.Exception exception, List<Trace> traceList)
protected void BuildTraceList(Exception exception, List<Payloads.Trace> traceList)
{
var trace = new Trace();
var trace = new Payloads.Trace();
trace.Exception = this.BuildException(exception);
trace.Frames = this.BuildFrames(exception);
traceList.Add(trace);
Expand All @@ -39,18 +41,26 @@ protected void BuildTraceList(System.Exception exception, List<Trace> traceList)
}
}

protected List<Frame> BuildFrames(System.Exception exception)
protected List<Frame> BuildFrames(Exception exception)
{
var frames = new List<Frame>();
var stacktrace = new StackTrace(exception, true);
foreach (var stackTraceFrame in stacktrace.GetFrames())
{
var method = stackTraceFrame.GetMethod();
var methodParameters = method.GetParameters();
var parameters = methodParameters.Length == 0
? string.Empty
: method.GetParameters()
.Select(p => $"{p.ParameterType.FullName} {p.Name}")
.Aggregate((p1, p2) => $"{p1}, {p2}");
var methodName = $"{method.DeclaringType.FullName}.{method.Name}({parameters})";
var frame = new Frame
{
Filename = stackTraceFrame.GetFileName(),
ColumnNumber = stackTraceFrame.GetFileColumnNumber(),
LineNumber = stackTraceFrame.GetFileLineNumber(),
Method = stackTraceFrame.GetMethod()?.ToString()
Method = methodName
};
frames.Add(frame);
}
Expand All @@ -63,12 +73,12 @@ protected List<Frame> BuildFrames(System.Exception exception)
return frames;
}

protected Exception BuildException(System.Exception exception)
protected Payloads.Exception BuildException(Exception exception)
{
var payloadException = new Exception();
var payloadException = new Payloads.Exception();
payloadException.Class = exception.GetType().Name;
payloadException.Message = exception.Message;
return payloadException;
}
}
}
}
2 changes: 1 addition & 1 deletion src/RollbarDotNet/RollbarDotNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<Description>Rollbar Integration for .NET Core</Description>
<Copyright>William Roush 2016-2017</Copyright>
<AssemblyTitle>RollbarDotNet</AssemblyTitle>
<VersionPrefix>0.6.1</VersionPrefix>
<VersionPrefix>0.6.2</VersionPrefix>
<Authors>William Roush</Authors>
<AssemblyName>RollbarDotNet</AssemblyName>
<PackageId>RollbarDotNet</PackageId>
Expand Down
11 changes: 8 additions & 3 deletions test/RollbarDotNet.Tests/Builder/ExceptionBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ public void SetsPayload()
{
try
{
throw new System.Exception("test exception");
this.ThrowException("", 0);
}
catch (System.Exception exception)
{
this.ExceptionBuilder.Execute(this.Payload, exception);
var payload = this.Payload.Data?.Body?.TraceChain?.FirstOrDefault();
Assert.Equal("Exception", payload?.Exception?.Class);
Assert.Equal("test exception", payload?.Exception ?.Message);
Assert.True(payload?.Frames?.Count == 1);
Assert.True(payload?.Frames?.Count == 2);
var frame = payload?.Frames?.FirstOrDefault();
Assert.Equal("Void SetsPayload()", frame?.Method);
Assert.Equal("RollbarDotNet.Tests.Builder.ExceptionBuilderTests.ThrowException(System.String a, System.Int32 b)", frame?.Method);
var stackTrace = new StackTrace(exception, true);
var stackTraceFrame = stackTrace.GetFrames().FirstOrDefault();
Assert.Equal(stackTraceFrame.GetFileColumnNumber(), frame?.ColumnNumber);
Expand All @@ -43,6 +43,11 @@ public void SetsPayload()
}
}

protected void ThrowException(string a, int b)
{
throw new System.Exception("test exception");
}

[Fact]
public void PayloadCannotBeNull()
{
Expand Down

0 comments on commit 79135be

Please sign in to comment.