Moved logging into service layer
This commit is contained in:
parent
300158ec25
commit
c139b80459
@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace TeamHobby.HobbyProjectGenerator.Logging
|
||||
{
|
||||
public interface ILogger
|
||||
{
|
||||
bool Log(LogEntry log);
|
||||
|
||||
// IList<string> GetAllLogs();
|
||||
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace TeamHobby.HobbyProjectGenerator.Logging.Contracts
|
||||
{
|
||||
public interface ILoggerFactory
|
||||
{
|
||||
ILogger CreateLogger()
|
||||
{
|
||||
return new DBLogger();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace TeamHobby.HobbyProjectGenerator.Logging
|
||||
{
|
||||
public class ConsoleLogger : ILogger
|
||||
{
|
||||
public bool Log(LogEntry log)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,24 @@
|
||||
using System;
|
||||
|
||||
namespace TeamHobby.HobbyProjectGenerator.Logging
|
||||
{
|
||||
public class DBLogger : ILogger
|
||||
{
|
||||
public DBLogger()
|
||||
{
|
||||
}
|
||||
|
||||
public IList<string> GetAllLogs()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public bool Log(LogEntry log)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,20 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using TeamHobby.HobbyProjectGenerator.Logging.Contracts;
|
||||
|
||||
namespace TeamHobby.HobbyProjectGenerator.Logging.Implementations
|
||||
{
|
||||
public class DBLoggerFactory : ILoggerFactory
|
||||
{
|
||||
public DBLoggerFactory()
|
||||
{
|
||||
}
|
||||
public ILogger CreateLogger()
|
||||
{
|
||||
return new DBLogger();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,24 @@
|
||||
using System;
|
||||
using TeamHobby.HobbyProjectGenerator.Logging.Contracts;
|
||||
|
||||
namespace TeamHobby.HobbyProjectGenerator.Logging
|
||||
{
|
||||
public class FileLogger : ILogger
|
||||
{
|
||||
public FileLogger()
|
||||
{
|
||||
}
|
||||
|
||||
public IList<string> GetAllLogs()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public bool Log(LogEntry log)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,58 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
namespace TeamHobby.HobbyProjectGenerator.Logging
|
||||
{
|
||||
// implementation of ILogger that stores logs as they initialize
|
||||
public class InMemoryLogger : ILogger
|
||||
{
|
||||
// make readonly to prevent it from being changed somewhere other than in the constructor
|
||||
private readonly IList<string> _logStore;
|
||||
|
||||
public InMemoryLogger()
|
||||
{
|
||||
// list of current logs
|
||||
_logStore = new List<string>();
|
||||
}
|
||||
|
||||
public InMemoryLogger(IList<string> logStore)
|
||||
{
|
||||
_logStore = logStore;
|
||||
}
|
||||
|
||||
public bool Log(LogEntry entry)
|
||||
{
|
||||
if (entry is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(entry));
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// string interpolation to add UTC timestamp to log description
|
||||
//_logStore.Add($"{DateTime.UtcNow}-> {description}");
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
public IList<string> GetAllLogs()
|
||||
{
|
||||
return _logStore;
|
||||
}
|
||||
|
||||
|
||||
public override bool Equals(object? obj)
|
||||
{
|
||||
return obj is InMemoryLogger logger &&
|
||||
EqualityComparer<IList<string>>.Default.Equals(_logStore, logger._logStore);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return HashCode.Combine(_logStore);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,53 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace TeamHobby.HobbyProjectGenerator.Logging
|
||||
{
|
||||
public enum LogLevel
|
||||
{
|
||||
Info, Debug, Warning, Error
|
||||
}
|
||||
|
||||
public enum LogCategory
|
||||
{
|
||||
View, Business, Server, Data, Datastore
|
||||
}
|
||||
|
||||
// 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; }
|
||||
|
||||
|
||||
// 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)
|
||||
{
|
||||
this.level = level;
|
||||
this.category = category;
|
||||
this.user = user;
|
||||
this.description = description;
|
||||
this.timestamp = DateTime.UtcNow;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,45 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using TeamHobby.HobbyProjectGenerator.DataAccessLayer;
|
||||
using TeamHobby.HobbyProjectGenerator.Logging.Contracts;
|
||||
using TeamHobby.HobbyProjectGenerator.Logging.Implementations;
|
||||
|
||||
|
||||
namespace TeamHobby.HobbyProjectGenerator.Logging
|
||||
{
|
||||
internal class LoggingManager
|
||||
{
|
||||
private readonly IDataSource<string> _conn;
|
||||
private readonly ILogger _logger;
|
||||
private readonly ILoggerFactory _factory;
|
||||
private readonly LogEntry _logEntry;
|
||||
|
||||
// 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)
|
||||
{
|
||||
_conn = dataSource;
|
||||
_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)
|
||||
{
|
||||
_conn = dataSource;
|
||||
_factory = factory;
|
||||
_logger = _factory.CreateLogger();
|
||||
|
||||
}
|
||||
|
||||
public void Process()
|
||||
{
|
||||
_logger.Log(_logEntry);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user