add logger
This commit is contained in:
@@ -11,6 +11,7 @@ namespace SimApi.Configs
|
|||||||
public bool EnableSimApiStorage { get; set; } = false;
|
public bool EnableSimApiStorage { get; set; } = false;
|
||||||
public bool EnableForwardHeaders { get; set; } = true;
|
public bool EnableForwardHeaders { get; set; } = true;
|
||||||
public bool EnableLowerUrl { get; set; } = true;
|
public bool EnableLowerUrl { get; set; } = true;
|
||||||
|
public bool EnableLogger { get; set; } = true;
|
||||||
public SimApiDocOptions SimApiDocOptions { get; set; } = new SimApiDocOptions();
|
public SimApiDocOptions SimApiDocOptions { get; set; } = new SimApiDocOptions();
|
||||||
public SimApiStorageOptions SimApiStorageOptions { get; set; } = new SimApiStorageOptions();
|
public SimApiStorageOptions SimApiStorageOptions { get; set; } = new SimApiStorageOptions();
|
||||||
|
|
||||||
|
|||||||
@@ -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>(TState state)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool IsEnabled(LogLevel logLevel)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception,
|
||||||
|
Func<TState, Exception, string> 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
using System.Collections.Concurrent;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
|
namespace SimApi.Logger
|
||||||
|
{
|
||||||
|
public class SimApiLoggerProvider : ILoggerProvider
|
||||||
|
{
|
||||||
|
private readonly ConcurrentDictionary<string, SimApiLogger> _loggers =
|
||||||
|
new ConcurrentDictionary<string, SimApiLogger>();
|
||||||
|
|
||||||
|
|
||||||
|
public ILogger CreateLogger(string categoryName)
|
||||||
|
{
|
||||||
|
return _loggers.GetOrAdd(categoryName, name => new SimApiLogger(name));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
_loggers.Clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+16
-7
@@ -5,7 +5,9 @@ using Microsoft.Extensions.DependencyInjection;
|
|||||||
using Microsoft.OpenApi.Models;
|
using Microsoft.OpenApi.Models;
|
||||||
using SimApi.Middlewares;
|
using SimApi.Middlewares;
|
||||||
using Microsoft.AspNetCore.HttpOverrides;
|
using Microsoft.AspNetCore.HttpOverrides;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
using SimApi.Configs;
|
using SimApi.Configs;
|
||||||
|
using SimApi.Logger;
|
||||||
|
|
||||||
namespace SimApi
|
namespace SimApi
|
||||||
{
|
{
|
||||||
@@ -168,27 +170,34 @@ namespace SimApi
|
|||||||
public static IApplicationBuilder UseSimApi(this IApplicationBuilder builder)
|
public static IApplicationBuilder UseSimApi(this IApplicationBuilder builder)
|
||||||
{
|
{
|
||||||
var options = builder.ApplicationServices.GetService<SimApiOptions>();
|
var options = builder.ApplicationServices.GetService<SimApiOptions>();
|
||||||
|
if (options.EnableLogger)
|
||||||
|
{
|
||||||
|
builder.ApplicationServices.GetService<ILoggerFactory>().AddProvider(new SimApiLoggerProvider());
|
||||||
|
}
|
||||||
|
|
||||||
|
var logger = builder.ApplicationServices.GetService<ILogger>();
|
||||||
|
|
||||||
if (options.EnableForwardHeaders)
|
if (options.EnableForwardHeaders)
|
||||||
{
|
{
|
||||||
SimApiUtil.Log("开始配置ForwardedHeaders...");
|
logger.LogInformation("开始配置ForwardedHeaders...");
|
||||||
builder.UseForwardedHeaders();
|
builder.UseForwardedHeaders();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.EnableCors)
|
if (options.EnableCors)
|
||||||
{
|
{
|
||||||
SimApiUtil.Log("开始配置Cors全部允许...");
|
logger.LogInformation("开始配置Cors全部允许...");
|
||||||
builder.UseCors("any");
|
builder.UseCors("any");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.EnableSimApiAuth)
|
if (options.EnableSimApiAuth)
|
||||||
{
|
{
|
||||||
SimApiUtil.Log("开始配置SimApiAuth...");
|
logger.LogInformation("开始配置SimApiAuth...");
|
||||||
builder.UseMiddleware<SimApiAuthMiddleware>();
|
builder.UseMiddleware<SimApiAuthMiddleware>();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.EnableSimApiDoc)
|
if (options.EnableSimApiDoc)
|
||||||
{
|
{
|
||||||
SimApiUtil.Log("开始配置SimApiDoc...");
|
logger.LogInformation("开始配置SimApiDoc...");
|
||||||
var docOptions = options.SimApiDocOptions;
|
var docOptions = options.SimApiDocOptions;
|
||||||
builder.UseSwagger(x => x.RouteTemplate = "/swagger/{documentName}.json").UseSwaggerUI(x =>
|
builder.UseSwagger(x => x.RouteTemplate = "/swagger/{documentName}.json").UseSwaggerUI(x =>
|
||||||
{
|
{
|
||||||
@@ -206,20 +215,20 @@ namespace SimApi
|
|||||||
|
|
||||||
if (options.EnableSimApiException)
|
if (options.EnableSimApiException)
|
||||||
{
|
{
|
||||||
SimApiUtil.Log("开始配置SimApiException...");
|
logger.LogInformation("开始配置SimApiException...");
|
||||||
builder.UseMiddleware<SimApiExceptionMiddleware>();
|
builder.UseMiddleware<SimApiExceptionMiddleware>();
|
||||||
}
|
}
|
||||||
|
|
||||||
//请求一下检测存储错误
|
//请求一下检测存储错误
|
||||||
if (options.EnableSimApiStorage)
|
if (options.EnableSimApiStorage)
|
||||||
{
|
{
|
||||||
SimApiUtil.Log("开始配置SimApiStorage...");
|
logger.LogInformation("开始配置SimApiStorage...");
|
||||||
builder.ApplicationServices.GetService<SimApiStorage>();
|
builder.ApplicationServices.GetService<SimApiStorage>();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.EnableLowerUrl)
|
if (options.EnableLowerUrl)
|
||||||
{
|
{
|
||||||
SimApiUtil.Log("开始配置使用URL小写...");
|
logger.LogInformation("开始配置使用URL小写...");
|
||||||
}
|
}
|
||||||
|
|
||||||
return builder;
|
return builder;
|
||||||
|
|||||||
Reference in New Issue
Block a user