Vostok.Logging
HomeQuickstartModulesImplementations
1.0.0
1.0.0
  • Home
  • Quickstart
  • Guarantees
  • Configuration
  • Concepts and basics
    • Log interface
    • Log events
    • Syntax
      • Logging extensions
      • Message templates
      • Providing property values
    • Formatting
      • Output templates
      • Special properties
      • Format specifiers
    • Source context
    • Operation context
  • Modules
    • Abstractions
    • Configuration
    • Formatting
    • Console
    • File
    • Hercules
    • Context
    • Serilog
    • Log4net
    • NUnit
    • Microsoft
  • Implementations
    • Silent log
    • Console log
    • File log
    • Hercules log
  • Integrations
    • Serilog integration
    • Log4net integration
    • Microsoft logging integration
  • How-to guides
    • Using operation context
    • Using static log provider
    • Filtering events
    • Enriching events
    • Transforming events
    • Combining multiple logs
    • Custom output templates
    • External configuration rules
Powered by GitBook
On this page
  • Structure
  • Implementation details
  • Rendering in text-based logs
  1. Concepts and basics

Source context

Source context is meant to denote the sources of logging events, such as classes calling the log methods. This context also encodes the hierarchy of log's ownership. It is produced with ForContext method and bound to returned log instance. The dominant use case for source context is to obtain class-based logs:

class MyClass
{
    private readonly ILog log;

    public MyClass(ILog log)
    {
        this.log = log.ForContext<MyClass>();
    }
}

Structure

Source context exhibits hierarchical structure: chained ForContext calls produce logs with multiple contexts ordered with respect to calls sequence.

log = log
    .ForContext("foo")
    .ForContext("bar")
    .ForContext("baz"); 

// log's source context is now ["foo", "bar", "baz"]

Implementation details

Handling of source context is implementation-specific.

Rendering in text-based logs

Here's an example of how text-based logs render source context:

var log = new SynchronousConsoleLog() as ILog;

log = log.ForContext("foo");

log.Info("Message 1");

log = log.ForContext("bar");

log.Info("Message 2");

log = log.ForContext("baz");

log.Info("Message 3");

Sample output from this code:

2019-03-10 01:21:25,517 INFO  [foo] Message 1
2019-03-10 01:21:25,554 INFO  [foo => bar] Message 2
2019-03-10 01:21:25,555 INFO  [foo => bar => baz] Message 3
PreviousFormat specifiersNextOperation context

Last updated 6 years ago

However, all implement it by enriching incoming log events with a special well-known SourceContext property. Its value is represented by public type that implements IReadOnlyList<string> and has a pretty ToString. This behavior can be replicated in custom log implementations by returning a from ForContext method.

does essentially the same.

maps source contexts to logger names.

built-in logs
SourceContextValue
SourceContextWrapper
Serilog adapter
Log4net adapter