-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLogHeaderCollection.cs
More file actions
135 lines (81 loc) · 3.58 KB
/
LogHeaderCollection.cs
File metadata and controls
135 lines (81 loc) · 3.58 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
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
using Gsemac.Collections;
using Gsemac.Core;
using Gsemac.IO.Logging.Extensions;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace Gsemac.IO.Logging {
public class LogHeaderCollection :
ILogHeaderCollection {
// Public members
public string this[string key] {
get => dict[key]();
set => dict[key] = () => value;
}
public ICollection<string> Keys => dict.Keys;
public ICollection<string> Values => new LazyReadOnlyCollection<string>(dict.Values.Select(f => f()));
public int Count => dict.Count;
public bool IsReadOnly => false;
public LogHeaderCollection() :
this(true) {
}
public LogHeaderCollection(bool addDefaultHeaders) {
if (addDefaultHeaders) {
this.Add(LogHeaderKey.ProductVersion, () => Assembly.GetEntryAssembly().GetName().Version.ToString());
this.Add(LogHeaderKey.ClrVersion, () => EnvironmentUtilities.GetClrVersion().ToString());
this.Add(LogHeaderKey.FrameworkVersion, () => EnvironmentUtilities.GetFrameworkVersion().ToString());
this.Add(LogHeaderKey.OSVersion, () => Environment.OSVersion.ToString() + string.Format(" ({0})", Environment.Is64BitOperatingSystem ? "64-bit" : "32-bit"));
this.Add(LogHeaderKey.Locale, () => System.Globalization.CultureInfo.InstalledUICulture.Name);
this.Add(LogHeaderKey.Path, () => PathUtilities.AnonymizePath(Assembly.GetEntryAssembly().Location));
this.Add(LogHeaderKey.WorkingDirectory, () => PathUtilities.AnonymizePath(System.IO.Directory.GetCurrentDirectory()));
this.Add(LogHeaderKey.Timestamp, () => DateTime.Now.ToString());
}
}
public void Add(string key, Func<string> getter) {
dict[key] = getter;
}
public void Add(string key, string value) {
Add(key, () => value);
}
public void Add(KeyValuePair<string, string> item) {
Add(item.Key, () => item.Value);
}
public void Clear() {
dict.Clear();
}
public bool Contains(KeyValuePair<string, string> item) {
return ContainsKey(item.Key);
}
public bool ContainsKey(string key) {
return dict.ContainsKey(key);
}
public void CopyTo(KeyValuePair<string, string>[] array, int arrayIndex) => throw new NotImplementedException();
public IEnumerator<KeyValuePair<string, string>> GetEnumerator() {
return dict.Select(pair => new KeyValuePair<string, string>(pair.Key, pair.Value())).GetEnumerator();
}
public bool Remove(string key) {
return dict.Remove(key);
}
public bool Remove(KeyValuePair<string, string> item) {
return Remove(item.Key);
}
public bool TryGetValue(string key, out string value) {
if (dict.TryGetValue(key, out Func<string> getter)) {
value = getter();
return true;
}
else {
value = default;
return false;
}
}
IEnumerator IEnumerable.GetEnumerator() {
return GetEnumerator();
}
public static LogHeaderCollection Empty => new LogHeaderCollection(false);
// Private members
private readonly IDictionary<string, Func<string>> dict = new OrderedDictionary<string, Func<string>>();
}
}