> For the complete documentation index, see [llms.txt](https://vostok.gitbook.io/logging/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://vostok.gitbook.io/logging/prototype/implementations/filelog.md).

# FileLog

A log which outputs events to a file.

### Set Up

Include `Logging.File` library in project:

```csharp
using Vostok.Logging.File;
```

&#x20;An `ILog` is created using the `FileLog`:

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

Create logs:

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

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

```csharp
FileLog.FlushAll();
```

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

```csharp
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:

```csharp
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:

```csharp
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](/logging/prototype/advanced-usage.md) 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.

```csharp
// 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                                                                               |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------- |
| <p></p><p><a href="https://github.com/vostok/logging.file/blob/master/Vostok.Logging.File/Configuration/FileLogSettings.cs"><strong>FilePath</strong></a></p>                   | <p></p><p>Path to the log file.</p>                                                       |
| <p></p><p><a href="https://github.com/vostok/logging.file/blob/master/Vostok.Logging.File/Configuration/FileLogSettings.cs"><strong>OutputTemplate</strong></a></p>             | <p></p><p>Used to render log messages.</p>                                                |
| <p></p><p><a href="https://github.com/vostok/logging.file/blob/master/Vostok.Logging.File/Configuration/FileLogSettings.cs"><strong>FormatProvider</strong></a></p>             | If specified, this IFormatProvider will be used when formatting log property values.      |
| <p></p><p><a href="https://github.com/vostok/logging.file/blob/master/Vostok.Logging.File/Configuration/FileLogSettings.cs"><strong>FileOpenMode</strong></a></p>               | Specifies the way to treat an existing log file: append (default) or rewrite.             |
| <p></p><p><a href="https://github.com/vostok/logging.file/blob/master/Vostok.Logging.File/Configuration/FileLogSettings.cs"><strong>RollingStrategy</strong></a></p>            | An optional rolling strategy (disabled by default).                                       |
| <p></p><p><a href="https://github.com/vostok/logging.file/blob/master/Vostok.Logging.File/Configuration/FileLogSettings.cs"><strong>Encoding</strong></a></p>                   | Output text encoding (UTF-8 by default).                                                  |
| <p></p><p><a href="https://github.com/vostok/logging.file/blob/master/Vostok.Logging.File/Configuration/FileLogSettings.cs"><strong>OutputBufferSize</strong></a></p>           | Output buffer size, in bytes (64K by default).                                            |
| <p></p><p><a href="https://github.com/vostok/logging.file/blob/master/Vostok.Logging.File/Configuration/FileLogSettings.cs"><strong>EnabledLogLevels</strong></a></p>           | A whitelist of enabled LogLevels.                                                         |
| <p></p><p><a href="https://github.com/vostok/logging.file/blob/master/Vostok.Logging.File/Configuration/FileLogSettings.cs"><strong>EventsQueueCapacity</strong></a></p>        | Capacity of the internal log events queue.                                                |
| <p></p><p><a href="https://github.com/vostok/logging.file/blob/master/Vostok.Logging.File/Configuration/FileLogSettings.cs"><strong>EventsBufferCapacity</strong></a></p>       | Specifies how many log events are processed in one iteration for each file.               |
| <p></p><p><a href="https://github.com/vostok/logging.file/blob/master/Vostok.Logging.File/Configuration/FileLogSettings.cs"><strong>FileSettingsUpdateCooldown</strong></a></p> | Cooldown for enforcing file-related settings (name, rolling strategy, buffer size, etc.). |
