-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathConsoleLogger.cs
74 lines (63 loc) · 2.29 KB
/
ConsoleLogger.cs
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using System;
using System.IO;
namespace QtSharp.CLI
{
public class ConsoleLogger
{
/// <summary> Original Console Output Stream. </summary>
/// <value> Original Console Output Stream. </value>
public TextWriter ConsoleStdOutput { get; set; }
/// <summary> Original Console Output Stream. </summary>
/// <value> Original Console Output Stream. </value>
public TextWriter ConsoleErrOutput { get; set; }
/// <summary> Path to the Log File. </summary>
/// <value> Path to the Log File. </value>
public string LogFilePath
{
get
{
return logFilePath;
}
}
protected string logFilePath;
/// <summary> Filestream used for output. </summary>
protected FileStream fstream { get; set; }
/// <summary> Filestream Writer used for output. </summary>
protected StreamWriter fstreamwriter { get; set; }
/// <summary> Default constructor. </summary>
public ConsoleLogger()
{
ConsoleStdOutput = Console.Out;
ConsoleErrOutput = Console.Error;
logFilePath = null;
}
/// <summary> Sets the log file output. </summary>
/// <param name="filename"> Filename of the log file to use. </param>
public void SetLogFile(string filename)
{
logFilePath = Path.Combine("logs", filename);
}
/// <summary> Starts console redirection. </summary>
public void Start()
{
Stop();
fstream = new FileStream(logFilePath, FileMode.Create);
fstreamwriter = new StreamWriter(fstream);
Console.SetOut(fstreamwriter);
Console.SetError(fstreamwriter);
}
/// <summary> Stops console redirection. </summary>
public void Stop()
{
Console.SetOut(ConsoleStdOutput);
Console.SetError(ConsoleErrOutput);
if (fstreamwriter != null) fstreamwriter.Close();
if (fstream != null) fstream.Close();
}
/// <summary> Creates log directory. </summary>
public void CreateLogDirectory()
{
if (Directory.Exists("logs") == false) Directory.CreateDirectory("logs");
}
}
}