Rollbar logs not working in .Net(core) 5.0 - .net-5

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", "jbond#mi6.uk", "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.

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...

Related

How to use ActionFilter on Prometheus mapPath in standard .Net Web API?

I want to filter the range of client IPs who can route to Prometheus metrics.
So in startup I have
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
app.UsePrometheusServer(q =>
{
q.MapPath = "/metrics";
});
app.UseWebApi(config);
}
And this is my custom actionFilter class
public class IpFilter : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext actionContext)
{
string clinetIP = GetClientIpAddress(actionContext.HttpContext.Items["MS_HttpRequestMessage"] as HttpRequestMessage);
if (IpAllowed(clinetIP))
base.OnActionExecuting(actionContext);
}
But I have no idea how to use IpFilter since it cannot be use as an attribute on a controller action.
I tried to use it by adding a middleware using owin but the next.Invoke doesn't work properly
public void Configuration(IAppBuilder app)
{
app.Map("/metrics", metricsApp =>
{
metricsApp.Use<TestIpMid>(deniedIps);
metricsApp.UsePrometheusServer(q => q.MapPath = "/metrics");
});
app.UsePrometheusServer(q =>
{
q.MapPath = "/metrics";
});
app.UseWebApi(config);
}
and this is the middleware:
public class TestIpMid : OwinMiddleware
{
private readonly HashSet<string> _deniedIps;
public TestIpMid(OwinMiddleware next, HashSet<string> deniedIps) : base(next)
{
_deniedIps = deniedIps;
}
public override async Task Invoke(IOwinContext context)
{
var ipAddress = context.Request.RemoteIpAddress;
if (_deniedIps.Contains(ipAddress))
{
context.Response.StatusCode = 403;
return;
}
await Next.Invoke(context);
}
}
please help me :'(
this solution worked for me but other ways I was thinking of didn't work
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
var config = new HttpConfiguration();
var allowedIps = ProtectedSettings.Read(ProtectedSettings.protheusIpWhitelist).Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
app.Use(async (Context, next) =>
{
var ipAddress = Context.Request.RemoteIpAddress;
if ((!allowedIps.Contains(ipAddress)) && Context.Request.Path.Value == "/metrics")
{
Context.Response.StatusCode = 403;
return;
}
await next.Invoke();
});
app.UsePrometheusServer(q =>
{
q.MapPath = "/metrics";
});
app.UseWebApi(config);
}

Dependency is not getting resolved in WebAPI (including OWIN) with Autofac

My global.asax looks like below
private void BuildIocContainer()
{
var builder = new ContainerBuilder();
builder.RegisterModule(new BootstrapModule());
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container); //Set the WebApi DependencyResolver
}
protected void Application_Start()
{
BuildIocContainer();
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
I have built a bootstrap module for autofac like the one below
public class BootstrapModule : Autofac.Module
{
private Assembly AssemblyOf<T>()
{
return typeof(T).Assembly;
}
private Assembly WebAssembly
{
get { return Assembly.GetExecutingAssembly(); }
}
private void RegisterMvc(ContainerBuilder builder)
{
builder.RegisterType<AsyncControllerActionInvoker>()
.As<IActionInvoker>();
builder.RegisterControllers(WebAssembly)
.InjectActionInvoker();
builder.RegisterApiControllers(WebAssembly).InjectActionInvoker();
}
private void RegisterLogger(ContainerBuilder builder)
{
builder.Register(CreateLogger)
.SingleInstance();
builder.Register(_ => new NLogWrapper(LogManager.GetLogger("DefaultLogger")))
.As<Logging.ILogger>()
.SingleInstance();
}
private static System.Func<Type, Logging.ILogger> CreateLogger(IComponentContext componentContext)
{
return type => new NLogWrapper(LogManager.GetLogger(type.FullName));
}
protected override void Load(ContainerBuilder builder)
{
RegisterLogger(builder);
RegisterMvc(builder);
builder.RegisterAssemblyTypes(WebAssembly)
.AsImplementedInterfaces();
builder.RegisterType<UserService>()
.As<IUserService>()
.InstancePerRequest();
builder.RegisterAssemblyTypes(typeof(IUserService).Assembly)
.AsImplementedInterfaces();
builder.RegisterAssemblyTypes(typeof(IUserRepository).Assembly)
.AsImplementedInterfaces();
builder.RegisterFilterProvider();
}
}
Now, when I try to hit account controller through postman client,
private IUserService _userService;
public AccountController(IUserService userService)
{
_userService = userService;
}
// POST api/Account/Register
[AllowAnonymous]
[Route("Register")]
public HttpStatusCode Register(User model)
{
if (!ModelState.IsValid)
{
return HttpStatusCode.BadRequest;
}
// TODO : Write mapper if needed
var user = new ToolUser()
{
FirstName = model.FirstName,
LastName = model.LastName,
EmailID = model.EmailID,
DOB = Convert.ToDateTime(model.DateOfBirth),
Gender = model.Gender.ToString(),
TenandID = model.TenantID,
Password = model.Password
};
HttpStatusCode result = _userService.RegisterUser(user);
return result;
}
I get the error
"An error occurred when trying to create a controller of type 'AccountController'. Make sure that the controller has a parameterless public constructor."
What am I doing wrong?
You should have something like
HttpConfiguration config = new HttpConfiguration();
somewhere, which you use to register your routes etc.
Pass that config to your BuildIocContainer() mehod and add the line
config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
instead of
GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container); //Set the WebApi DependencyResolver
Hope that helps

