mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-07-25 06:31:30 +00:00
Basic ASP.NET -> Kettu logger
This commit is contained in:
parent
581e6bac2a
commit
fe03663f38
9 changed files with 155 additions and 7 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -7,4 +7,5 @@ riderModule.iml
|
||||||
/.idea/.idea.ProjectLighthouse/.idea/dataSources/
|
/.idea/.idea.ProjectLighthouse/.idea/dataSources/
|
||||||
/.idea/.idea.ProjectLighthouse/.idea/dataSources.local.xml
|
/.idea/.idea.ProjectLighthouse/.idea/dataSources.local.xml
|
||||||
*.sln.DotSettings.user
|
*.sln.DotSettings.user
|
||||||
/ProjectLighthouse/r/*
|
/ProjectLighthouse/r/*
|
||||||
|
/ProjectLighthouse/logs/*
|
|
@ -7,6 +7,7 @@
|
||||||
<s:Boolean x:Key="/Default/UserDictionary/Words/=brun/@EntryIndexedValue">True</s:Boolean>
|
<s:Boolean x:Key="/Default/UserDictionary/Words/=brun/@EntryIndexedValue">True</s:Boolean>
|
||||||
<s:Boolean x:Key="/Default/UserDictionary/Words/=ezoiar/@EntryIndexedValue">True</s:Boolean>
|
<s:Boolean x:Key="/Default/UserDictionary/Words/=ezoiar/@EntryIndexedValue">True</s:Boolean>
|
||||||
<s:Boolean x:Key="/Default/UserDictionary/Words/=farc/@EntryIndexedValue">True</s:Boolean>
|
<s:Boolean x:Key="/Default/UserDictionary/Words/=farc/@EntryIndexedValue">True</s:Boolean>
|
||||||
|
<s:Boolean x:Key="/Default/UserDictionary/Words/=Kettu/@EntryIndexedValue">True</s:Boolean>
|
||||||
<s:Boolean x:Key="/Default/UserDictionary/Words/=lbpme/@EntryIndexedValue">True</s:Boolean>
|
<s:Boolean x:Key="/Default/UserDictionary/Words/=lbpme/@EntryIndexedValue">True</s:Boolean>
|
||||||
<s:Boolean x:Key="/Default/UserDictionary/Words/=lolcatftw/@EntryIndexedValue">True</s:Boolean>
|
<s:Boolean x:Key="/Default/UserDictionary/Words/=lolcatftw/@EntryIndexedValue">True</s:Boolean>
|
||||||
<s:Boolean x:Key="/Default/UserDictionary/Words/=Swingy/@EntryIndexedValue">True</s:Boolean>
|
<s:Boolean x:Key="/Default/UserDictionary/Words/=Swingy/@EntryIndexedValue">True</s:Boolean>
|
||||||
|
|
29
ProjectLighthouse/Logging/AspNetToKettuLogger.cs
Normal file
29
ProjectLighthouse/Logging/AspNetToKettuLogger.cs
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
using System;
|
||||||
|
using Kettu;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
|
namespace LBPUnion.ProjectLighthouse.Logging {
|
||||||
|
public class AspNetToKettuLogger : ILogger {
|
||||||
|
|
||||||
|
public IDisposable BeginScope<TState>(TState state) {
|
||||||
|
return NullScope.Instance;
|
||||||
|
}
|
||||||
|
public bool IsEnabled(LogLevel logLevel) => true;
|
||||||
|
|
||||||
|
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
15
ProjectLighthouse/Logging/AspNetToKettuLoggerProvider.cs
Normal file
15
ProjectLighthouse/Logging/AspNetToKettuLoggerProvider.cs
Normal file
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
17
ProjectLighthouse/Logging/LighthouseFileLogger.cs
Normal file
17
ProjectLighthouse/Logging/LighthouseFileLogger.cs
Normal file
|
@ -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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
50
ProjectLighthouse/Logging/LoggerLevels.cs
Normal file
50
ProjectLighthouse/Logging/LoggerLevels.cs
Normal file
|
@ -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
|
||||||
|
}
|
11
ProjectLighthouse/Logging/NullScope.cs
Normal file
11
ProjectLighthouse/Logging/NullScope.cs
Normal file
|
@ -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() {}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,34 +1,49 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
|
using Kettu;
|
||||||
|
using LBPUnion.ProjectLighthouse.Logging;
|
||||||
using LBPUnion.ProjectLighthouse.Types.Settings;
|
using LBPUnion.ProjectLighthouse.Types.Settings;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Microsoft.Extensions.Logging.Console;
|
||||||
|
|
||||||
namespace LBPUnion.ProjectLighthouse {
|
namespace LBPUnion.ProjectLighthouse {
|
||||||
public static class Program {
|
public static class Program {
|
||||||
public static void Main(string[] args) {
|
public static void Main(string[] args) {
|
||||||
|
// Log startup time
|
||||||
Stopwatch startupStopwatch = new();
|
Stopwatch startupStopwatch = new();
|
||||||
startupStopwatch.Start();
|
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;
|
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) {
|
if(dbConnected) {
|
||||||
Stopwatch migrationStopwatch = new();
|
Stopwatch migrationStopwatch = new();
|
||||||
migrationStopwatch.Start();
|
migrationStopwatch.Start();
|
||||||
|
|
||||||
Console.WriteLine("Migrating database...");
|
Logger.Log("Migrating database...", LoggerLevelDatabase.Instance);
|
||||||
using Database database = new();
|
using Database database = new();
|
||||||
database.Database.Migrate();
|
database.Database.Migrate();
|
||||||
|
|
||||||
migrationStopwatch.Stop();
|
migrationStopwatch.Stop();
|
||||||
Console.WriteLine($"Migration took {migrationStopwatch.ElapsedMilliseconds}ms.");
|
Logger.Log($"Migration took {migrationStopwatch.ElapsedMilliseconds}ms.", LoggerLevelDatabase.Instance);
|
||||||
} else Environment.Exit(1);
|
} else Environment.Exit(1);
|
||||||
|
|
||||||
startupStopwatch.Stop();
|
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();
|
CreateHostBuilder(args).Build().Run();
|
||||||
}
|
}
|
||||||
|
@ -37,6 +52,10 @@ namespace LBPUnion.ProjectLighthouse {
|
||||||
Host.CreateDefaultBuilder(args)
|
Host.CreateDefaultBuilder(args)
|
||||||
.ConfigureWebHostDefaults(webBuilder => {
|
.ConfigureWebHostDefaults(webBuilder => {
|
||||||
webBuilder.UseStartup<Startup>();
|
webBuilder.UseStartup<Startup>();
|
||||||
|
})
|
||||||
|
.ConfigureLogging(logging => {
|
||||||
|
logging.ClearProviders();
|
||||||
|
logging.Services.TryAddEnumerable(ServiceDescriptor.Singleton<ILoggerProvider, AspNetToKettuLoggerProvider>());
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -9,6 +9,7 @@
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="BCrypt.Net-Next" Version="4.0.2" />
|
<PackageReference Include="BCrypt.Net-Next" Version="4.0.2" />
|
||||||
|
<PackageReference Include="Kettu" Version="1.0.2" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="5.0.11" />
|
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="5.0.11" />
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.11" />
|
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.11" />
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.11">
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.11">
|
||||||
|
@ -22,4 +23,8 @@
|
||||||
<Compile Remove="Types\SlotXsd.cs" />
|
<Compile Remove="Types\SlotXsd.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<Folder Include="logs" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue