Web349.Logging 0.2.0

dotnet add package Web349.Logging --version 0.2.0
NuGet\Install-Package Web349.Logging -Version 0.2.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Web349.Logging" Version="0.2.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Web349.Logging --version 0.2.0
#r "nuget: Web349.Logging, 0.2.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Web349.Logging as a Cake Addin
#addin nuget:?package=Web349.Logging&version=0.2.0

// Install Web349.Logging as a Cake Tool
#tool nuget:?package=Web349.Logging&version=0.2.0

Nuget (with prereleases)

Web349 Logging Library

This .NET Core 6 library contains the logging functionality used throughout Web349's internal and external projects. Contains a powerful Datadog logger.

ENV variable configuration

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.

Requirements

Notes

  • Each instance of a Logger class creates its own context logger.Context for identification purposes. If not specified during Logger implementation creation, the Context is a new GUID.
  • While technically possible, multiple threads should not share the same Logger instance.
  • Each Logger context keeps track of an int64 Event ID field allowing for easier identification and log tracing.
  • At the moment of writing SlackLogger only supports sending of unenriched, text-only messages. Use Markdown to apply formatting in Slack.
  • The SlackLogger name has the follow regular expression limitation: ^[a-zA-Z0-9_]+$.
  • The Dispatcher settings only apply to Logger implementations that use a producer/consumer dispatching service to send off the logs (e.g., SlackLogger and DatadogLogger).

Datadog logger usage examples

Logging regular information

Logger logger = new DatadogLogger();
logger.LogInformation("This is the message that will appear in Datadog");

Logging enriched information

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");

Logging an Exception

Logger logger = new DatadogLogger();
try
{
	throw new Exception("oops");
}
catch(Exception ex)
{
	logger.LogError(ex.Message, ex);
}

Slack logger usage examples

Logging regular information

// 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");

Information censorship usage example

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");

Contribution

Want to help? Comments? Love? Hate? Open up an Issueo or Pull Request on Github: https://github.com/web349/logging

Disclaimer

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.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.2.0 145 6/19/2023
0.1.2 129 6/18/2023
0.1.1 162 6/18/2023