Skip to content

Hook for Writing to a Log File on Close #81

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

Closed
define-private-public opened this issue Feb 10, 2019 · 6 comments
Closed

Hook for Writing to a Log File on Close #81

define-private-public opened this issue Feb 10, 2019 · 6 comments

Comments

@define-private-public
Copy link

Hi, I really like this logging framework; thanks for making it.

I'm right now developing console/desktop apps, not something like a daemon or webapp which is supposed to stay alive for a long time. I have two sinks being used right now Console & File. I'm wondering if there is some way I could output a "break line" (say the character '-' repeated 80 times on it's own line), for the File sink when the Log is being destroyed or flushed.

That way it would be much easier to discern between multiple runs in the single log file.

@nblumhardt
Copy link
Member

Hi Benjamin, thanks for the note. I'm transferring this to serilog/serilog-sinks-file, since it relates to a feature we've been discussing over there (#48).

There's nothing built-in to deal with it currently, though, so workaround like logging an event with message ------ and then applying a filter or using levelling to keep it out of Console() output might be the best option.

@nblumhardt nblumhardt transferred this issue from serilog/serilog Feb 10, 2019
@nblumhardt nblumhardt changed the title Output break line on flush/close Hook for Writing to a Log File on Close Apr 20, 2019
@nblumhardt
Copy link
Member

I think #80 will cover this 👍

@nblumhardt
Copy link
Member

Hi! Version 4.1.0-dev-00833 on NuGet now supports this, but as a header rather than as a footer; check out:

https://github.com/serilog/serilog-sinks-file/blob/dev/test/Serilog.Sinks.File.Tests/FileSinkTests.cs#L187

Via: #80

FileHeaderWriter in the test project is only an example; by tweaking the logic in there, the --- line could be written only when the file being opened already contains content. I think this will prove more reliable than trying to write on close, as process crashes etc. might prevent it from being called.

If you need a hand to wire it up, please let me know. I'll close this ticket, but if you end up with a snippet showing how it's done, please feel free to post it (or your feedback) in the comments :-)

@define-private-public
Copy link
Author

I have this on my TODO list to plug this in and see how it works.

@define-private-public
Copy link
Author

define-private-public commented May 6, 2019

Here's what I did. I put this somewhere:

/// <summary>
/// On every launch, will add a note at the top to signify the start of the log
/// </summary>
public class LaunchHeader : FileLifecycleHooks
{
    public string Header { get; }

    public LaunchHeader(string header)
    {
        Header = header;
    }

    public override Stream OnFileOpened(Stream underlyingStream, System.Text.Encoding encoding)
    {
        // Always write a new header
        var writer = new StreamWriter(underlyingStream, encoding);
        writer.WriteLine();
        writer.WriteLine();
        writer.WriteLine(Header);
        writer.WriteLine(DateTime.Now);
        writer.WriteLine();
        writer.Flush();
        underlyingStream.Flush();

        return base.OnFileOpened(underlyingStream, encoding);
    }
}

And this is when I'm configuring the log (at startup):

var logger = new LoggerConfiguration()
  .MinimumLevel.Debug()
  .WriteTo.File(
    "log.txt",
    hooks: new LaunchHeader(new string('=', 80))
  )
  .CreateLogger();

And now, at each startup, I get this:



================================================================================
5/5/19 9:25:09 PM

@cocowalla
Copy link
Contributor

cocowalla commented May 6, 2019

@define-private-public I also have a /cocowalla/serilog-sinks-file-header for this.

By default it only inserts the header for new files, but this is configurable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants