Class FileLogExporter
Exports log records to a file as specified by FileLogExporterOptions.
Namespace: Neon.Diagnostics
Assembly: Neon.Common.dll
Syntax
public class FileLogExporter : BaseExporter<LogRecord>
Remarks
This exporter currently supports writing logs as JSON or a bespoke human readable format (the default). You can customize this by using options with the Format to one of the FileLogExporterFormat values (as shown below).
IMPORTANT: To enable the inclusion of log tags in the output, you must
set OpenTelemetryLoggerOptions.ParseStateValues=true
when
configuring your OpenTelemetry options. This is is false
by default.
var loggerFactory = LoggerFactory.Create(
builder =>
{
builder.AddOpenTelemetry(
options =>
{
options.ParseStateValues = true; // <--- SET THIS TO TRUE
options.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(serviceName: ServiceName, serviceVersion: ServiceVersion));
options.AddFileExporter(
options =>
{
options.Format = FileLogExporterFormat.Human;
options.LogFolder = NeonHelper.GetBaseDirtectory();
options.LogFileName = "MyProgram.log";
});
});
});</code></pre>
This exporter supports log file rotation. This is controlled by the FileLimit
and MaxLogFiles options. When the current log file's size equals or exceeds
FileLimit after writing a log event, the exporter will close and rename the
current file by appending a timestamp and start logging to a new file named LogFileName.
FileLimit defaults to 10 MiB and MaxLogFiles
defaults to retain 10 log files.
The rotated files will be named like "LOGFILENAME-YYYY-MM-ddTHH-mm-ss.fffZ.EXT", where LOGFILENAME is the
filename part of LogFileName and EXT is the extension.
MaxLogFiles controls how many log files will be retained.
Constructors
FileLogExporter(FileLogExporterOptions)
Constructs a log exporter that writes log records to standard output and/or standard error as single line JSON objects.
Declaration
public FileLogExporter(FileLogExporterOptions options = null)
Parameters
Type | Name | Description |
---|---|---|
FileLogExporterOptions | options | Optionally specifies the exporter options. |
Methods
Dispose(bool)
Declaration
protected override void Dispose(bool disposing)
Parameters
Type | Name | Description |
---|---|---|
bool | disposing |
Export(in Batch<LogRecord>)
Declaration
public override ExportResult Export(in Batch<LogRecord> batch)
Parameters
Type | Name | Description |
---|---|---|
Batch<LogRecord> | batch |
Returns
Type | Description |
---|---|
ExportResult |
OnForceFlush(int)
Declaration
protected override bool OnForceFlush(int timeoutMilliseconds)
Parameters
Type | Name | Description |
---|---|---|
int | timeoutMilliseconds |
Returns
Type | Description |
---|---|
bool |
OnShutdown(int)
Declaration
protected override bool OnShutdown(int timeoutMilliseconds)
Parameters
Type | Name | Description |
---|---|---|
int | timeoutMilliseconds |
Returns
Type | Description |
---|---|
bool |