-
Notifications
You must be signed in to change notification settings - Fork 125
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
Comments
Hi Benjamin, thanks for the note. I'm transferring this to There's nothing built-in to deal with it currently, though, so workaround like logging an event with message |
I think #80 will cover this 👍 |
Hi! Version Via: #80
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 :-) |
I have this on my TODO list to plug this in and see how it works. |
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:
|
@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. |
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.
The text was updated successfully, but these errors were encountered: