RavenDB.StructuredLogger
7.1.0
dotnet add package RavenDB.StructuredLogger --version 7.1.0
NuGet\Install-Package RavenDB.StructuredLogger -Version 7.1.0
<PackageReference Include="RavenDB.StructuredLogger" Version="7.1.0" />
paket add RavenDB.StructuredLogger --version 7.1.0
#r "nuget: RavenDB.StructuredLogger, 7.1.0"
// Install RavenDB.StructuredLogger as a Cake Addin #addin nuget:?package=RavenDB.StructuredLogger&version=7.1.0 // Install RavenDB.StructuredLogger as a Cake Tool #tool nuget:?package=RavenDB.StructuredLogger&version=7.1.0
RavenDB.StructuredLog
An ASP.NET Core logger that utilizes RavenDB to store structured logs.
<a href="https://andrewlock.net/creating-an-extension-method-for-attaching-key-value-pairs-to-scope-state-using-asp-net-core/">Structured log</a> that uses RavenDB as the log store.
Old, ugly way of logging makes for thousands of opaque logs:
- "User foo@bar.com signed in at 5:13 Oct 7"
- "User no@regerts.com signed in at 2:25 Nov 8"
- "User bar@zanz.com signed in at 3:18 Nov 21"
- "User me@me2.com signed in at 12:0 Dec 15"
- [and on and on for 1000+ entries - oiy!]
But with structured and grouped logging, you get a fewer logs that group similar logs together and makes them searchable:
{
"MessageTemplate": "User {email} signed in at {date}",
"Level": "Information",
"OccurrenceCount": 1032,
"FirstOccurrence": "2017-09-27T17:29:46.6597966+00:00",
"LastOccurrence": "2017-09-27T17:39:50.5554997+00:00",
"Occurrences": [
{
"Message": "User foo@bar.com signed in at 5:13 Oct 7",
"Level": "Information",
"Created": "2017-09-27T17:39:48.4248681+00:00",
"Category": "Sample.Controllers.HomeController",
"EventId": null,
"TemplateValues": {
"{OriginalFormat}": "User {email} signed in at {date}",
"email": "foo@bar.com",
"date": "5:13 Oct 7"
}
},
{
"Message": "User no@regerts.com signed in at 2:25 Nov 8",
"Level": "Information",
"Created": "2017-09-27T17:39:48.4248681+00:00",
"Category": "Sample.Controllers.HomeController",
"EventId": null,
"TemplateValues": {
"{OriginalFormat}": "User {email} signed in at {date}",
"email": "no@regerts.com",
"date": "2:25 Nov 8"
}
}
]
}
The end result is humans can easily understand what errors are occurring in your software and how often. Moreover, unlike old school logging where logs are giant opaque strings, structured logs are searchable as their template values are extracted and stored outside the log message.
Instructions
- In Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
// Create your Raven doc store
var docStore = new DocumentStore { ... };
// Recommended:
// Avoid logging objects that contain self referenced loops.
// Must be done before .Initialize().
docStore.IgnoreSelfReferencingLoops();
docStore.Initialize();
// Add RavenDB structured logging.
services.AddRavenStructuredLogger(docStore); // docStore may be omitted if it's already in the DI container
...
}
- Use logging as you normally would inside your controllers and services:
public class HomeController : Controller
{
private ILogger<HomeController> logger;
public HomeController(ILogger<HomeController> logger)
{
this.logger = logger;
}
public string Get()
{
// Simple logging
logger.LogInformation("Hi there!");
// Logging with templates
logger.LogInformation("The time on the server is {time}.", DateTime.UtcNow);
// Logging exceptions
logger.LogError(exception, "Woops, an error occurred");
// Logging exceptions with templates
logger.LogError(exception, "Woops, an error occurred executing {action} at {date}", this.ControllerContext.ActionDescriptor.ActionName, DateTime.UtcNow);
// Logging with scopes
using (logger.BeginScope(42))
{
logger.LogInformation("This message will have forty-two stored with it");
}
// Logging with multiple scopes and scope templates.
using (logger.BeginScope(42))
using (logger.BeginScope("The current user is {user}", User.Identity.Name))
using (logger.BeginKeyValueScope(nameof(totalCount), totalCount))
{
logger.LogInformation("This log will contain forty-two, the current signed in user name, and a key-value pair containing the name of the totalCount variable and its value.");
}
...
}
}
- You're done!
Need help? See the sample app.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. 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. |
-
net8.0
- Microsoft.AspNetCore.Hosting.Abstractions (>= 2.2.0)
- Microsoft.Extensions.Configuration.Abstractions (>= 8.0.0)
- Microsoft.Extensions.DependencyInjection (>= 8.0.0)
- Microsoft.Extensions.Logging (>= 8.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.0)
- RavenDB.Client (>= 6.0.2)
- System.Reactive (>= 6.0.0)
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 |
---|---|---|
7.1.0 | 189 | 3/26/2024 |
7.0.0 | 161 | 1/13/2024 |
6.0.1 | 897 | 9/2/2020 |
6.0.0 | 486 | 7/26/2020 |
5.1.0 | 515 | 5/15/2020 |
5.0.0 | 794 | 11/28/2019 |
4.2.1 | 684 | 9/11/2019 |
4.2.0 | 535 | 9/9/2019 |
4.1.0 | 548 | 9/4/2019 |
4.0.8 | 683 | 5/16/2019 |
4.0.7 | 1,060 | 8/15/2018 |
4.0.6 | 938 | 8/13/2018 |
4.0.5 | 986 | 7/10/2018 |
4.0.4 | 1,114 | 7/3/2018 |
4.0.3 | 1,209 | 3/13/2018 |
4.0.2 | 1,087 | 3/13/2018 |
4.0.1 | 1,153 | 2/12/2018 |
4.0.0 | 1,125 | 2/11/2018 |
2.0.2 | 1,134 | 1/11/2018 |
2.0.1 | 1,279 | 11/27/2017 |
2.0.0 | 1,184 | 10/17/2017 |
1.0.1 | 1,097 | 10/3/2017 |
1.0.0 | 1,118 | 9/27/2017 |
Initialization works with CreateBuilder() in modern ASP.NET apps