-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Logging utility added, more unit test, documentation in code.
- Loading branch information
1 parent
0a85da4
commit c0adfd7
Showing
15 changed files
with
621 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using LiteDB; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace ArcherCore.Logging | ||
{ | ||
/// <summary> | ||
/// Model for creating a log variable. | ||
/// </summary> | ||
public class LoggingModel | ||
{ | ||
/// <value>Property <c>Id</c> represents key for record</value> | ||
[BsonId] | ||
public long Id { get; set; } | ||
|
||
/// <value>Property <c>Message</c> represents the content for the log.</value> | ||
public string Message { get; set; } | ||
|
||
/// <value>Property <c>LogType</c> represents the type of logging (Info, Error, Warning, etc.)</value> | ||
public string LogType { get; set; } | ||
|
||
/// <value>Property <c>Location</c> represents the location of where the error occurred.</value> | ||
public string Location { get; set; } | ||
|
||
/// <value>Property <c>Time</c> represents the time in which the error occurred.</value> | ||
public DateTime Time { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
using LiteDB; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace ArcherCore.Logging | ||
{ | ||
public static class LoggingService | ||
{ | ||
/// <summary> | ||
/// Starts the logging loop for logging services. | ||
/// </summary> | ||
public static async Task LogLoop() | ||
{ | ||
while (true) | ||
{ | ||
while (LoggingVariables.LogQueue.Count > 0) | ||
{ | ||
if (LoggingVariables.LogQueue.TryDequeue(out var content)) | ||
{ | ||
if(!LoggingVariables.UseDb) | ||
{ | ||
var text = "[" + content.Time + "]" + "[" + content.LogType + "]" + " : " + "[" + content.Location + "]" + " : " + content.Message; | ||
await File.AppendAllTextAsync(LoggingVariables.LoggingPath + "Log.txt", Environment.NewLine + text); | ||
} | ||
else | ||
{ | ||
var logsDb = LoggingVariables.LogDb.GetCollection<LoggingModel>(LoggingVariables.ARCHER_LOG); | ||
if(logsDb != null) | ||
{ | ||
var log = new LoggingModel { | ||
Location = content.Location, | ||
LogType = content.LogType, | ||
Message = content.Message, | ||
Time = content.Time | ||
}; | ||
|
||
logsDb.Insert(log); | ||
} | ||
} | ||
} | ||
} | ||
|
||
await Task.Delay(20); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Starts the DB Service for logging service. | ||
/// </summary> | ||
public static async Task InitializeDb() | ||
{ | ||
var mapper = new BsonMapper(); | ||
mapper.RegisterType<DateTime>( | ||
value => value.ToString("o", CultureInfo.InvariantCulture), | ||
bson => DateTime.ParseExact(bson, "o", CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind)); | ||
mapper.RegisterType<DateTimeOffset>( | ||
value => value.ToString("o", CultureInfo.InvariantCulture), | ||
bson => DateTimeOffset.ParseExact(bson, "o", CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind)); | ||
|
||
string path = LoggingVariables.LoggingPath; | ||
LoggingVariables.LogDb = new LiteDatabase(new ConnectionString { Filename = path + LoggingVariables.ARCHER_DB_NAME, Connection = ConnectionType.Direct, ReadOnly = false }, mapper); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
using LiteDB; | ||
using Newtonsoft.Json; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace ArcherCore.Logging | ||
{ | ||
public static class LoggingUtilities | ||
{ | ||
/// <summary> | ||
/// Add a row to the log queue | ||
/// </summary> | ||
/// <param name="message">input string</param> | ||
/// <param name="logType">input string</param> | ||
/// <param name="location">input string</param> | ||
/// <param name="time">input DateTime</param> | ||
public static async Task Log(string message, string logType, string location, DateTime time) | ||
{ | ||
LoggingVariables.LogQueue.Enqueue((message, logType, location, DateTime.Now)); | ||
} | ||
|
||
/// <summary> | ||
/// Gets the most recent 100 records by default for a log | ||
/// </summary> | ||
/// <param name="logCount">input int</param> | ||
/// <returns> | ||
/// Returns a List<LoggingModel>? | ||
/// </returns> | ||
public static async Task<List<LoggingModel>?> GetLog(int logCount = 100) | ||
{ | ||
var logsDb = LoggingVariables.LogDb.GetCollection<LoggingModel>(LoggingVariables.ARCHER_LOG); | ||
var recentLogs = logsDb.Find(Query.All(Query.Descending)).Take(logCount).ToList(); | ||
|
||
if(recentLogs.Count > 0 ) | ||
{ | ||
return recentLogs; | ||
} | ||
else | ||
{ | ||
return null; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Gets the most recent 100 records by default for a log and serializes it. | ||
/// </summary> | ||
/// <param name="logCount">input int</param> | ||
/// <returns> | ||
/// Returns a JSON string? | ||
/// </returns> | ||
public static async Task<string?> GetLogJson(int logCount = 100) | ||
{ | ||
var logsDb = LoggingVariables.LogDb.GetCollection<LoggingModel>(LoggingVariables.ARCHER_LOG); | ||
var recentLogs = logsDb.Find(Query.All(Query.Descending)).Take(logCount).ToList(); | ||
|
||
if (recentLogs.Count > 0) | ||
{ | ||
return JsonConvert.SerializeObject(recentLogs); | ||
} | ||
else | ||
{ | ||
return null; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using LiteDB; | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace ArcherCore.Logging | ||
{ | ||
public static class LoggingVariables | ||
{ | ||
public static string LoggingPath = ""; | ||
public static bool UseDb = false; | ||
public static ConcurrentQueue<(string Message, string LogType, string Location, DateTime Time)> LogQueue = new ConcurrentQueue<(string, string, string, DateTime)>(); | ||
public static LiteDatabase LogDb { get; set; } | ||
public const string ARCHER_DB_NAME = @"log.db"; | ||
public const string ARCHER_LOG = "archer_log"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.