Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • jake williamson 207 posts 872 karma points
    Jun 30, 2022 @ 02:35
    jake williamson
    0

    How to add a custom RayGun ILogger implementation to a v9 site to log handled exceptions

    hey out there,

    we're trying to add a RayGun logger to a v9 site to catch handled exceptions, e.g. where we've something like the following:

    private readonly ILogger<MyController> _logger;
    ...
    try
    {
        throw new Exception("oops! i threw an exception...");
    }
    catch (Exception ex)
    {
        _logger.LogError(ex, "log error testing");
    }
    

    back in v8 days, we followed these instructions:

    Full C# control over Serilog configuration

    however there isn't an equivalent in v9 in the docos. we're guessing this is due to v9 now using the Microsoft.Extensions.Logging.ILogger interface.

    we've been experimenting and the closest we can find to what we're trying to achieve is this blog post:

    https://www.iambacon.co.uk/blog/logging-exceptions-to-raygun-from-your-blazor-server-app

    although we've followed the instructions to the letter and we get into this piece of code in the RaygunLoggerProvider:

    public ILogger CreateLogger(string categoryName)
    {
        return _loggers.GetOrAdd(categoryName, x => new RaygunLogger(categoryName, _config));
    }
    

    but we never actually hit our logger...

    has anyone else done something like this, i.e. added their own implementation for the ILogger interface, added it to the logger collection and hit their code?!

    as ever, any suggestions would be really appreciated!

  • jake williamson 207 posts 872 karma points
    Jul 20, 2023 @ 00:28
    jake williamson
    100

    after some more hunting, we finally found the solution!

    no code changes required, the trick is to add the following package:

    Mindscape.Raygun4Net.AspNetCore

    and then add the following to your appsettings.json:

    {
      "Serilog": {
        "Using": [
          "Serilog.Sinks.Raygun"
        ],
        "WriteTo": [
          {
            "Name": "Raygun",
            "Args": {
              "applicationKey": "YourRaygunApiKey",
              "restrictedToMinimumLevel": "Error"
            }
          }
        ]
      }
    }
    

    just replace YourRaygunApiKey and you're in business ;)

Please Sign in or register to post replies

Write your reply to:

Draft