Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/JudahGabriel/RavenDB.StructuredLog
An ASP.NET Core logger that utilizes RavenDB to store structured logs.
https://github.com/JudahGabriel/RavenDB.StructuredLog
aspnetcore logging ravendb structured-logging
Last synced: 7 days ago
JSON representation
An ASP.NET Core logger that utilizes RavenDB to store structured logs.
- Host: GitHub
- URL: https://github.com/JudahGabriel/RavenDB.StructuredLog
- Owner: JudahGabriel
- License: mit
- Created: 2017-09-26T18:35:47.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2024-03-26T07:07:22.000Z (8 months ago)
- Last Synced: 2024-11-01T18:39:03.409Z (12 days ago)
- Topics: aspnetcore, logging, ravendb, structured-logging
- Language: C#
- Homepage: https://www.nuget.org/packages/RavenDB.StructuredLogger
- Size: 1.17 MB
- Stars: 12
- Watchers: 3
- Forks: 6
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# RavenDB.StructuredLog
An ASP.NET Core logger that utilizes RavenDB to store structured logs.Structured log that uses RavenDB as the log store.
Old, ugly way of logging makes for thousands of opaque logs:
- "User [email protected] signed in at 5:13 Oct 7"
- "User [email protected] signed in at 2:25 Nov 8"
- "User [email protected] signed in at 3:18 Nov 21"
- "User [email protected] 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:
```json
{
"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 [email protected] 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": "[email protected]",
"date": "5:13 Oct 7"
}
},
{
"Message": "User [email protected] 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": "[email protected]",
"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 ##
1. In Startup.cs:```csharp
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...
}
```2. Use logging as you normally would inside your controllers and services:
```csharp
public class HomeController : Controller
{
private ILogger logger;public HomeController(ILogger 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.");
}
...
}
}
```3. You're done!
Need help? See the [sample app](https://github.com/JudahGabriel/RavenDB.StructuredLog/tree/master/Sample).