From c27011deeae63e4a7e1df843f0987c7d8a25e54a Mon Sep 17 00:00:00 2001 From: xRain Date: Mon, 28 Jun 2021 05:36:54 +0800 Subject: [PATCH] add logger --- Configurations/SimApiOptions.cs | 1 + Logger/SimApiLogger.cs | 50 +++++++++++++++++++++++++++++++++ Logger/SimApiLoggerProvider.cs | 22 +++++++++++++++ SimApiExtensions.cs | 23 ++++++++++----- 4 files changed, 89 insertions(+), 7 deletions(-) create mode 100644 Logger/SimApiLogger.cs create mode 100644 Logger/SimApiLoggerProvider.cs diff --git a/Configurations/SimApiOptions.cs b/Configurations/SimApiOptions.cs index 1cbd937..c003dee 100644 --- a/Configurations/SimApiOptions.cs +++ b/Configurations/SimApiOptions.cs @@ -11,6 +11,7 @@ namespace SimApi.Configs public bool EnableSimApiStorage { get; set; } = false; public bool EnableForwardHeaders { get; set; } = true; public bool EnableLowerUrl { get; set; } = true; + public bool EnableLogger { get; set; } = true; public SimApiDocOptions SimApiDocOptions { get; set; } = new SimApiDocOptions(); public SimApiStorageOptions SimApiStorageOptions { get; set; } = new SimApiStorageOptions(); diff --git a/Logger/SimApiLogger.cs b/Logger/SimApiLogger.cs new file mode 100644 index 0000000..0dc2772 --- /dev/null +++ b/Logger/SimApiLogger.cs @@ -0,0 +1,50 @@ +using System; +using Microsoft.Extensions.Logging; + +namespace SimApi.Logger +{ + public class SimApiLogger : ILogger + { + private string Name { get; } + + public SimApiLogger(string name) + { + Name = name; + } + + public IDisposable BeginScope(TState state) + { + return null; + } + + public bool IsEnabled(LogLevel logLevel) + { + return true; + } + + public void Log(LogLevel logLevel, EventId eventId, TState state, Exception exception, + Func formatter) + { + var defautColor = Console.ForegroundColor; + Console.ForegroundColor = logLevel switch + { + LogLevel.Debug => ConsoleColor.DarkMagenta, + LogLevel.Information => ConsoleColor.DarkCyan, + LogLevel.Warning => ConsoleColor.Yellow, + LogLevel.Error => ConsoleColor.Red, + LogLevel.Critical => ConsoleColor.DarkRed, + _ => defautColor + }; + Console.WriteLine( + $"[ {Name} ][ {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff")} ][ {logLevel.ToString()} ]"); + Console.ForegroundColor = defautColor; + Console.WriteLine($"{state}"); + if (exception != null) + { + Console.WriteLine($"{exception}"); + } + + Console.WriteLine(); + } + } +} \ No newline at end of file diff --git a/Logger/SimApiLoggerProvider.cs b/Logger/SimApiLoggerProvider.cs new file mode 100644 index 0000000..48b0a7d --- /dev/null +++ b/Logger/SimApiLoggerProvider.cs @@ -0,0 +1,22 @@ +using System.Collections.Concurrent; +using Microsoft.Extensions.Logging; + +namespace SimApi.Logger +{ + public class SimApiLoggerProvider : ILoggerProvider + { + private readonly ConcurrentDictionary _loggers = + new ConcurrentDictionary(); + + + public ILogger CreateLogger(string categoryName) + { + return _loggers.GetOrAdd(categoryName, name => new SimApiLogger(name)); + } + + public void Dispose() + { + _loggers.Clear(); + } + } +} \ No newline at end of file diff --git a/SimApiExtensions.cs b/SimApiExtensions.cs index cdd07c5..22b5803 100644 --- a/SimApiExtensions.cs +++ b/SimApiExtensions.cs @@ -5,7 +5,9 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.OpenApi.Models; using SimApi.Middlewares; using Microsoft.AspNetCore.HttpOverrides; +using Microsoft.Extensions.Logging; using SimApi.Configs; +using SimApi.Logger; namespace SimApi { @@ -168,27 +170,34 @@ namespace SimApi public static IApplicationBuilder UseSimApi(this IApplicationBuilder builder) { var options = builder.ApplicationServices.GetService(); + if (options.EnableLogger) + { + builder.ApplicationServices.GetService().AddProvider(new SimApiLoggerProvider()); + } + + var logger = builder.ApplicationServices.GetService(); + if (options.EnableForwardHeaders) { - SimApiUtil.Log("开始配置ForwardedHeaders..."); + logger.LogInformation("开始配置ForwardedHeaders..."); builder.UseForwardedHeaders(); } if (options.EnableCors) { - SimApiUtil.Log("开始配置Cors全部允许..."); + logger.LogInformation("开始配置Cors全部允许..."); builder.UseCors("any"); } if (options.EnableSimApiAuth) { - SimApiUtil.Log("开始配置SimApiAuth..."); + logger.LogInformation("开始配置SimApiAuth..."); builder.UseMiddleware(); } if (options.EnableSimApiDoc) { - SimApiUtil.Log("开始配置SimApiDoc..."); + logger.LogInformation("开始配置SimApiDoc..."); var docOptions = options.SimApiDocOptions; builder.UseSwagger(x => x.RouteTemplate = "/swagger/{documentName}.json").UseSwaggerUI(x => { @@ -206,20 +215,20 @@ namespace SimApi if (options.EnableSimApiException) { - SimApiUtil.Log("开始配置SimApiException..."); + logger.LogInformation("开始配置SimApiException..."); builder.UseMiddleware(); } //请求一下检测存储错误 if (options.EnableSimApiStorage) { - SimApiUtil.Log("开始配置SimApiStorage..."); + logger.LogInformation("开始配置SimApiStorage..."); builder.ApplicationServices.GetService(); } if (options.EnableLowerUrl) { - SimApiUtil.Log("开始配置使用URL小写..."); + logger.LogInformation("开始配置使用URL小写..."); } return builder;