Skip to content

Commit

Permalink
Merge pull request #48 from alphacloud/feature/46-uri Represent Uri a…
Browse files Browse the repository at this point in the history
…s string

Represent Uri as string, not destructure it, closes #46
  • Loading branch information
krajek authored Jan 27, 2018
2 parents 656a2b4 + 1c7d0c3 commit 4b75483
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ private object DestructureValue(object value, int level, IDictionary<object, IDi
return this.DestructureValueEnumerable(value, level, destructuredObjects, ref nextCyclicRefId);
}

if (typeof(Uri).GetTypeInfo().IsAssignableFrom(valueTypeInfo))
{
return this.DestructureUri((Uri)value);
}

return this.DestructureObject(value, valueType, level, destructuredObjects, ref nextCyclicRefId);
}

Expand All @@ -126,6 +131,11 @@ private object DestructureValueEnumerable(object value, int level, IDictionary<o
return resultList;
}

private object DestructureUri(Uri value)
{
return value.ToString();
}

private object DestructureValueDictionary(object value, int level, IDictionary<object, IDictionary<string, object>> destructuredObjects, ref int nextCyclicRefId)
{
if (destructuredObjects.ContainsKey(value))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace Serilog.Exceptions.Test.Destructurers
{
using System;
using System.Collections;
using System.Collections.Generic;
using Serilog.Exceptions.Core;
using Serilog.Exceptions.Destructurers;
Expand Down Expand Up @@ -52,6 +53,43 @@ public void Destructure_()
Assert.Contains(typeof(TestException).FullName, properties["Type"].ToString());
}

[Fact]
public void CanDestructureUriProperty()
{
const string uriValue = "http://localhost/property";
var exception = new UriException("test", new Uri(uriValue));

var propertiesBag = new ExceptionPropertiesBag(exception);
this.destructurer.Destructure(exception, propertiesBag, null);

var properties = propertiesBag.GetResultDictionary();
var uriPropertyValue = properties[nameof(UriException.Uri)];
Assert.IsType<string>(uriPropertyValue);
Assert.Equal(uriValue, uriPropertyValue);
}

[Fact]
public void CanDestructureUriDataItem()
{
const string uriValue = "http://localhost/data-item";
var exception = new Exception("test")
{
Data =
{
{ "UriDataItem", new Uri(uriValue) }
}
};

var propertiesBag = new ExceptionPropertiesBag(exception);
this.destructurer.Destructure(exception, propertiesBag, null);

var properties = propertiesBag.GetResultDictionary();
var data = (IDictionary)properties[nameof(Exception.Data)];
var uriDataValue = data["UriDataItem"];
Assert.IsType<string>(uriDataValue);
Assert.Equal(uriValue, uriDataValue);
}

public class TestException : Exception
{
public TestException()
Expand Down Expand Up @@ -84,5 +122,16 @@ public string this[int i]
get { return "IndexerValue"; }
}
}

public class UriException : Exception
{
public UriException(string message, Uri uri)
: base(message)
{
this.Uri = uri;
}

public Uri Uri { get; }
}
}
}

0 comments on commit 4b75483

Please sign in to comment.