0

我有.Net core 5.0 api,我正在尝试添加日志,但不知何故它不起作用。下面是我的设置。

    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", "jbond@mi6.uk", "JBOND");
    }

这是我的控制器。

公共类 TestController : ControllerBase { 私有只读 IRepositoryWrapper _repositories; 私有只读 Rollbar.ILogger _logger;私有只读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 };
    }
}

它给了我“无法解析 Rollbar.ILogger 类型的服务”的错误。我什至没有在任何文档中看到处理这种情况的方法。任何帮助将非常感激。

4

1 回答 1

0

确保您使用的是 Rollbar.NetCore.AspNet Nuget 包 v5.0.0-beta 和更新版本。例如:https ://www.nuget.org/packages/Rollbar.NetCore.AspNet/5.0.4-beta

并按照以下说明操作: https ://docs.rollbar.com/docs/v5-integrating-with-aspnet-core-2-and-newer

如果这没有帮助,请在以下位置打开一个问题: https ://github.com/rollbar/Rollbar.NET/issues 我会在那里跟进...

于 2021-10-21T20:41:54.200 回答