LogDNA provider for Microsoft.Extensions.Logging written in .NET Standard 2.0.
If you're looking to integrate with ASP.NET Core, take a look at the README for
RedBear.LogDNA.Extensions.Logging.Webinstead.
Install-Package RedBear.LogDNA.Extensions.Logging
To add the LogDNA provider in .NET Core 2.0+:
public void ConfigureServices(IServiceCollection services)
{
services.AddLogging(loggingBuilder =>
loggingBuilder.AddLogDNA("ingestion_key"));
// Other services ...
}The following additional overloads exist on AddLogDNA():
logLevel- to set the default log level (default isWarning);
loggerfactory.AddLogDNA("ingestion_key", LogLevel.Debug);options- to pass in additional configuration options (see next section).
var options = new LogDNAOptions("ingestion_key");
loggerFactory.AddLogDNA(options);The AddLogDNA() method has an override that takes an instance of a LogDNAOptions class:
var options = new LogDNAOptions("ingestion_key");
options.LogLevel = LogLevel.Warning;
options.HostName = "MyHost";
options.Tags = new [] { "one", "two" };
options.MessageDetailFactory.RegisterHandler((detail) =>
{
detail.AddOrUpdateProperty("Foo", "Bar");
});
loggerFactory.AddLogDNA(options);The LogDNAOptions class has the following properties:
LogLevel- to set the default log level (default isWarning);HostName- used to override the machine's hostname. Defaults toEnvironment.MachineName;Tags- to be associated with the host. Defaults tonull;MessageDetailFactory- see next section.
Additionally, different log levels can be set for different namespaces using the .AddNamespace(namespace, level) method:
// Would apply to all log names starting with "MyApp." - e.g. MyApp.Services, MyApp.Models, etc
options.AddNamespace("MyApp.", LogLevel.Debug);It is recommended to set the default log level (options.LogLevel) to Warning and then set a lower log level for your own code using AddNamespace().
The MessageDetail class is serialised to create a JSON message for LogDNA to ingest:
{
message : "This is my messsage",
level : "WARN",
Value : {
Foo : "foo",
Bar : "bar"
}
}Each new instance of a MessageDetail class is produced by an implementation of IMessageDetailFactory.
The properties of the MessageDetail JSON can be further customised by registering an Action<MessageDetail> event handler that will fire whenever a new MessageDetail class is created:
var options = new LogDNAOptions("ingestion_key");
options.MessageDetailFactory.RegisterHandler((detail) =>
{
detail.AddOrUpdateProperty("Foo", "Bar");
});
loggerFactory.AddLogDNA(options);For example, the ASP.NET Core implementation of this log provider uses this approach to set the following properties using the IHttpContextAccessor:
- Trace Identifier;
- IP Address;
- User Agent;
- etc.
The Microsoft logging framework will occasionally try and use its own form of serialisation, typically when dealing with IEnumerable<> objects.
To ensure that the original object is serialised into JSON and that a value appears as follows in LogDNA, use the Wrapper class.
var array = new string[] { "one", "two", "three" };
logger.LogInformation("An array", new Wrapper(array));The logger will unwrap the object prior to serialisation.
Please remember that indexing of log entries only happens on paid accounts. This means you won't see JSON representations of objects or coloured highlighting of INFO, WARN, etc, if you are using a free account.
Also, please remember LogDNA's standard service limits for ingestion.
