lock FileSink and StringSink

This commit is contained in:
Ellpeck 2020-07-29 14:10:49 +02:00
parent a17ebd37ad
commit b989e6d541
2 changed files with 21 additions and 10 deletions

View file

@ -51,11 +51,13 @@ namespace ExtremelySimpleLogger {
/// </summary> /// </summary>
/// <param name="s">The message to log</param> /// <param name="s">The message to log</param>
public override void Log(string s) { public override void Log(string s) {
if (this.reopenOnWrite) { lock (this.file) {
using (var w = this.file.AppendText()) if (this.reopenOnWrite) {
w.WriteLine(s); using (var w = this.file.AppendText())
} else { w.WriteLine(s);
this.writer.WriteLine(s); } else {
this.writer.WriteLine(s);
}
} }
} }
@ -64,8 +66,10 @@ namespace ExtremelySimpleLogger {
/// </summary> /// </summary>
public override void Dispose() { public override void Dispose() {
base.Dispose(); base.Dispose();
if (!this.reopenOnWrite) lock (this.file) {
this.writer.Dispose(); if (!this.reopenOnWrite)
this.writer.Dispose();
}
} }
} }

View file

@ -12,14 +12,20 @@ namespace ExtremelySimpleLogger {
/// The string that this sink currently contains. /// The string that this sink currently contains.
/// Can be cleared using <see cref="Clear"/>. /// Can be cleared using <see cref="Clear"/>.
/// </summary> /// </summary>
public string Value => this.builder.ToString(); public string Value {
get {
lock (this.builder)
return this.builder.ToString();
}
}
/// <summary> /// <summary>
/// Logs the given message, which has already been formatted using <see cref="Sink.Formatter"/>. /// Logs the given message, which has already been formatted using <see cref="Sink.Formatter"/>.
/// </summary> /// </summary>
/// <param name="s">The message to log</param> /// <param name="s">The message to log</param>
public override void Log(string s) { public override void Log(string s) {
this.builder.AppendLine(s); lock (this.builder)
this.builder.AppendLine(s);
} }
/// <summary> /// <summary>
@ -27,7 +33,8 @@ namespace ExtremelySimpleLogger {
/// After this call, <see cref="Value"/> will be empty. /// After this call, <see cref="Value"/> will be empty.
/// </summary> /// </summary>
public void Clear() { public void Clear() {
this.builder.Clear(); lock (this.builder)
this.builder.Clear();
} }
} }