Integrating with ASP.NET Core 2 and Newer
Overview
The SDK can be integrated into an ASP.NET Core 2 application on two levels:
- Each ASP.NET Core controllers’ method implementation could be surrounded by a try-catch block where, within the
catch(…){…}
block, any caught exception is passed to a common exception handling routine which in turn reports the exception via the SDK. - A request processing pipeline of the application is extended with the Rollbar.NET middleware component that monitors all the inner middleware components of the pipeline for unhandled exceptions and reports them via the Rollbar.NET Notifier singleton instance and then rethrows the exceptions while wrapping them with a new Exception object.
You can check out a sample ASP.NET Core 2 based application that demonstrates proper use of the middleware component here.
First, the singleton component needs to be properly configured. There are two approaches to doing this. If both approaches are used, the second option always overrides the first option.
Option 1 - Configure the Rollbar Logger via the app settings
Add at least the minimum required configuration parameters to the hosting application’s appsettings.json
file. Any of the properties of the RollbarConfig class that has public setter can be set using this approach.
{
"Logging": {
"IncludeScopes": false,
"Debug": {
"LogLevel": {
"Default": "Warning"
}
},
"Console": {
"LogLevel": {
"Default": "Warning"
}
}
},
"Rollbar": {
"AccessToken": "POST_SERVER_ITEM_ACCESS_TOKEN",
"Environment": "AspNetCoreMiddlewareTest"
}
}
Option 2 - Configure the Rollbar Logger in code
Within the ConfigureServices(…)
method of Startup.cs
, add proper Rollbar configuration and register the Rollbar logger with the application services:
// This method gets called by the runtime.
// Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
ConfigureRollbarSingleton();
//...
}
/// <summary>
/// Configures the Rollbar singleton-like notifier.
/// </summary>
private void ConfigureRollbarSingleton()
{
const string rollbarAccessToken = "POST_SERVER_ITEM_ACCESS_TOKEN";
const string rollbarEnvironment = "RollbarNetSamples";
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
;
}
Activate the Rollbar
Next, add Rollbar logging service:
// This method gets called by the runtime.
// Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
//...
services.AddRollbarLogger(loggerOptions =>
{
loggerOptions.Filter =
(loggerName, loglevel) => loglevel >= LogLevel.Trace;
});
services.AddMvc();
}
Now, add the Rollbar middleware to the application pipeline. This is normally done by adding its usage within the Configure(…)
method of Startup.cs
.
// This method gets called by the runtime.
// Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRollbarMiddleware();
// Any other middleware component intended to be "monitored"
// by Rollbar.NET middleware go below this line:
app.UseMvc();
}
That's it! Now every unhandled exception within the middleware pipeline under the Rollbar middleware component monitoring will be reported to Rollbar.
Updated 12 months ago