Logging invocations with errors would throw inside logger (#1445)

This commit is contained in:
BrennanConroy 2018-02-13 14:57:21 -08:00 committed by GitHub
parent e7520904da
commit d9b32ee323
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 2 deletions

View File

@ -69,7 +69,16 @@ namespace Microsoft.AspNetCore.SignalR.Internal.Protocol
public override string ToString()
{
return $"InvocationMessage {{ {nameof(InvocationId)}: \"{InvocationId}\", {nameof(Target)}: \"{Target}\", {nameof(Arguments)}: [ {string.Join(", ", Arguments?.Select(a => a?.ToString())) ?? string.Empty } ] }}";
string args;
try
{
args = string.Join(", ", Arguments?.Select(a => a?.ToString())) ?? string.Empty;
}
catch (Exception ex)
{
args = $"Error: {ex.Message}";
}
return $"InvocationMessage {{ {nameof(InvocationId)}: \"{InvocationId}\", {nameof(Target)}: \"{Target}\", {nameof(Arguments)}: [ {args} ] }}";
}
}
@ -86,7 +95,16 @@ namespace Microsoft.AspNetCore.SignalR.Internal.Protocol
public override string ToString()
{
return $"StreamInvocation {{ {nameof(InvocationId)}: \"{InvocationId}\", {nameof(Target)}: \"{Target}\", {nameof(Arguments)}: [ {string.Join(", ", Arguments?.Select(a => a?.ToString())) ?? string.Empty} ] }}";
string args;
try
{
args = string.Join(", ", Arguments?.Select(a => a?.ToString())) ?? string.Empty;
}
catch (Exception ex)
{
args = $"Error: {ex.Message}";
}
return $"StreamInvocation {{ {nameof(InvocationId)}: \"{InvocationId}\", {nameof(Target)}: \"{Target}\", {nameof(Arguments)}: [ {args} ] }}";
}
}
}

View File

@ -0,0 +1,29 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Runtime.ExceptionServices;
using Microsoft.AspNetCore.SignalR.Internal.Protocol;
using Xunit;
namespace Microsoft.AspNetCore.SignalR.Tests
{
public class HubMethodInvocationMessageTests
{
[Fact]
public void InvocationMessageToStringPutsErrorInArgs()
{
var hubMessage = new InvocationMessage("echo", ExceptionDispatchInfo.Capture(new Exception("test")), null);
Assert.Equal("InvocationMessage { InvocationId: \"\", Target: \"echo\", Arguments: [ Error: test ] }", hubMessage.ToString());
}
[Fact]
public void StreamInvocationMessageToStringPutsErrorInArgs()
{
var hubMessage = new StreamInvocationMessage("3", "echo", ExceptionDispatchInfo.Capture(new Exception("test")), null);
Assert.Equal("StreamInvocation { InvocationId: \"3\", Target: \"echo\", Arguments: [ Error: test ] }", hubMessage.ToString());
}
}
}