Setting apikey programmatically (based on HTTP header)

Alec

Posted on
Jul 29 2015

I can do this in my ASP.Net / ASP.Net MVC handler as I just create the RaygunClient myself and can create it with a key overriding the RaygunSettings tag.

However, for WebAPI it seems hooked up much more magically and it's not clear how I could do this there.

I am doing this so I can send errors from our automation test suite to a different RayGun app than manual testing to make things clearer.


Jason Fauchelle

Raygun

Posted on
Jul 29 2015

Hi Alec,

One way to do this would be to use config transforms - so that a different API key is used for different environments. If this will not work for you, or you'd like to avoid config transforms, then you can use one of the RaygunWebApiClient.Attach overloads that takes a function. Something like this:

private static RaygunWebApiClient _client;

RaygunWebApiClient.Attach(config, (message) =>
{
  if (_client == null)
  {
    _client = new RaygunWebApiClient();
  }
  return _client;
});

Except instead of a Single client, you'd build/pick an appropriate client (with different api keys) based on the Headers of the HttpRequestMessage (message parameter).

Hope that helps, let me know if you have further questions about this.

-Jason Fauchelle


Alec

Posted on
Aug 02 2015

That did the trick! Thanks.

I couldn't see that signature in the documentation unless I missed it.


Jason Fauchelle

Raygun

Posted on
Aug 03 2015

Hi Alec,

Great to hear that works for you. No you didn't miss it - it's not in the documentation yet, but I've noted down to add it in soon.

-Jason Fauchelle


Reply