Skip to content

Commit 907b3e4

Browse files
Added log correlation and revised background tasks layout (#230)
1 parent 128bbcc commit 907b3e4

File tree

10 files changed

+443
-233
lines changed

10 files changed

+443
-233
lines changed

gaseous-server/Classes/Common.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Concurrent;
23
using System.Security.Cryptography;
34

45
namespace gaseous_server.Classes
@@ -111,5 +112,30 @@ public static string NormalizePath(string path)
111112
.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
112113
}
113114
}
115+
116+
/// <summary>
117+
/// Provides a way to set contextual data that flows with the call and
118+
/// async context of a test or invocation.
119+
/// </summary>
120+
public static class CallContext
121+
{
122+
static ConcurrentDictionary<string, AsyncLocal<object>> state = new ConcurrentDictionary<string, AsyncLocal<object>>();
123+
124+
/// <summary>
125+
/// Stores a given object and associates it with the specified name.
126+
/// </summary>
127+
/// <param name="name">The name with which to associate the new item in the call context.</param>
128+
/// <param name="data">The object to store in the call context.</param>
129+
public static void SetData(string name, object data) =>
130+
state.GetOrAdd(name, _ => new AsyncLocal<object>()).Value = data;
131+
132+
/// <summary>
133+
/// Retrieves an object with the specified name from the <see cref="CallContext"/>.
134+
/// </summary>
135+
/// <param name="name">The name of the item in the call context.</param>
136+
/// <returns>The object in the call context associated with the specified name, or <see langword="null"/> if not found.</returns>
137+
public static object GetData(string name) =>
138+
state.TryGetValue(name, out AsyncLocal<object> data) ? data.Value : null;
139+
}
114140
}
115141

gaseous-server/Classes/Logging.cs

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,37 @@ static public void Log(LogType EventType, string ServerProcess, string Message,
7575
LogToDisk(logItem, TraceOutput, null);
7676
}
7777

78+
string correlationId;
79+
if (CallContext.GetData("CorrelationId").ToString() == null)
80+
{
81+
correlationId = "";
82+
}
83+
else
84+
{
85+
correlationId = CallContext.GetData("CorrelationId").ToString();
86+
}
87+
88+
string callingProcess;
89+
if (CallContext.GetData("CallingProcess").ToString() == null)
90+
{
91+
callingProcess = "";
92+
}
93+
else
94+
{
95+
callingProcess = CallContext.GetData("CallingProcess").ToString();
96+
}
97+
7898
Database db = new Database(Database.databaseType.MySql, Config.DatabaseConfiguration.ConnectionString);
79-
string sql = "DELETE FROM ServerLogs WHERE EventTime < @EventRententionDate; INSERT INTO ServerLogs (EventTime, EventType, Process, Message, Exception) VALUES (@EventTime, @EventType, @Process, @Message, @Exception);";
99+
string sql = "DELETE FROM ServerLogs WHERE EventTime < @EventRententionDate; INSERT INTO ServerLogs (EventTime, EventType, Process, Message, Exception, CorrelationId, CallingProcess) VALUES (@EventTime, @EventType, @Process, @Message, @Exception, @correlationid, @callingprocess);";
80100
Dictionary<string, object> dbDict = new Dictionary<string, object>();
81101
dbDict.Add("EventRententionDate", DateTime.UtcNow.AddDays(Config.LoggingConfiguration.LogRetention * -1));
82102
dbDict.Add("EventTime", logItem.EventTime);
83103
dbDict.Add("EventType", logItem.EventType);
84104
dbDict.Add("Process", logItem.Process);
85105
dbDict.Add("Message", logItem.Message);
86106
dbDict.Add("Exception", Common.ReturnValueIfNull(logItem.ExceptionValue, "").ToString());
107+
dbDict.Add("correlationid", correlationId);
108+
dbDict.Add("callingprocess", callingProcess);
87109

88110
try
89111
{
@@ -184,6 +206,24 @@ static public List<LogItem> GetLogs(LogsViewModel model)
184206
}
185207
}
186208

209+
if (model.CorrelationId != null)
210+
{
211+
if (model.CorrelationId.Length > 0)
212+
{
213+
dbDict.Add("correlationId", model.CorrelationId);
214+
whereClauses.Add("CorrelationId = @correlationId");
215+
}
216+
}
217+
218+
if (model.CallingProcess != null)
219+
{
220+
if (model.CallingProcess.Length > 0)
221+
{
222+
dbDict.Add("callingProcess", model.CallingProcess);
223+
whereClauses.Add("CallingProcess = @callingProcess");
224+
}
225+
}
226+
187227
// compile WHERE clause
188228
string whereClause = "";
189229
if (whereClauses.Count > 0)
@@ -220,7 +260,9 @@ static public List<LogItem> GetLogs(LogsViewModel model)
220260
EventType = (LogType)row["EventType"],
221261
Process = (string)row["Process"],
222262
Message = (string)row["Message"],
223-
ExceptionValue = (string)row["Exception"]
263+
ExceptionValue = (string)row["Exception"],
264+
CorrelationId = (string)row["CorrelationId"],
265+
CallingProcess = (string)row["CallingProcess"]
224266
};
225267

226268
logs.Add(log);
@@ -243,6 +285,8 @@ public class LogItem
243285
public DateTime EventTime { get; set; }
244286
public LogType? EventType { get; set; }
245287
public string Process { get; set; } = "";
288+
public string CorrelationId { get; set; } = "";
289+
public string? CallingProcess { get; set; } = "";
246290
private string _Message = "";
247291
public string Message
248292
{
@@ -267,6 +311,8 @@ public class LogsViewModel
267311
public DateTime? StartDateTime { get; set; }
268312
public DateTime? EndDateTime { get; set; }
269313
public string? SearchText { get; set; }
314+
public string? CorrelationId { get; set; }
315+
public string? CallingProcess { get; set; }
270316
}
271317
}
272318
}

0 commit comments

Comments
 (0)