From 32e444d7e8fb7a0cdcf0766a497dc2cf463249cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=8B=E9=9B=A8=E8=90=BD?= Date: Mon, 11 Sep 2023 15:17:47 +0800 Subject: [PATCH] :tada: Init. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 秋雨落 --- CrystalConnector/CrystalConnector.csproj | 21 ++++++++++- CrystalConnector/CrystalService.cs | 48 ++++++++++++++++++++++++ CrystalConnector/NLog.config | 34 +++++++++++++++++ CrystalConnector/Program.cs | 28 +++++++++++++- CrystalConnector/config.json | 2 + 5 files changed, 131 insertions(+), 2 deletions(-) create mode 100644 CrystalConnector/CrystalService.cs create mode 100644 CrystalConnector/NLog.config create mode 100644 CrystalConnector/config.json diff --git a/CrystalConnector/CrystalConnector.csproj b/CrystalConnector/CrystalConnector.csproj index 8b92b45..09b702b 100644 --- a/CrystalConnector/CrystalConnector.csproj +++ b/CrystalConnector/CrystalConnector.csproj @@ -8,7 +8,26 @@ - + + + + + + + + + + + + + PreserveNewest + + + PreserveNewest + + + + diff --git a/CrystalConnector/CrystalService.cs b/CrystalConnector/CrystalService.cs new file mode 100644 index 0000000..edeb159 --- /dev/null +++ b/CrystalConnector/CrystalService.cs @@ -0,0 +1,48 @@ +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using NLog; + +namespace CrystalConnector; + +public class CrystalService : IHostedService +{ + private ILogger Logger { get; } + private IConfiguration Config { get; } + + public CrystalService(ILogger logger, IConfiguration config, IHostApplicationLifetime appLifetime) + { + Logger = logger; + Config = config; + + appLifetime.ApplicationStarted.Register(OnStarted); + appLifetime.ApplicationStopping.Register(OnStopping); + appLifetime.ApplicationStopped.Register(OnStopped); + } + + private void OnStarted() + { + Logger.LogInformation("Starting!"); + } + + private void OnStopping() + { + Logger.LogInformation("Stopping!"); + } + + private void OnStopped() + { + } + + public Task StartAsync(CancellationToken cancellationToken) + { + return Task.CompletedTask; + } + + public Task StopAsync(CancellationToken cancellationToken) + { + LogManager.Shutdown(); + + return Task.CompletedTask; + } +} \ No newline at end of file diff --git a/CrystalConnector/NLog.config b/CrystalConnector/NLog.config new file mode 100644 index 0000000..b5594d1 --- /dev/null +++ b/CrystalConnector/NLog.config @@ -0,0 +1,34 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/CrystalConnector/Program.cs b/CrystalConnector/Program.cs index bac24da..b16dc49 100644 --- a/CrystalConnector/Program.cs +++ b/CrystalConnector/Program.cs @@ -1 +1,27 @@ -Console.WriteLine("Hello, world!"); +using CrystalConnector; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using NLog.Extensions.Logging; +using LogLevel = Microsoft.Extensions.Logging.LogLevel; + +var builder = Host.CreateApplicationBuilder(args); + +builder.Services.AddHostedService(); + +// builder.Services.AddSingleton(); + +builder.Environment.ContentRootPath = Directory.GetCurrentDirectory(); +builder.Configuration.AddJsonFile("config.json", true) + .AddJsonFile($"config.{builder.Environment.EnvironmentName}.json") + .AddEnvironmentVariables(prefix: "CRYSTAL_") + .AddCommandLine(args); + +builder.Logging.ClearProviders() + .SetMinimumLevel(LogLevel.Trace) + .AddNLog(); + +using var host = builder.Build(); + +await host.RunAsync(); diff --git a/CrystalConnector/config.json b/CrystalConnector/config.json new file mode 100644 index 0000000..7a73a41 --- /dev/null +++ b/CrystalConnector/config.json @@ -0,0 +1,2 @@ +{ +} \ No newline at end of file