-
Notifications
You must be signed in to change notification settings - Fork 70
Added time and file size options for FileWriter #151
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
Open
RobbieLePommie
wants to merge
1
commit into
DarkRiftNetworking:master
Choose a base branch
from
RobbieLePommie:RobbieLePommie-LogFileLimits
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,38 +23,138 @@ public sealed class FileWriter : LogWriter | |
| /// <summary> | ||
| /// The stream to the log file to write to. | ||
| /// </summary> | ||
| private StreamWriter LogFileStream { get; } | ||
| private StreamWriter LogFileStream { get; set; } | ||
|
|
||
| private readonly object streamLock = new object(); | ||
|
|
||
| /// <summary> | ||
| /// The directory we are writing to. | ||
| /// </summary> | ||
| public string LogFilePath { get; private set; } | ||
|
|
||
| /// <summary> | ||
| /// The format of the path (used for rotation). | ||
| /// </summary> | ||
| public string LogFilePathFormat { get; private set; } | ||
|
|
||
| /// <summary> | ||
| /// The time this file started (for rotation). | ||
| /// </summary> | ||
| public DateTime LogCreated { get; private set; } | ||
|
|
||
| /// <summary> | ||
| /// Number of bytes written. | ||
| /// </summary> | ||
| public int LogBytesWritten { get; private set; } | ||
|
|
||
| /// <summary> | ||
| /// Maximum number of bytes (before rotating). 0 to disable. | ||
| /// </summary> | ||
| public int LogMaxBytes { get; private set; } | ||
|
|
||
| /// <summary> | ||
| /// Maximum number of seconds per log file (before rotating). 0 to disable. | ||
| /// </summary> | ||
| public int LogMaxTimeSeconds { get; private set; } | ||
|
|
||
| /// <summary> | ||
| /// Creates a new file writer with the given plugin load data. | ||
| /// </summary> | ||
| /// <param name="logWriterLoadData">The data for this plugin.</param> | ||
| public FileWriter(LogWriterLoadData logWriterLoadData) : base(logWriterLoadData) | ||
| { | ||
| //Get the log directory concatenated with the date | ||
| LogFilePath = string.Format(logWriterLoadData.Settings["file"], DateTime.Now); | ||
|
|
||
| //Create the actual log directory (will do nothing if it already exists) | ||
| Directory.CreateDirectory(Path.GetDirectoryName(LogFilePath)); | ||
| // Sore the format for creating the filename | ||
| LogFilePathFormat = logWriterLoadData.Settings["file"] ?? "Logs/{0:yyyy-MM-dd}/{0:HH-mm-ss}.txt"; | ||
|
|
||
| // Get the maximum size (in MB, convert to bytes) | ||
| try | ||
| { | ||
| int logMaxSizeMB = int.Parse(logWriterLoadData.Settings["maxSize"] ?? "100"); | ||
| LogMaxBytes = Math.Max(0, logMaxSizeMB * 1000000); | ||
| } | ||
| catch (FormatException) | ||
| { | ||
| LogMaxBytes = 0; | ||
| } | ||
|
|
||
| // Get the maximum time before rotation (in seconds) | ||
| try | ||
| { | ||
| LogMaxTimeSeconds = int.Parse(logWriterLoadData.Settings["maxTime"] ?? "86400"); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be good to have the property named 'maxTimeSeconds' so it's absolutely clear what units it uses |
||
| } | ||
| catch (FormatException) | ||
| { | ||
| LogMaxTimeSeconds = 0; | ||
| } | ||
|
|
||
| CreateStream(); | ||
| } | ||
|
|
||
| private void CreateStream() | ||
| { | ||
| // Only create a new stream if the file name is different from existing. | ||
| string newFilePath = string.Format(LogFilePathFormat, DateTime.Now); | ||
| if (LogFilePath is null || newFilePath != LogFilePath) | ||
| { | ||
| // Close the old stream, if open | ||
| if (!(LogFilePath is null)) | ||
| { | ||
| LogFileStream.WriteLine($"Log File Continues in {newFilePath}"); | ||
| CloseStream(); | ||
| } | ||
|
|
||
| //Get the log directory concatenated with the date | ||
| LogFilePath = string.Format(LogFilePathFormat, DateTime.Now); | ||
|
|
||
| //Create the log file and stream for it | ||
| LogFileStream = new StreamWriter(LogFilePath); | ||
| //Create the actual log directory (will do nothing if it already exists) | ||
| Directory.CreateDirectory(Path.GetDirectoryName(LogFilePath)); | ||
|
|
||
| //Create the log file and stream for it | ||
| LogFileStream = new StreamWriter(LogFilePath); | ||
|
|
||
| // Write | ||
| if (!(LogFilePath is null)) | ||
| { | ||
| LogFileStream.WriteLine($"Log File Continued from {LogFilePath}"); | ||
| LogFileStream.Flush(); | ||
| } | ||
|
|
||
|
|
||
| // Reset the counters | ||
| LogFilePath = newFilePath; | ||
| LogBytesWritten = 0; | ||
| LogCreated = DateTime.Now; | ||
| } | ||
| } | ||
|
|
||
| private void CloseStream() | ||
| { | ||
| LogFileStream.Flush(); | ||
| LogFileStream.Close(); | ||
| LogFileStream.Dispose(); | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public override void WriteEvent(WriteEventArgs args) | ||
| { | ||
| //Write to file | ||
| lock (LogFileStream) | ||
| lock (streamLock) | ||
| { | ||
| LogFileStream.WriteLine(args.FormattedMessage); | ||
|
|
||
| LogFileStream.Flush(); | ||
|
|
||
| // Remember number of btyes written | ||
| LogBytesWritten += args.FormattedMessage.Length + 2; | ||
|
|
||
| // If limits exceeded, then rotate while lock still in place | ||
| // Will only rotate is log file older than a second (to avoid naming clashes) | ||
| if ((LogMaxBytes > 0 && LogBytesWritten >= LogMaxBytes) | ||
| || (LogMaxTimeSeconds > 0 && (int)DateTime.Now.Subtract(LogCreated).TotalSeconds >= LogMaxTimeSeconds) | ||
| ) { | ||
| CreateStream(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -63,8 +163,8 @@ protected override void Dispose(bool disposing) | |
| { | ||
| if (disposing) | ||
| { | ||
| lock (LogFileStream) | ||
| LogFileStream.Dispose(); | ||
| lock (streamLock) | ||
| CloseStream(); | ||
| } | ||
| base.Dispose(disposing); | ||
| } | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be good to have the property named 'maxSizeMb' so it's absolutely clear what units it uses