Why is my Controller being passed a UserManager which is Disposed?

When i set a breakpoint in the constructor of this controller, I'm seeing a System.ObjectDisposedException from userManager.Users . Why might .NET be giving me a disposed UserManager? I have other controllers that seem to be set up the same way and their UserManagers work fine (this one dies when it calls GetUserAsync)
[RequireHttps]
public class HomeController : Controller
{
private readonly UserManager<ApplicationUser> _userManager;
private readonly RegionService _regionService;
private readonly ILogger _logger;
public HomeController(UserManager<ApplicationUser> userManager, RegionService regionService, ILoggerFactory loggerFactory)
{
_userManager = userManager;
_regionService = regionService;
_logger = loggerFactory.CreateLogger<HomeController>();
}
[HttpGet]
public async Task<IActionResult> Index()
{
var user = await _userManager.GetUserAsync(HttpContext.User);
...
}
...and i shouldn't be accessing a database in Home/Index; I'll change that afterward
Edit: This is how services are being configured:
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddApplicationInsightsTelemetry(Configuration);
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<ApplicationUser, IdentityRole>(options =>
{
options.Password.RequireDigit = true;
options.Password.RequireLowercase = false;
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireUppercase = false;
options.Password.RequiredLength = 6;
})
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddMvc();
// Add application services.
services.AddTransient<IEmailSender, AuthMessageSender>();
services.AddTransient<ISmsSender, AuthMessageSender>();
services.Configure<AuthMessageSenderOptions>(Configuration);
services.AddTransient<RegionService>();
// require SSL
services.Configure<MvcOptions>(options =>
{
options.Filters.Add(new RequireHttpsAttribute());
});
}
The issue was in the constructor of a class I created called RegionService
public RegionService(IServiceProvider serviceProvider)
{
using (var context = serviceProvider.GetService<ApplicationDbContext>())
{
...
}
}
The 'using' statement causes 'context' to be disposed after it's done with that block of code. I should have just been making a new instance of the DbContext that can be disposed:
public RegionService(IServiceProvider serviceProvider)
{
using (var context = new ApplicationDbContext(
serviceProvider.GetRequiredService<DbContextOptions<ApplicationDbContext>>()))
{
...
}
}

How to properly set up WebApi, EasyNetQ and Ninject?

This is what I have thus far. I'm stuck on the concept/where to implement the IBus injection/creation point (the publisher). I was trying to keep the publisher functionality all within the project and not create a separate service for it.
bus = RabbitHutch.CreateBus("host=localhost", x => x.Register<IEasyNetQLogger>(_ => logger));
This is my first pass at this so I'm open to suggestions and best practice advice :-)
Things left to do:
create a rabbitmq config file with proper settings defining queue name and ?
create a message handler that takes care of connection management that is?
create the publisher at app start up, dispose properly when ?
EasyNetQ Wrapper to replace EasyNetQ internal IoC, Ninject replacement:
public class NinjectContainerWrapper: IContainer, IDisposable
{
public NinjectContainerWrapper()
{
}
//ninject container/kernal? here
//private readonly ISomeNinjectInterface container;
public TService Resolve<TService>() where TService : class
{
throw new NotImplementedException();
}
public IServiceRegister Register<TService, TImplementation>()
where TService : class
where TImplementation : class, TService
{
throw new NotImplementedException();
}
public IServiceRegister Register<TService>(Func<EasyNetQ.IServiceProvider, TService> serviceCreator) where TService : class
{
throw new NotImplementedException();
}
public void Dispose()
{
throw new NotImplementedException();
}
}
NinjectWebCommon.cs
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<IAppSettings>().To<AppSettings>();
kernel.Bind<IDmpeService>().To<DmpeService>();
kernel.Bind<IPublisher>().To<DmpePublisher>();
kernel.Bind<IEasyNetQLogger>().To<GdmEasyNetQLogger>();
kernel.Bind<ILoggingService>().ToMethod(x =>
{
var scope = x.Request.ParentRequest.Service.FullName;
var log = (ILoggingService)LogManager.GetLogger(scope, typeof(LoggingService));
return log;
});
}
the publisher class:
public class DmpePublisher: IPublisher
{
private readonly IEasyNetQLogger _logger;
private readonly IAppSettings _appSettings;
private readonly IBus bus = null;
public DmpePublisher(IEasyNetQLogger logger, IAppSettings appSettings)
{
this._logger = logger;
this._appSettings = appSettings;
// register our alternative container factory
RabbitHutch.SetContainerFactory(() =>
{
var ninjectContainer = new NinjectContainerWrapper();
// wrap it in our implementation of EasyNetQ.IContainer
//return new NinjectContainerWrapper(ninjectContainer);
});
bus = RabbitHutch.CreateBus("host=localhost", x => x.Register<IEasyNetQLogger>(_ => logger));
}
public void PublishMessage(Messages.IMessage message)
{
throw new NotImplementedException();
//log post
//_logger.InfoWrite("Publishing message: {0}", message);
}
}

Simple Injector: Implementation that depends on http request

I'm a beginner with Simple Injector and have a scenario where I need help to implement. I will try to simplify what I need to do.
I have a WebAPI where I need to authenticate users and based on the type of user choose an implementation.
Consider this structure
public interface ICarRepository {
void SaveCar(Car car);
}
//Some implementation for ICarRepository
public interface ICarLogic {
void CreateCar(Car car);
}
public class CarLogicStandard: ICarLogic {
private ICarRepository _carRepository;
public CarLogicStandard(ICarRepository carRepository) {
_carRepository = carRepository;
}
public void CreateCar(Car car) {
car.Color = "Blue";
_carRepository.SaveCar();
//Other stuff...
}
}
public class CarLogicPremium: ICarLogic {
private ICarRepository _carRepository;
public CarLogicPremium(ICarRepository carRepository) {
_carRepository = carRepository;
}
public void CreateCar(Car car) {
car.Color = "Red";
_carRepository.SaveCar();
//Other stuff 2...
}
}
And now I have a controller
public class CarController: ApiController {
private ICarLogic _carLogic;
public CarController(ICarLogic carLogic) {
_carLogic = carLogic;
}
public void Post(somePostData) {
//Identify the user based on post data
//....
Car car = somePostData.SomeCar();
_carLogic.CreateCar(car);
}
}
The code above will not work because in my request I need to identify the user. If it is a premium user the controller should use the CarLogicPremium and if it is a standard user the controller should use the CarLogicStandard.
I can configure the repository and others interfaces that don't need this logic on Global.asax however, since this case I need the request to decide which implementation should be used, I supose that I need to solve this in some other way.
There is a "Simple Injector" way to handle this? Or should I try another approach?
The simplest solution would be to configure the decision in the composition root, along with the rest of the container's configuration:
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
var container = new Container();
container.Register<CarLogicStandard>();
container.Register<CarLogicPremium>();
container.RegisterPerWebRequest<ICarRepository, CarRepository>();
container.Register<ICarLogic>(
() =>
HttpContext.Current != null &&
HttpContext.Current.User != null &&
HttpContext.Current.User.IsInRole("Premium")
? (ICarLogic)container.GetInstance<CarLogicPremium>()
: (ICarLogic)container.GetInstance<CarLogicStandard>()
);
// This is an extension method from the integration package.
container.RegisterWebApiControllers(GlobalConfiguration.Configuration);
container.Verify();
GlobalConfiguration.Configuration.DependencyResolver =
new SimpleInjectorWebApiDependencyResolver(container);
}
You could also create an abstraction over the current user and decorate standard features with premium features
public class CarLogicPremium : ICarLogic
{
private readonly ICarLogic decorated;
private readonly ICurrentUser currentUser;
private readonly ICarRepository carRepository;
public CarLogicPremium(
ICarLogic decorated,
ICurrentUser currentUser,
ICarRepository carRepository)
{
this.decorated = decorated;
this.currentUser = currentUser;
this.carRepository = carRepository;
}
public void CreateCar(Car car)
{
if (currentUser.IsPremiumMember)
{
car.Color = "Red";
this.carRepository.SaveCar(car);
//Other stuff 2...
}
else
{
this.decorated.CreateCar(car);
}
}
}
which would be configured a bit like this
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
var container = new Container();
container.Register<ICurrentUser, HttpCurrentUserProxy>();
container.RegisterPerWebRequest<ICarRepository, CarRepository>();
container.Register<ICarLogic, CarLogicStandard>();
container.RegisterDecorator(typeof(ICarLogic), typeof(CarLogicPremium));
container.RegisterWebApiControllers(GlobalConfiguration.Configuration);
container.Verify();
GlobalConfiguration.Configuration.DependencyResolver =
new SimpleInjectorWebApiDependencyResolver(container);
}
But it really depends how many variations of services you will be creating over time. If you will be constantly adding new premium features you should look to implement a variation of the Try-X pattern. Let me know if one of the above works for you or if you need more info ...

Resources