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
  • Log configuration rules
  • Create a ConfigurableLog
  1. How-to guides

External configuration rules

Prerequisites:

  • Install configuration module

  • Read about log events

  • Read about source context

  • Read about filtering

  • Read about enrichment

ConfigurableLog is essentially a CompositeLog with named components and rules for filtering and enrichment. Rules can be supplied from any external source and support reconfiguration without application restart.

Log configuration rules

Every rule contains some of the following:

Property

Type

Default value

Description

Enabled

bool

true

Enables/disables the logging in scope of the rule entirely.

Log

string

null

Limits the scope of the rule to the log with given name. If not specified, the rule applies to all logs.

Source

string

null

Operation

string

null

MinimumLevel

LogLevel?

null

Sets the minimum log level for the events in scope of the rule.

Properties

Dictionary

null

Adds given set of properties to every event in scope of the rule.

There are also some important points to remember when defining rules:

  • You should never create two rules with identical scopes (Log + Source + Operation). Consider merging them instead.

  • Rules without Log scope are evaluated before any log-specific rules are considered.

  • When evaluating whether to log an event, the most specific of the matching rules gets to decide.

    • A rule with Source scope is more specific than a rule without any scope.

    • A rule with Operation scope is more specific than a rule without any scope.

    • A rule with Operation scope is more specific than a rule with Source scope.

    • A rule with Source + Operation scope is the most specific one possible.

  • Additional properties are added from all matching rules.

Create a ConfigurableLog

var rules = new[]
{
    new LogConfigurationRule { Log = "c1", Source = "noisy", MinimumLevel = LogLevel.Warn},
    new LogConfigurationRule { Log = "c2", Enabled = false },
    new LogConfigurationRule { Log = "c3", Properties = new Dictionary<string, string> { ["key"] = "value" } }
};

var log = new ConfigurableLogBuilder()
    .AddLog("c1", new SynchronousConsoleLog())
    .AddLog("c2", new SynchronousConsoleLog())
    .AddLog("c3", new SynchronousConsoleLog())
    .SetRules(rules)
    .Build();

Rules can also be supplied with an IObservable<LogConfigurationRule[]>. Here's an equivalent of the configuration above using Vostok.Configuration library and storing rules in JSON file:

var source = new JsonFileSource("log-rules.json");
var rulesObservable = ConfigurationProvider.Default.Observe<LogConfigurationRule[]>(source);

var log = new ConfigurableLogBuilder()
    .AddLog("c1", new SynchronousConsoleLog())
    .AddLog("c2", new SynchronousConsoleLog())
    .AddLog("c3", new SynchronousConsoleLog())
    .SetRules(rulesObservable)
    .Build();
[
    { "Log": "c1", "Source": "noisy", "MinimumLevel": "Warn" },
    { "Log": "c2", "Enabled": "false" },
    { "Log": "c3", "Properties": { "key": "value" } }
]
PreviousCustom output templates

Last updated 5 years ago

Limits the scope of the rule to events with having given prefix.

Limits the scope of the rule to events with having given prefix.

source context
operation context