Skip to content

Latest commit

 

History

History
118 lines (80 loc) · 4.58 KB

README.md

File metadata and controls

118 lines (80 loc) · 4.58 KB

Rollbar Logging

A Rollbar implementation of Microsoft.Extensions.Logging.ILogger to be used in .net core applications

How to Use

Install via Nuget

dotnet add Archon.Rollbar

ASP.NET Core

Configure it like you would any other logger. In your Program.cs:

var webHost = new WebHostBuilder()
	.ConfigureLogging(logging => logging.AddRollbar(
		"my_rollbar_access_token",
		"rollbar_environment",
		new Server { CodeVersion = "v1.0.0" } // this param is optional
		LogLevel.Warning // optional log level threshold: only logs at this level or higher will be sent to rollbar (e.g. Warning, Error, Critical)
	));

And then in your Startup.Configure, setup the rollbar request logger middleware as the first middleware registration:

app.UseRollbarRequestLogger();

Note: The UseRollbarRequestLogger is not required. You only need to set that up if you want to send POST bodies to rollbar. The UseRollbarRequestLogger extension method will setup a middleware that will buffer the request body stream so that the logger can access/read it & send it to rollbar.

You can then use ILogger or ILogger<T> like you normally would to create some logs that will show up in your rollbar account.

.NET Core Console App

If you want to use the logging infrastructure outside of ASP.Net Core, you will need to instantiate the IServiceCollection yourself:

var serviceProvider = new ServiceCollection()
	.AddLogging(logging => logging.AddRollbar("my_rollbar_access_token", "rollbar_environment"))
	.BuildServiceProvider();

ILogger<Whatever> logger = lg.CreateLogger<Whatever>();
logger.LogDebug("All done!");

If you don't want to use the built-in Microsoft.Extensions.DependencyInjection, you will have to create the LoggerFactory yourself:

ILoggerFactory lg = new LoggerFactory();
lg.AddRollbar("my_rollbar_access_token", "rollbar_environment");

ILogger<Whatever> logger = lg.CreateLogger<Whatever>();
logger.LogInformation("Woo hoo!");

In this case, if you want to specify a threshold for the rollbar logger, you will have to add the filter manually:

var lg = new LoggerFactory(
	new ILoggerProvider[0],
	new LoggerFilterOptions().AddFilter<RollbarLoggerProvider>(null, threshold)
);

lg.AddRollbar("my_rollbar_access_token", "rollbar_environment");

This is all the ILoggingBuilder extension method is doing internally.

Event IDs

The rollbar logger will honor any log event IDs you use when you create your logs. It will pass the event ID as the rollbar log fingerprint. This means any logs logged with the same event ID will be grouped together in rollbar as a single item. This makes it easy to filter noise out of your logs.

If you don't pass an event ID to your log, Rollbar will use its built-in fingerprinting algorithm.

Structured Logging

The Microsoft.Extensions.Logging infrastructure allows for structured logs. This means, if you create a log like this:

string thing = "poop";
string stuff = "toilets";
logger.LogInformation("Doing a {thing} with {stuff}", thing, stuff);

This will send the correct log message to Rollbar while also including the structured data so that you can query it using Rollbar's RQL:

{
	"message": "Doing a poop with toilets",
	"thing": "poop",
	"stuff": "toilets"
}

User Data

The RollbarLogger makes use of ClaimsPrincipal to retrieve username, email, and a user ID to send to rollbar. As long as the current user is authenticated and provides a ClaimsPrincipal, that user information will be sent to Rollbar.

Why Not Use the Official Integration?

Most of the implementation here is copy pasta'd from the official repo with a few notable differences:

  1. Use HttpClient instead of WebRequest because it is not 2004 anymore.
  2. No static Rollbar class. Use dependency injection with ILoggers. Again, not 2004 anymore.
  3. The official integration isn't compiled to .net standard (I did try to help 😒).

How to Build

To build, clone this repo and run:

dotnet restore
dotnet build