-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogWriter.cs
More file actions
46 lines (37 loc) · 1.02 KB
/
LogWriter.cs
File metadata and controls
46 lines (37 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using System;
using System.Globalization;
using System.IO;
namespace SimpleHttp
{
public sealed class LogWriter
{
public const string DefaultTimestampFormat = "yyyy-MM-dd HH:mm:ss";
public static string GetTimestamp(string format) =>
DateTime.Now.ToString(format, CultureInfo.InvariantCulture);
public static string GetTimestamp() => GetTimestamp(DefaultTimestampFormat);
readonly TextWriter writer;
public string TimestampFormat { get; set; }
public LogWriter(TextWriter writer)
{
this.writer = writer;
TimestampFormat = DefaultTimestampFormat;
}
public LogWriter(Stream stream) :
this(new StreamWriter(stream, HttpHelperExtensions.UTF8))
{ }
public LogWriter(string file) :
this(new StreamWriter(file, true, HttpHelperExtensions.UTF8))
{ }
public LogWriter() :
this(Console.Out)
{ }
public void Log(string text)
{
if (TimestampFormat != null)
writer.WriteLine($"[{GetTimestamp(TimestampFormat)}] {text}");
else
writer.WriteLine(text);
writer.Flush();
}
}
}