Rollbar logs not working in .Net(core) 5.0

525 Views Asked by At

I have .NET Core 5.0 API and I'm trying to add logs but somehow its not working. below is my setup.

    public void ConfigureServices(IServiceCollection services)
    {
        ConfigureRollbarSingleton();

        services.AddControllers();

        services.AddDbContext<AfterSchoolContext>(options => options.UseSqlServer(connectionString: connectionString, m => m.MigrationsAssembly("AfterSchoolHQ")));

        services.AddScoped<IRepositoryWrapper, RepositoryWrapper>();

        // Automapper Configuration
        services.AddAutoMapper(typeof(Startup));

        services.AddHttpContextAccessor();

        services.AddRollbarLogger(loggerOptions =>
        {
            loggerOptions.Filter =
              (loggerName, loglevel) => loglevel >= LogLevel.Trace;
        });


        services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new OpenApiInfo { Title = "AfterSchoolHQ", Version = "v1" });
        });
    }

    private void ConfigureRollbarSingleton()
    {
        RollbarLocator.RollbarInstance
          // minimally required Rollbar configuration:
          .Configure(new RollbarConfig(rollbarAccessToken) { Environment = rollbarEnvironment })
          // optional step if you would like to monitor 
          // Rollbar internal events within your application:
          .InternalEvent += OnRollbarInternalEvent
          ;

        // Optional info about reporting Rollbar user:
        SetRollbarReportingUser("007", "[email protected]", "JBOND");
    }

And here is my controller.

public class TestController : ControllerBase
{
    private readonly IRepositoryWrapper _repositories;
    private readonly Rollbar.ILogger _logger;
    private readonly IMapper _mapper;

    public TestController(Rollbar.ILogger logger, IMapper mapper, IRepositoryWrapper repositories)
    {
        _mapper = mapper;
        _logger = logger;
        _repositories = repositories;
    }

    [HttpGet("GetByID")]
    public IActionResult GetById(int id)
    {
        try
        {
            if (id <= 0)
                return new NotFoundResult();
                
            _logger.Info("test"); //I'm trying to add a log from here
            var request = _repositories.GetById(id: id);
            if (request == null)
                return new NotFoundResult();

            return new OkObjectResult(_mapper.Map(source: request));
        }
        catch (Exception ex)
        {
            return new ObjectResult(ex.Message) { StatusCode = 500 };
        }
    }

It gives me error that:

unable to resolve service for type Rollbar.ILogger

I don't even see a way in any docs to handle this cases. any help would be really appreciated.

1

There are 1 best solutions below

0
AKornich On

Make sure you are using Rollbar.NetCore.AspNet Nuget package v5.0.0-beta and newer. For example: https://www.nuget.org/packages/Rollbar.NetCore.AspNet/5.0.4-beta

and follow this instructions: https://docs.rollbar.com/docs/v5-integrating-with-aspnet-core-2-and-newer

If that does not help, please, open an issue at: https://github.com/rollbar/Rollbar.NET/issues I'll follow up there...