NorthWest Nodes common library
The NorthWest Nodes Common Library is a class library written in C# and requires .NET Core 6.0. It contains several common functions used throughout the closed- and open-source projects at NorthWest Nodes (https://northwestnodes.com).
One of its main features is a highly flexible Datadog logger.
Installation
Install-Package NorthWestNodes.Common
dotnet add package NorthWestNodes.Common
Datadog logger
Configuration
Namespaces:
NorthWestNodes.Common.Logging
NorthWestNodes.Common.Logging.Datadog
Environment variables:
- REQUIRED
DD_API_KEY
: the API key for your Datadog app. - REQUIRED
DD_SITE
: determines the intake URL. Valid options are:US1
,US3
,US5
,US1_FED
,EU
. - OPTIONAL
DD_HOST
: theHost
column in Datadog. - OPTIONAL
DD_SERVICE
: theService
column in Datadog. - OPTIONAL
DD_SOURCE
: theSource
column in Datadog. - OPTIONAL
DD_LOGLEVEL
: the log level. Valid options are (from most verbose to none):verbose
(default),debug
,info
,warn
,error
,fatal
,none
Usage examples
Use the default DDLog
singleton instance (note: this is not thread-safe!) to send a log message:
DDLog.Default.WriteInfo("Hello world, this is an INFO message");
Note: the single is not thread-safe and can cause issues when enriching your log messages from concurrent consumers. When using concurrent consumers, create a separate DDLog
instance per consumer.
Send a basic Datadog log event:
Log log = DDLog.Create();
log.WriteInfo("An INFO log event message appearing in Datadog's message column");
Send a Datadog log event with the source
set:
Log log = DDLog.Create("MySourceClassInMyProgram");
log.WriteInfo("An INFO log event specifying the class in which it was instantiated. How handy!");
Dynamic enrichtments
Send a Datadog log event with a dynamic enrichment. Dynamic enrichments get removed/reset after each Write operation:
Stopwatch sw = Stopwatch.StartNew();
// do a thing
sw.Stop();
Log log = DDLog.Create();
log
.EnrichDynamic("responseTime", sw.ElapsedMilliseconds)
.WriteInfo("An INFO log event message appearing in Datadog's message column");
Static enrichments
Send a Datadog log event with a static enrichment. Static enrichments do not get removed after each Write operation. Can be used in combination with Dynamic enrichtments:
Log log = DDLog.Create();
log.EnrichStatic("network", "source-network");
log
.EnrichDynamic("someField", someVariable)
.WriteInfo("An INFO log event message appearing in Datadog's message column");
Log levels
You can set the log level programmatically:
Log log = DDLog.Create();
log.SetLogLevel(LogLevel.Error); // only errors and up
log.WriteInfo("Hello world"); // will NOT be processed
log.WriteFatal("Ack, mein leben!"); // will be processed
Log event dispatching
The Datadog logger uses a background Task to dispatch log events in batches. By default a batch contains a maximum of 10 log events and each a maximum age of 5 seconds before being forcibly dispatched regardless of how many items a batch contains. Currently these settings are harded-coded, but you're more than welcome to submit a PR.