mirror of
https://github.com/Ellpeck/ExtremelySimpleLogger.git
synced 2024-11-22 10:03:29 +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,9 +36,14 @@ namespace ExtremelySimpleLogger {
|
||||||
public DirectorySink(DirectoryInfo directory, int maxFiles = 10, bool reopenOnWrite = false, string dateFormat = DefaultDateFormat) {
|
public DirectorySink(DirectoryInfo directory, int maxFiles = 10, bool reopenOnWrite = false, string dateFormat = DefaultDateFormat) {
|
||||||
this.reopenOnWrite = reopenOnWrite;
|
this.reopenOnWrite = reopenOnWrite;
|
||||||
|
|
||||||
|
try {
|
||||||
if (!directory.Exists)
|
if (!directory.Exists)
|
||||||
directory.Create();
|
directory.Create();
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new IOException($"Failed to create directory sink directory {directory}", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
// delete old files
|
// delete old files
|
||||||
var files = directory.GetFiles();
|
var files = directory.GetFiles();
|
||||||
if (files.Length >= maxFiles) {
|
if (files.Length >= maxFiles) {
|
||||||
|
@ -49,11 +54,14 @@ namespace ExtremelySimpleLogger {
|
||||||
ordered.RemoveAt(0);
|
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);
|
var date = DateTime.Now.ToString(dateFormat);
|
||||||
this.file = new FileInfo(Path.Combine(directory.FullName, $"{date}.txt"));
|
this.file = new FileInfo(Path.Combine(directory.FullName, $"{date}.txt"));
|
||||||
if (!reopenOnWrite) {
|
if (!reopenOnWrite) {
|
||||||
this.writer = this.file.AppendText();
|
this.writer = this.Append();
|
||||||
this.writer.AutoFlush = true;
|
this.writer.AutoFlush = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -67,7 +75,7 @@ namespace ExtremelySimpleLogger {
|
||||||
protected override void Log(Logger logger, LogLevel level, string s) {
|
protected override void Log(Logger logger, LogLevel level, string s) {
|
||||||
lock (this.file) {
|
lock (this.file) {
|
||||||
if (this.reopenOnWrite) {
|
if (this.reopenOnWrite) {
|
||||||
using (var w = this.file.AppendText())
|
using (var w = this.Append())
|
||||||
w.WriteLine(s);
|
w.WriteLine(s);
|
||||||
} else {
|
} else {
|
||||||
this.writer.WriteLine(s);
|
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;
|
using System.IO;
|
||||||
|
|
||||||
namespace ExtremelySimpleLogger {
|
namespace ExtremelySimpleLogger {
|
||||||
|
@ -33,15 +34,23 @@ namespace ExtremelySimpleLogger {
|
||||||
this.reopenOnWrite = reopenOnWrite;
|
this.reopenOnWrite = reopenOnWrite;
|
||||||
this.file = file;
|
this.file = file;
|
||||||
|
|
||||||
|
try {
|
||||||
var dir = file.Directory;
|
var dir = file.Directory;
|
||||||
if (dir != null && !dir.Exists)
|
if (dir != null && !dir.Exists)
|
||||||
dir.Create();
|
dir.Create();
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new IOException($"Failed to create directory for file sink {file}", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
if (file.Exists && (!append || file.Length >= fileSizeLimit))
|
if (file.Exists && (!append || file.Length >= fileSizeLimit))
|
||||||
file.Delete();
|
file.Delete();
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new IOException($"Failed to delete file sink file {file}", e);
|
||||||
|
}
|
||||||
|
|
||||||
if (!reopenOnWrite) {
|
if (!reopenOnWrite) {
|
||||||
this.writer = file.AppendText();
|
this.writer = this.Append();
|
||||||
this.writer.AutoFlush = true;
|
this.writer.AutoFlush = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -55,7 +64,7 @@ namespace ExtremelySimpleLogger {
|
||||||
protected override void Log(Logger logger, LogLevel level, string s) {
|
protected override void Log(Logger logger, LogLevel level, string s) {
|
||||||
lock (this.file) {
|
lock (this.file) {
|
||||||
if (this.reopenOnWrite) {
|
if (this.reopenOnWrite) {
|
||||||
using (var w = this.file.AppendText())
|
using (var w = this.Append())
|
||||||
w.WriteLine(s);
|
w.WriteLine(s);
|
||||||
} else {
|
} else {
|
||||||
this.writer.WriteLine(s);
|
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