FileLog

A log which outputs events to a file.

Set Up

Include Logging.File library in project:

using Vostok.Logging.File;

An ILog is created using the FileLog:

var log = new FileLog(
    new FileLogSettings { FilePath = "log.txt" });

Create logs:

log.Info("1");
log.Debug("2");
log.Warn("3");

Use Flush or FlushAsync to ensure that logged events are written to file:

FileLog.FlushAll();

Configure the log. For example, change the file name. Let it contain the current time:

ILog flog = new FileLog(new FileLogSettings
{
    FilePath = Path.Combine($"{DateTime.Now:yyyy-MM-dd.HH-mm-ss}.log")     
});

Choose enabled log levels. Only messages of these levels will be written to the file:

ILog flog = new FileLog(new FileLogSettings
{
    FilePath = Path.Combine($"{DateTime.Now:yyyy-MM-dd.HH-mm-ss}.log"),
    EnabledLogLevels = new [] {LogLevel.Error, LogLevel.Fatal},      
});

Change encoding:

ILog flog = new FileLog(new FileLogSettings
{
    FilePath = Path.Combine($"{DateTime.Now:yyyy-MM-dd.HH-mm-ss}.log"),
    EnabledLogLevels = new [] {LogLevel.Error, LogLevel.Fatal},
    Encoding = Encoding.BigEndianUnicode
});

See the section about Advanced Usage for samples of settings usage.

Configurations

The above is an example of how to configure a log file from code. However, this is not the only way to build this process.

There are two ways to configure Vostok's FileLog:

  • create settings file;

  • set settings provider.

// configure from code — settings instance
var log1 = new FileLog(new FileLogSettings());

// configure from code — settings instance provider
var log2 = new FileLog(() => new FileLogSettings());

// configure from file
var log3 = new FileLog(new JsonFileSource("log3.json"));

Sample configuration file :

*тут я не умею, наверное*

Settings

These parameters are adjusted in FileLogSettings:

Parameters

Description

Path to the log file.

Used to render log messages.

If specified, this IFormatProvider will be used when formatting log property values.

Specifies the way to treat an existing log file: append (default) or rewrite.

An optional rolling strategy (disabled by default).

Output text encoding (UTF-8 by default).

Output buffer size, in bytes (64K by default).

A whitelist of enabled LogLevels.

Capacity of the internal log events queue.

Specifies how many log events are processed in one iteration for each file.

Cooldown for enforcing file-related settings (name, rolling strategy, buffer size, etc.).

Last updated