-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathLogging.cs
72 lines (65 loc) · 2.55 KB
/
Logging.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
using log4net.Core;
using ReHUD;
namespace log4net.Appender {
public class FloodRollingAppender : RollingFileAppender {
public static void ProcessLoggingEvent(LoggingEvent loggingEvent) {
if (loggingEvent.MessageObject is LogMessage logMessage)
{
loggingEvent.Properties["StartTimestamp"] = logMessage.StartTime;
loggingEvent.Properties["EndTimestamp"] = logMessage.EndTime == "" ? "" : " to " + logMessage.EndTime;
loggingEvent.Properties["Message"] = logMessage.Message;
}
else
{
loggingEvent.Properties["StartTimestamp"] = DateTime.Now.ToString(LogMessage.timeFormat);
loggingEvent.Properties["EndTimestamp"] = "";
loggingEvent.Properties["Message"] = loggingEvent.MessageObject;
}
}
protected override void Append(LoggingEvent loggingEvent) {
ProcessLoggingEvent(loggingEvent);
base.Append(loggingEvent);
}
}
public class FloodConsoleAppender : ConsoleAppender {
protected override void Append(LoggingEvent loggingEvent) {
FloodRollingAppender.ProcessLoggingEvent(loggingEvent);
base.Append(loggingEvent);
}
}
}
namespace ReHUD {
public class LogMessage
{
public static readonly string timeFormat = "yyyy-MM-dd HH:mm:ss.fff";
public string StartTime { get; set; }
public string EndTime { get; set; }
public string Message { get; set; }
public LogMessage(double startTimestamp, double endTimestamp, string message)
{
var startTime = GetTimeString(startTimestamp, true);
var endTime = GetTimeString(endTimestamp);
StartTime = startTime;
EndTime = endTime;
Message = message;
}
public static string GetTimeString(double timestamp, bool useNow = false) {
if (timestamp == -1) {
if (useNow) {
return DateTime.Now.ToString(timeFormat);
} else {
return "";
}
} else {
return UnixTimeStampToDateTime(timestamp).ToString(timeFormat);
}
}
public static DateTime UnixTimeStampToDateTime(double unixTimeStamp)
{
// Unix timestamp is seconds past epoch
DateTime dateTime = DateTime.UnixEpoch;
dateTime = dateTime.AddSeconds(unixTimeStamp).ToLocalTime();
return dateTime;
}
}
}