diff --git a/.gitignore b/.gitignore index 6ec59b85..d6a197ac 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,5 @@ riderModule.iml /.idea/.idea.ProjectLighthouse/.idea/dataSources/ /.idea/.idea.ProjectLighthouse/.idea/dataSources.local.xml *.sln.DotSettings.user -/ProjectLighthouse/r/* \ No newline at end of file +/ProjectLighthouse/r/* +/ProjectLighthouse/logs/* \ No newline at end of file diff --git a/ProjectLighthouse.sln.DotSettings b/ProjectLighthouse.sln.DotSettings index f6af5eef..07522038 100644 --- a/ProjectLighthouse.sln.DotSettings +++ b/ProjectLighthouse.sln.DotSettings @@ -7,6 +7,7 @@ True True True + True True True True diff --git a/ProjectLighthouse/Logging/AspNetToKettuLogger.cs b/ProjectLighthouse/Logging/AspNetToKettuLogger.cs new file mode 100644 index 00000000..c92e8bb4 --- /dev/null +++ b/ProjectLighthouse/Logging/AspNetToKettuLogger.cs @@ -0,0 +1,29 @@ +using System; +using Kettu; +using Microsoft.Extensions.Logging; + +namespace LBPUnion.ProjectLighthouse.Logging { + public class AspNetToKettuLogger : ILogger { + + public IDisposable BeginScope(TState state) { + return NullScope.Instance; + } + public bool IsEnabled(LogLevel logLevel) => true; + + public void Log(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func formatter) { + LoggerLevel loggerLevel = logLevel switch { + + LogLevel.Trace => LoggerLevelAspNetTrace.Instance, + LogLevel.Debug => LoggerLevelAspNetDebug.Instance, + LogLevel.Information => LoggerLevelAspNetInformation.Instance, + LogLevel.Warning => LoggerLevelAspNetWarning.Instance, + LogLevel.Error => LoggerLevelAspNetError.Instance, + LogLevel.Critical => LoggerLevelAspNetCritical.Instance, + LogLevel.None => LoggerLevelAspNetNone.Instance, + _ => throw new ArgumentOutOfRangeException(nameof(logLevel), logLevel, null) + }; + + Logger.Log(state.ToString(), loggerLevel); + } + } +} \ No newline at end of file diff --git a/ProjectLighthouse/Logging/AspNetToKettuLoggerProvider.cs b/ProjectLighthouse/Logging/AspNetToKettuLoggerProvider.cs new file mode 100644 index 00000000..0766cde9 --- /dev/null +++ b/ProjectLighthouse/Logging/AspNetToKettuLoggerProvider.cs @@ -0,0 +1,15 @@ +using System; +using Microsoft.Extensions.Logging; + +namespace LBPUnion.ProjectLighthouse.Logging { + [ProviderAlias("Kettu")] + public class AspNetToKettuLoggerProvider : ILoggerProvider, IDisposable { + public void Dispose() { + // cry about it + } + + public ILogger CreateLogger(string categoryName) { + return new AspNetToKettuLogger(); + } + } +} \ No newline at end of file diff --git a/ProjectLighthouse/Logging/LighthouseFileLogger.cs b/ProjectLighthouse/Logging/LighthouseFileLogger.cs new file mode 100644 index 00000000..a19b4361 --- /dev/null +++ b/ProjectLighthouse/Logging/LighthouseFileLogger.cs @@ -0,0 +1,17 @@ +using System; +using System.IO; +using Kettu; +using LBPUnion.ProjectLighthouse.Helpers; + +namespace LBPUnion.ProjectLighthouse.Logging { + public class LighthouseFileLogger : LoggerBase { + private static readonly string logsDirectory = Path.Combine(Environment.CurrentDirectory, "logs"); + + public override void Send(LoggerLine line) { + FileHelper.EnsureDirectoryCreated(logsDirectory); + + File.AppendAllText(Path.Combine(logsDirectory, line.LoggerLevel + ".log"), line + "\n"); + File.AppendAllText(Path.Combine(logsDirectory, "all.log"), line.ToString()); + } + } +} \ No newline at end of file diff --git a/ProjectLighthouse/Logging/LoggerLevels.cs b/ProjectLighthouse/Logging/LoggerLevels.cs new file mode 100644 index 00000000..5bb26a93 --- /dev/null +++ b/ProjectLighthouse/Logging/LoggerLevels.cs @@ -0,0 +1,50 @@ +using Kettu; + +namespace LBPUnion.ProjectLighthouse.Logging { + public class LoggerLevelStartup : LoggerLevel { + public override string Name => "Startup"; + public static LoggerLevelStartup Instance = new(); + } + + public class LoggerLevelDatabase : LoggerLevel { + public override string Name => "Database"; + public static LoggerLevelDatabase Instance = new(); + } + + #region ASP.NET + public class LoggerLevelAspNetTrace : LoggerLevel { + public override string Name => "ASP.NET: Trace"; + public static LoggerLevelAspNetTrace Instance = new(); + } + + public class LoggerLevelAspNetDebug : LoggerLevel { + public override string Name => "ASP.NET: Debug"; + public static LoggerLevelAspNetDebug Instance = new(); + } + + public class LoggerLevelAspNetInformation : LoggerLevel { + public override string Name => "ASP.NET: Information"; + public static LoggerLevelAspNetInformation Instance = new(); + } + + public class LoggerLevelAspNetWarning : LoggerLevel { + public override string Name => "ASP.NET: Warning"; + public static LoggerLevelAspNetWarning Instance = new(); + } + + public class LoggerLevelAspNetError : LoggerLevel { + public override string Name => "ASP.NET: Error"; + public static LoggerLevelAspNetError Instance = new(); + } + + public class LoggerLevelAspNetCritical : LoggerLevel { + public override string Name => "ASP.NET: Critical"; + public static LoggerLevelAspNetCritical Instance = new(); + } + + public class LoggerLevelAspNetNone : LoggerLevel { + public override string Name => "ASP.NET: None"; + public static LoggerLevelAspNetNone Instance = new(); + } + #endregion +} \ No newline at end of file diff --git a/ProjectLighthouse/Logging/NullScope.cs b/ProjectLighthouse/Logging/NullScope.cs new file mode 100644 index 00000000..2df62c0f --- /dev/null +++ b/ProjectLighthouse/Logging/NullScope.cs @@ -0,0 +1,11 @@ +using System; + +namespace LBPUnion.ProjectLighthouse.Logging { + public class NullScope : IDisposable{ + public static NullScope Instance { get; } = new(); + + private NullScope() {} + + public void Dispose() {} + } +} \ No newline at end of file diff --git a/ProjectLighthouse/Program.cs b/ProjectLighthouse/Program.cs index 89ddb2f2..70b8708f 100644 --- a/ProjectLighthouse/Program.cs +++ b/ProjectLighthouse/Program.cs @@ -1,34 +1,49 @@ using System; using System.Diagnostics; +using Kettu; +using LBPUnion.ProjectLighthouse.Logging; using LBPUnion.ProjectLighthouse.Types.Settings; using Microsoft.AspNetCore.Hosting; using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Console; namespace LBPUnion.ProjectLighthouse { public static class Program { public static void Main(string[] args) { + // Log startup time Stopwatch startupStopwatch = new(); startupStopwatch.Start(); - Console.WriteLine("Welcome to Project Lighthouse!"); - Console.WriteLine("Determining if the database is available..."); + + // Setup logging + + Logger.StartLogging(); + LoggerLine.LogFormat = "[{0}] {1}"; + Logger.AddLogger(new ConsoleLogger()); + Logger.AddLogger(new LighthouseFileLogger()); + + Logger.Log("Welcome to Project Lighthouse!", LoggerLevelStartup.Instance); + Logger.Log("Determining if the database is available...", LoggerLevelStartup.Instance); bool dbConnected = ServerSettings.DbConnected; - Console.WriteLine(dbConnected ? "Connected to the database." : "Database unavailable! Exiting."); + Logger.Log(dbConnected ? "Connected to the database." : "Database unavailable! Exiting.", LoggerLevelStartup.Instance); if(dbConnected) { Stopwatch migrationStopwatch = new(); migrationStopwatch.Start(); - Console.WriteLine("Migrating database..."); + Logger.Log("Migrating database...", LoggerLevelDatabase.Instance); using Database database = new(); database.Database.Migrate(); migrationStopwatch.Stop(); - Console.WriteLine($"Migration took {migrationStopwatch.ElapsedMilliseconds}ms."); + Logger.Log($"Migration took {migrationStopwatch.ElapsedMilliseconds}ms.", LoggerLevelDatabase.Instance); } else Environment.Exit(1); startupStopwatch.Stop(); - Console.WriteLine($"Ready! Startup took {startupStopwatch.ElapsedMilliseconds}ms. Passing off control to ASP.NET..."); + Logger.Log($"Ready! Startup took {startupStopwatch.ElapsedMilliseconds}ms. Passing off control to ASP.NET...", LoggerLevelStartup.Instance); CreateHostBuilder(args).Build().Run(); } @@ -37,6 +52,10 @@ namespace LBPUnion.ProjectLighthouse { Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup(); + }) + .ConfigureLogging(logging => { + logging.ClearProviders(); + logging.Services.TryAddEnumerable(ServiceDescriptor.Singleton()); }); } } \ No newline at end of file diff --git a/ProjectLighthouse/ProjectLighthouse.csproj b/ProjectLighthouse/ProjectLighthouse.csproj index 59b5059c..0fccd949 100644 --- a/ProjectLighthouse/ProjectLighthouse.csproj +++ b/ProjectLighthouse/ProjectLighthouse.csproj @@ -9,6 +9,7 @@ + @@ -22,4 +23,8 @@ + + + +