Skip to content

Commit

Permalink
[BC] FileLoggerProcessor.LogFileInfo: encapsulation revised (setters …
Browse files Browse the repository at this point in the history
…of AppendStream, EnsurePreambleAsync hidden)

+ minor optimization for LogFileAccessMode.OpenTemporarily
  • Loading branch information
adams85 committed Feb 4, 2020
1 parent 2af30b1 commit b1488fc
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions source/FileLogger/FileLoggerProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,23 @@ protected class LogFileInfo

public int Counter { get; set; }
public string CurrentPath { get; set; }
public Func<LogFileInfo, CancellationToken, ValueTask> EnsurePreambleAsync { get; set; }
public Stream AppendStream { get; set; }

public Stream AppendStream { get; private set; }
public Func<LogFileInfo, CancellationToken, ValueTask> EnsurePreambleAsync { get; private set; }

public void OpenAppendStream(IFileInfo fileInfo)
{
AppendStream = FileAppender.CreateAppendStream(fileInfo);
EnsurePreambleAsync = FileLoggerProcessor.EnsurePreambleAsync;

// the compiler creates a cached delegate for non-capturing lambda expressions,
// so frequent allocation in the case of LogFileAccessMode.OpenTemporarily can be avoided this way
EnsurePreambleAsync = async (logFile, cancellationToken) =>
{
if (logFile.AppendStream.Length == 0)
await WriteBytesAsync(logFile, logFile.Encoding.GetPreamble(), cancellationToken).ConfigureAwait(false);
logFile.EnsurePreambleAsync = (lf, ct) => default;
};
}

public void CloseAppendStream()
Expand Down Expand Up @@ -325,23 +335,11 @@ protected virtual bool UpdateFilePath(LogFileInfo logFile, FileLogEntry entry, C
return true;
}

private static async ValueTask EnsurePreambleAsync(LogFileInfo logFile, CancellationToken cancellationToken)
{
if (logFile.AppendStream.Length == 0)
{
var preamble = logFile.Encoding.GetPreamble();
await WriteBytesAsync(logFile, preamble, cancellationToken).ConfigureAwait(false);
}

logFile.EnsurePreambleAsync = (lf, ct) => default;
}

protected virtual async ValueTask WriteEntryCoreAsync(LogFileInfo logFile, FileLogEntry entry, CancellationToken cancellationToken)
{
await logFile.EnsurePreambleAsync(logFile, cancellationToken).ConfigureAwait(false);

var data = logFile.Encoding.GetBytes(entry.Text);
await WriteBytesAsync(logFile, data, cancellationToken).ConfigureAwait(false);
await WriteBytesAsync(logFile, logFile.Encoding.GetBytes(entry.Text), cancellationToken).ConfigureAwait(false);
}

private async ValueTask WriteEntryAsync(LogFileInfo logFile, FileLogEntry entry, CancellationToken cancellationToken)
Expand Down

0 comments on commit b1488fc

Please sign in to comment.