Added in old code for logging library

This commit is contained in:
colincreasman 2022-01-04 13:53:20 -08:00
parent a424b6f7a2
commit 4f8aeaead8
5 changed files with 119 additions and 47 deletions

View File

@ -8,7 +8,12 @@ namespace TeamHobby.HobbyProjectGenerator.ServiceLayer
{
public interface ILogger
{
bool Log(LogEntry log);
// private IDataSource<string> _datasource = T;
// attempts to connect to the data source provided by the type T
// returns true if the connection is successful
// public bool Connect();
public bool Log(LogEntry Log);
// IList<string> GetAllLogs();

View File

@ -8,9 +8,9 @@ namespace TeamHobby.HobbyProjectGenerator.ServiceLayer.Contracts
{
public interface ILoggerFactory
{
ILogger CreateLogger()
{
return new DBLogger();
}
ILogger CreateLogger();
}
}

View File

@ -11,9 +11,10 @@ namespace TeamHobby.HobbyProjectGenerator.ServiceLayer.Implementations
{
public DBLoggerFactory()
{
//this._logDatabase = logDatabase;
}
public ILogger CreateLogger()
{
public ILogger CreateLogger() {
return new DBLogger();
}
}

View File

@ -4,9 +4,8 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TeamHobby.HobbyProjectGenerator.ServiceLayer
{
public enum LogLevel
public enum LogLevel
{
Info, Debug, Warning, Error
}
@ -16,38 +15,60 @@ namespace TeamHobby.HobbyProjectGenerator.ServiceLayer
View, Business, Server, Data, Datastore
}
namespace TeamHobby.HobbyProjectGenerator.Logging.Contracts
{
// LogEntry is a record type with init properties for each field
// ensures that log entries are immutable after initialization
// LogEntry is a record type with init properties for each field
// ensures that log entries are immutable after initialization
public record LogEntry
{
public LogLevel level { get; init; }
public LogCategory category { get; init; }
public string user { get; init; }
public string description { get; init; }
public DateTime timestamp { get; init; }
private LogLevel _level { get; init; }
private LogCategory _category { get; init; }
private string _user { get; init; }
private string _description { get; init; }
// Constructor requireing only the description as an arg
// Assigns default values for LogLevel (Info), LogCategory (Server), user ("System"), and the UTC time at initialization for timestamp
public LogEntry(string description)
{
this.level = LogLevel.Info;
this.category = LogCategory.Server;
this.user = "System";
this.description = description;
this.timestamp = DateTime.UtcNow;
}
// Constructor with args for all fields except timestamp
// timestamp is always set to the UTC time at the time of initialization
public LogEntry(LogLevel level, string user, LogCategory category, string description)
public LogEntry(LogLevel level, LogCategory category, string user, string description)
{
this.level = level;
this.category = category;
this.user = user;
this.description = description;
this.timestamp = DateTime.UtcNow;
this._level = level;
this._category = category;
this._user = user;
this._description = description;
}
// Constructor requiring only the description as an arg
// Assigns default values for LogLevel (Info), LogCategory (Server), user ("System"), and the UTC time at initialization for timestamp
// public LogEntry(string description)
// {
// this._level = LogLevel.Debug;
// this._category = LogCategory.Server;
// this._user = "SYSTEM";
// this._description = description;
// }
public LogLevel GetLevel() {
return this._level;
}
public LogCategory GetCategory() {
return this._category;
}
public string GetUser() {
return this._user;
}
public string GetDescription() {
return this._description;
}
public void ShowLog() {
Console.WriteLine("Timestamp: {0} | Level: {1}, | Category: {2}, | User: {3}, | Description: '{4}' ", DateTime.Now, this._level.ToString(),this._category.ToString(), this._user, this._description);
}
}
}

View File

@ -10,36 +10,81 @@ using TeamHobby.HobbyProjectGenerator.ServiceLayer.Implementations;
namespace TeamHobby.HobbyProjectGenerator.ServiceLayer
{
internal class LoggingManager
public class LoggingManager
{
private readonly IDataSource<string> _conn;
private readonly ILogger _logger;
private readonly ILoggerFactory _factory;
private readonly LogEntry _logEntry;
// private static IDataSource<string> _datasource;
private ILogger _logger;
private ILoggerFactory _factory;
private LogEntry _logEntry;
// assign default values for LogEntry fields
// these are not readonly because they can be overwritten to specify logging at specific field(s) from the main controller by using the public setters for each field
private LogLevel _level = LogLevel.Debug;
private LogCategory _category = LogCategory.Server;
private string _user = "System";
// Both Constructors take an IDataSource arg to make logging extensible to future data sources
// first constructor has no additional args - defaults to using the DBFactiry implementation
public LoggingManager(IDataSource<string> dataSource)
// first constructor has no additional args - defaults to using a DBLoggerFactory to log to database
public LoggingManager()
{
_conn = dataSource;
// _datasource = datasource;
_logEntry = new LogEntry(_level, _category, _user, "Logging Manager instantiated");
_factory = new DBLoggerFactory();
_logger = _factory.CreateLogger();
}
// second constructor takes an additional ILoggerFactory arg - allows for extensible logger types
public LoggingManager(IDataSource<string> dataSource, ILoggerFactory factory)
public LoggingManager(ILoggerFactory factory)
{
_conn = dataSource;
//_datasource = datasource;
_logEntry = new LogEntry(_level, _category, _user, "Logging Manager instantiated");
_factory = factory;
_logger = _factory.CreateLogger();
}
public void Process()
{
// // used to create a template entry with the desired fields to write entries at
// public void CreateLog(LogLevel level, LogCategory category, string user, string description) {
// _logEntry = new LogEntry(level,
// category, user, description);
// _logger.Log(_logEntry);
// }
// creates a LogEntry object using a description arg
// uses whateever the current assignments for the other fields ares
public bool CreateLog(string description) {
// creates a LogEntry using the passed in description and current assignements from the necessary fields
_logEntry = new LogEntry(_level, _category, _user, description);
// attempt to log the LogEntry by calling the _logger's Log() method
try {
_logger.Log(_logEntry);
return true;
}
catch {
return false;
}
}
public void Level(LogLevel level) {
this._level = level;
}
public void Category(LogCategory category) {
this._category = category;
}
public void User(string user) {
this._user = user;
}
// public static void Source(IDataSource<string> datasource) {
// _datasource = datasource;
// }
// public static IDataSource<string> GetDataSource() {
// return _datasource;
// }
}
}