Giter VIP home page Giter VIP logo

ravendb.structuredlog's Introduction

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:

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 [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:
public void ConfigureServices(IServiceCollection services)
{
	// Add RavenDB structured logging.
	services.AddLogging(builder => builder.AddRavenStructuredLogger(docStore)); // Where docStore is your RavenDB DocumentStore singleton.

	...
}
  1. 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.");
		}
		
		...
	}
}
  1. You're done!

Need help? See the sample app.

ravendb.structuredlog's People

Contributors

jtone123 avatar judahgabriel avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.