Skip to content

Flush to disk option #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 6, 2016
Merged

Flush to disk option #20

merged 3 commits into from
Oct 6, 2016

Conversation

nblumhardt
Copy link
Member

Fixes #18 (rolling file support needed downstream).

Log.Logger = new LoggerConfiguration()
      .WriteTo.File("log.txt", flushToDiskInterval: TimeSpan.FromSeconds(1))
      .CreateLogger();

Attempts to optimize things a bit so no flush is performed unless events have been written.

The IFlushableFileSink and PeriodicFlushToDiskSink<T> are (reluctantly) public, as this will permit the downstream RollingFileSink to wire up to the same mechanism, without us having to encapsulate the periodic flushing behavior in FileSink and SharedFileSink directly. In the long run, we might get some kind of generialized IFlushable into the base Serilog package and make it available across the board through some kind of generic wrapper.

I verified by hand that the FlushToDisk() is called, but can't come up with an easy way to test it without some kind of multi-process runner. If we query the file size from Windows from the unit test process, it'll always report a consistent view of the world to us :-).

Bonus: discovered the shared file sink tests were inadvertently ignored 😢; after enabling, found a bug preventing size limiting from taking effect. Fixed this too.

@nblumhardt
Copy link
Member Author

CC @pakrym

/// A sink wrapper that periodically flushes the wrapped sink to disk.
/// </summary>
/// <typeparam name="TSink">The type of the wrapped sink.</typeparam>
public class PeriodicFlushToDiskSink<TSink> : ILogEventSink, IDisposable
Copy link

@pakrym pakrym Oct 5, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why generic? To avoid interface method invocation?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'm not so keen on this upon second thoughts. It is to make clear that the passed-in ILogEventSink should also be IFlushableFileSink (no union types in C#) but the code would be a bit nicer to read if non-generic and tested at runtime. Going to change this.

@pakrym
Copy link

pakrym commented Oct 5, 2016

LGTM

@nblumhardt
Copy link
Member Author

Okay, I'm going to make a couple of minor tweaks this morning, then push through a -dev version and follow up with the RollingFile version. Should be all through as pre-release later today.

@nblumhardt nblumhardt merged commit d1fb6f3 into serilog:dev Oct 6, 2016
mderriey added a commit to mderriey/tfs-webhooks that referenced this pull request Jun 19, 2017
Initially the file sink of Serilog didn't have the ability to flush to disk at specified intervals.
This was brough in a newer version (see serilog/serilog-sinks-file#20).
mderriey added a commit to mderriey/tfs-webhooks that referenced this pull request Jun 19, 2017
Initially the file sink of Serilog didn't have the ability to flush to disk at specified intervals.
This was brough in a newer version (see serilog/serilog-sinks-file#20).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants