MindscapeHQ / raygun4net

Raygun provider for .NET
https://raygun.com
MIT License
125 stars 91 forks source link

NotSupportedException when adding a tag from ASP.NET middleware #466

Closed dougwaldron closed 2 years ago

dougwaldron commented 2 years ago

When my application throws an exception that is logged by Raygun, I get a second exception report from the Raygun client itself that occurs when my Raygun Client Provider tries to add a tag:

public class RaygunClientProvider : DefaultRaygunAspNetCoreClientProvider
{
    public override RaygunClient GetClient(RaygunSettings settings, HttpContext context)
    {
        var client = base.GetClient(settings, context);

        client.SendingMessage += (_, args) =>
        {
            args.Message.Details.Tags ??= new List<string>();
            args.Message.Details.Tags.Add(Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"));
        };

        return client;
    }
}

The call to Tags.Add() throws System.NotSupportedException: "Collection was of a fixed size." The stack trace from the Raygun report:

{
  "Error": {
    "Data": {},
    "ClassName": "System.NotSupportedException",
    "Message": "Collection was of a fixed size.",
    "StackTrace": [
      {
        "LineNumber": 11,
        "ClassName": "System.ThrowHelper",
        "MethodName": "ThrowNotSupportedException(ExceptionResource resource)"
      },
      {
        "LineNumber": 0,
        "ClassName": "System.SZArrayHelper",
        "MethodName": "Add[T](T value)"
      },
      {
        "LineNumber": 17,
        "ClassName": "WebApp.Platform.Raygun.RaygunClientProvider+<>c",
        "FileName": "D:\\projects\\reports\\src\\WebApp\\Platform\\Raygun\\RaygunClientProvider.cs",
        "MethodName": "<GetClient>b__0_0(Object _, RaygunSendingMessageEventArgs args)"
      },
      {
        "LineNumber": 27,
        "ClassName": "Mindscape.Raygun4Net.RaygunClientBase",
        "MethodName": "OnSendingMessage(RaygunMessage raygunMessage)"
      }
    ]
  }
}

I have another, very similar application with the same Client Provider where this issue does not occur. The only relevant difference between them that I can think of is that this app has <Nullable>enable</Nullable> in the "csproj" file. Could it be that with nullable reference types enabled, the Tags property is getting initialized with a fixed-size data structure, like an Array?

darcythomas commented 2 years ago

I will have a look into this for you today.

darcythomas commented 2 years ago

Ok so I worked out what is going on.

Somewhere else in your app you are probably using raygunClient.Send(e,new []{"SomeOtherTag"}); The IList (,new []{"SomeOtherTag"}) added in the raygunClient.Send() has a fixed length. Which is giving your error.

So as a workaround you could change:

raygunClient.Send(e,new []{"SomeOtherTag"});

To;

raygunClient.Send(e,new List<string>{"SomeOtherTag"});

I will see about making a contribution, to safely convert new []{} into a List<String> inside the .Send() method.

dougwaldron commented 2 years ago

I appreciate your looking into this!

I don't manually send exceptions anywhere in the application, and I don't add tags anywhere other than the client provider posted above. So I went back to check on Raygun for the original exception and discovered it came through with a single tag:

    "Tags": [
        "UnhandledException"
    ],

Looking for this tag in the Raygun middleware shows the tag is indeed getting added as a fixed-size array:

https://github.com/MindscapeHQ/raygun4net/blob/94235f7af4bfebcb309c88df5be29e13b2649797/Mindscape.Raygun4Net.AspNetCore/RaygunAspNetMiddleware.cs#L21

https://github.com/MindscapeHQ/raygun4net/blob/94235f7af4bfebcb309c88df5be29e13b2649797/Mindscape.Raygun4Net.AspNetCore/RaygunAspNetMiddleware.cs#L84

Fixing that line should do the trick! Let me know if I can submit a PR.

darcythomas commented 2 years ago

Yes please submit a PR! I'll get that approved and deployed asap.

darcythomas commented 2 years ago

This fix should be available on NuGet shortly

dougwaldron commented 2 years ago

Thanks again, much appreciated!