我有.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 类型的服务”的错误。我什至没有在任何文档中看到处理这种情况的方法。任何帮助将非常感激。