-
Notifications
You must be signed in to change notification settings - Fork 16
/
Log.cs
161 lines (147 loc) · 4.83 KB
/
Log.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using static WebOne.Program;
namespace WebOne
{
/// <summary>
/// Agent for message logging and displaying
/// </summary>
static class LogAgent
{
static StreamWriter LogStreamWriter = null;
static bool LogStreamWriterReady = true;
/// <summary>
/// Write a message to server log.
/// </summary>
/// <param name="LogMessage">The message string.</param>
/// <param name="Display">Shall the message be displayed.</param>
/// <param name="Write">Shall the message be written.</param>
/// <param name="DisplayText">The message string (which should be displayed if <paramref name="Display"/> is true).</param>
public static void WriteLine(string LogMessage, bool Display = true, bool Write = true, string DisplayText = "")
{
if (Display && !DaemonMode)
if (DisplayText == "") Console.WriteLine(LogMessage);
else Console.WriteLine(DisplayText);
if (Write && LogStreamWriter != null)
{
new Task(() =>
{
while (!LogStreamWriterReady) { }
LogStreamWriterReady = false;
LogStreamWriter.WriteLine(LogMessage);
LogStreamWriterReady = true;
}).Start();
return;
}
}
/// <summary>
/// Open a server log file and begin logging.
/// </summary>
/// <param name="LogFileName">Path to the log file.</param>
/// <param name="Append">Append the log file or overwrite?</param>
public static void OpenLogFile(string LogFileName = null, bool Append = false)
{
if (LogFileName != null)
{
try
{
LogStreamWriter = new StreamWriter(LogFileName, Append) { AutoFlush = true };
string CommandLineArgs = string.Empty;
for (int i = 1; i < Environment.GetCommandLineArgs().Length; i++) { CommandLineArgs += " " + Environment.GetCommandLineArgs()[i]; };
string StartMsg = string.Format("{0}\tWebOne {1}{6} ({2} {3}, Runtime {4} {5}) log started.",
DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss"),
Variables["WOVer"],
RuntimeInformation.OSDescription,
RuntimeInformation.OSArchitecture,
Environment.Version,
RuntimeInformation.ProcessArchitecture,
CommandLineArgs);
LogStreamWriter.WriteLine(StartMsg);
Console.WriteLine("Using event log file {0}.", LogFileName);
}
catch (Exception ex)
{
Console.WriteLine("Cannot use log file {0}: {1}.", LogFileName, ex.Message);
}
}
else
{
LogStreamWriter = null;
Console.WriteLine("Not using log file.");
}
}
/// <summary>
/// Does messages logging into a log file.
/// </summary>
public static bool IsLoggingEnabled
{
get
{
return LogStreamWriter != null;
}
}
}
/// <summary>
/// Log message writer for log agent
/// </summary>
public class LogWriter
{
public DateTime BeginTime = DateTime.Now;
bool FirstTime = true;
/// <summary>
/// Write the text representation of the specified array of objects to
/// server log agent using the specified format information.
/// </summary>
/// <param name="format">A composite format string.</param>
/// <param name="arg">An array of objects to write using format.</param>
/// <seealso cref="Console.WriteLine(string, object)"/>
public void WriteLine(string format, params object[] arg)
{
WriteLine(true, true, format, arg);
}
/// <summary>
/// Write the text representation of the specified array of objects to
/// server log agent using the specified format information.
/// </summary>
/// <param name="display">Shall the representation be displayed in the console window.</param>
/// <param name="format">A composite format string.</param>
/// <param name="arg">An array of objects to write using format.</param>
public void WriteLine(bool display, bool displayTimestamp, string format, params object[] arg)
{
string str;
string timestamp;
string message = string.Format(format, arg);
if (FirstTime)
{
timestamp = string.Format("{0}+0", BeginTime.ToString("dd.MM.yyyy HH:mm:ss.fff")); FirstTime = false;
}
else
{
timestamp = GetTime(BeginTime);
}
if (displayTimestamp) //e.g. ">GET http://example.com/ (127.0.0.1)"
{
if (timestamp.Length < 20) //23.02.2021 15:55:58.283+7600010 = 31 character long
{
str = string.Format("{0}+0\t\t{1}", BeginTime.ToString("dd.MM.yyyy HH:mm:ss.fff"), message);
LogAgent.WriteLine(str, display, false);
str = string.Format("{0}+0\t{1}", BeginTime.ToString("dd.MM.yyyy HH:mm:ss.fff"), message);
LogAgent.WriteLine(str, false, true);
}
else
{
str = string.Format("{0}\t{1}", GetTime(BeginTime), message);
LogAgent.WriteLine(str, true, display);
}
}
else //e.g. "Starting server..."
{
str = string.Format("{0}+0\t{1}", BeginTime.ToString("dd.MM.yyyy HH:mm:ss.fff"), message);
LogAgent.WriteLine(str, display, true, message);
return;
}
}
}
}