This .NET Core 6 library contains the logging functionality used throughout Web349's internal and external projects. Contains a powerful Datadog logger.
| Required | Name | Type | Default | Notes |
|---|---|---|---|---|
WEB349_LOGGING_LOGLEVEL |
string |
Verbose |
The log level. Must be any of the following values: Silent, Fatal, Error, Warning, Information, Debug, Verbose |
|
WEB349_LOGGING_CENSORSHIP_ENABLED |
bool |
true |
Toggles censorship of potentially sensitive information in the enriched fields of log events. The function looks for keywords configured in the WEB349_LOGGING_CENSORSHIP_KEYWORDS env var. |
|
WEB349_LOGGING_CENSORSHIP_KEYWORDS |
string |
api;key;secret;credential;auth;cookie;login |
A semi-colon (;) delimited list of keywords to look for in the enriched fields of log events. Setting this ENV var to a custom value will replace the default values, unless explicitely specified. | |
| ✅ | WEB349_LOGGING_DATADOG_APIKEY |
string |
Your Datadog API key | |
| ✅ | WEB349_LOGGING_DATADOG_SITE |
string |
The Datadog site to send logs to. Must be any of the following values: US1, US3, US5, EU, AP1, US1_GOV |
|
WEB349_LOGGING_DATADOG_SOURCE |
string |
Fills the Source field in Datadog | ||
WEB349_LOGGING_DATADOG_SERVICE |
string |
Fills the Service field in Datadog | ||
WEB349_LOGGING_DATADOG_HOST |
string |
Fills the Host field in Datadog | ||
WEB349_LOGGING_DATADOG_COMPRESSLOGS |
bool |
true |
Set to true to enable gzip compression |
|
WEB349_LOGGING_DATADOG_BATCH_SIZE |
int |
10 |
The maximum size of a single log event batch. | |
WEB349_LOGGING_DATADOG_BATCH_AGE |
int |
5 |
The maximum age, in seconds, of a single log event batch that has not reached its maximum batch size | |
WEB349_LOGGING_DATADOG_HTTPCLIENT_TIMEOUT |
int |
10 |
Underlying HttpClient timeout in seconds |
|
WEB349_LOGGING_SLACK_HTTPCLIENT_TIMEOUT |
int |
10 |
Underlying HttpClient timeout in seconds for calling the registered Webhook URL |
|
WEB349_LOGGING_SLACK_WEBHOOKURL_<NAME> |
string |
A SlackLogger needs a name argument in its constructor that should match up with this ENV var |
||
WEB349_LOGGING_DISPATCHER_DELAY_IDLE |
int |
1000 |
The delay, in miliseconds, for the log batch dispatcher to wait in between polls while idling. | |
WEB349_LOGGING_DISPATCHER_DELAY_ACTIVE |
int |
100 |
The delay, in miliseconds, for the log batch dispatcher to wait in between polls while processing log events. |
- .NET Core 6 Download at dot.net
System.Text.Jsonfor JSON serialization
- Each instance of a
Loggerclass creates its own contextlogger.Contextfor identification purposes. If not specified duringLoggerimplementation creation, theContextis a newGUID. - While technically possible, multiple threads should not share the same
Loggerinstance. - Each
Loggercontext keeps track of anint64Event ID field allowing for easier identification and log tracing. - At the moment of writing
SlackLoggeronly supports sending of unenriched, text-only messages. Use Markdown to apply formatting in Slack. - The
SlackLoggername has the follow regular expression limitation:^[a-zA-Z0-9_]+$. - The Dispatcher settings only apply to
Loggerimplementations that use a producer/consumer dispatching service to send off the logs (e.g.,SlackLoggerandDatadogLogger).
Logger logger = new DatadogLogger();
logger.LogInformation("This is the message that will appear in Datadog");
Logger logger = new DatadogLogger();
logger
.Enrich("fieldName", 123)
.Enrich("anotherFieldName", "some string value")
.Enrich("foo", serializableBarObject)
.LogInformation("This is the message that will appear in Datadog, with the three fields enriched above");
Logger logger = new DatadogLogger();
try
{
throw new Exception("oops");
}
catch(Exception ex)
{
logger.LogError(ex.Message, ex);
}
// the Slack webhook URL will match to the value set to WEB349_LOGGING_SLACK_WEBHOOKURL_SERVICENAME
// dashes (-) will be replace with an underscore (_) and spaces are removed.
Logger serviceNameLogger = new SlackLogger("serviceName");
serviceNameLogger.LogInformation("a message");
The library has the ability to censor potentially sensitive information from enriched fields. See WEB349_LOGGING_CENSORSHIP_KEYWORDS for more information. The following is an example of information censorship:
Logger logger = new DatadogLogger();
logger
.Enrich("myFieldNotCensored", obj)
.Enrich("apiKeyBeingCensored", apiKeyStr) // this field contains 'api' and its value will be censored to '***'
.LogDebug("Hello friend, this is a DEBUG log event with one censored and one uncensored field");
Want to help? Comments? Love? Hate? Open up an Issueo or Pull Request on Github: https://github.com/web349/logging
This software is provided for free, AS IS, without any guarantees whatsoever, because I felt there were no easy-to-use Datadog loggers for .NET Core.