mirror of
https://github.com/Ellpeck/ExtremelySimpleLogger.git
synced 2024-11-21 17:53:28 +01:00
rethrow file and directory think exceptions with proper information
This commit is contained in:
parent
7a5929f8e8
commit
46bb08f60b
2 changed files with 52 additions and 19 deletions
|
@ -36,24 +36,32 @@ namespace ExtremelySimpleLogger {
|
|||
public DirectorySink(DirectoryInfo directory, int maxFiles = 10, bool reopenOnWrite = false, string dateFormat = DefaultDateFormat) {
|
||||
this.reopenOnWrite = reopenOnWrite;
|
||||
|
||||
if (!directory.Exists)
|
||||
directory.Create();
|
||||
try {
|
||||
if (!directory.Exists)
|
||||
directory.Create();
|
||||
} catch (Exception e) {
|
||||
throw new IOException($"Failed to create directory sink directory {directory}", e);
|
||||
}
|
||||
|
||||
// delete old files
|
||||
var files = directory.GetFiles();
|
||||
if (files.Length >= maxFiles) {
|
||||
// order files by their creation time so that older files are deleted first
|
||||
var ordered = files.OrderBy(f => f.CreationTime).ToList();
|
||||
while (ordered.Count >= maxFiles) {
|
||||
ordered[0].Delete();
|
||||
ordered.RemoveAt(0);
|
||||
try {
|
||||
// delete old files
|
||||
var files = directory.GetFiles();
|
||||
if (files.Length >= maxFiles) {
|
||||
// order files by their creation time so that older files are deleted first
|
||||
var ordered = files.OrderBy(f => f.CreationTime).ToList();
|
||||
while (ordered.Count >= maxFiles) {
|
||||
ordered[0].Delete();
|
||||
ordered.RemoveAt(0);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new IOException($"Failed to delete old files in directory sink {directory}", e);
|
||||
}
|
||||
|
||||
var date = DateTime.Now.ToString(dateFormat);
|
||||
this.file = new FileInfo(Path.Combine(directory.FullName, $"{date}.txt"));
|
||||
if (!reopenOnWrite) {
|
||||
this.writer = this.file.AppendText();
|
||||
this.writer = this.Append();
|
||||
this.writer.AutoFlush = true;
|
||||
}
|
||||
}
|
||||
|
@ -67,7 +75,7 @@ namespace ExtremelySimpleLogger {
|
|||
protected override void Log(Logger logger, LogLevel level, string s) {
|
||||
lock (this.file) {
|
||||
if (this.reopenOnWrite) {
|
||||
using (var w = this.file.AppendText())
|
||||
using (var w = this.Append())
|
||||
w.WriteLine(s);
|
||||
} else {
|
||||
this.writer.WriteLine(s);
|
||||
|
@ -86,5 +94,13 @@ namespace ExtremelySimpleLogger {
|
|||
}
|
||||
}
|
||||
|
||||
private StreamWriter Append() {
|
||||
try {
|
||||
return this.file.AppendText();
|
||||
} catch (Exception e) {
|
||||
throw new IOException($"Failed to append to directory sink file {this.file}", e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -1,3 +1,4 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace ExtremelySimpleLogger {
|
||||
|
@ -33,15 +34,23 @@ namespace ExtremelySimpleLogger {
|
|||
this.reopenOnWrite = reopenOnWrite;
|
||||
this.file = file;
|
||||
|
||||
var dir = file.Directory;
|
||||
if (dir != null && !dir.Exists)
|
||||
dir.Create();
|
||||
try {
|
||||
var dir = file.Directory;
|
||||
if (dir != null && !dir.Exists)
|
||||
dir.Create();
|
||||
} catch (Exception e) {
|
||||
throw new IOException($"Failed to create directory for file sink {file}", e);
|
||||
}
|
||||
|
||||
if (file.Exists && (!append || file.Length >= fileSizeLimit))
|
||||
file.Delete();
|
||||
try {
|
||||
if (file.Exists && (!append || file.Length >= fileSizeLimit))
|
||||
file.Delete();
|
||||
} catch (Exception e) {
|
||||
throw new IOException($"Failed to delete file sink file {file}", e);
|
||||
}
|
||||
|
||||
if (!reopenOnWrite) {
|
||||
this.writer = file.AppendText();
|
||||
this.writer = this.Append();
|
||||
this.writer.AutoFlush = true;
|
||||
}
|
||||
}
|
||||
|
@ -55,7 +64,7 @@ namespace ExtremelySimpleLogger {
|
|||
protected override void Log(Logger logger, LogLevel level, string s) {
|
||||
lock (this.file) {
|
||||
if (this.reopenOnWrite) {
|
||||
using (var w = this.file.AppendText())
|
||||
using (var w = this.Append())
|
||||
w.WriteLine(s);
|
||||
} else {
|
||||
this.writer.WriteLine(s);
|
||||
|
@ -74,5 +83,13 @@ namespace ExtremelySimpleLogger {
|
|||
}
|
||||
}
|
||||
|
||||
private StreamWriter Append() {
|
||||
try {
|
||||
return this.file.AppendText();
|
||||
} catch (Exception e) {
|
||||
throw new IOException($"Failed to append to file sink {this.file}", e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue