Compare commits

...
2 Commits
Author SHA1 Message Date
xrain c27011deea add logger 2021-06-28 05:36:54 +08:00
xrain b545af2365 add password oauth method for doc 2021-06-27 10:59:19 +08:00
4 changed files with 97 additions and 7 deletions
+1
View File
@@ -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();
+50
View File
@@ -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();
}
}
}
+22
View File
@@ -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();
}
}
}
+24 -7
View File
@@ -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
{
@@ -93,6 +95,14 @@ namespace SimApi
};
haveOauth = true;
break;
case "Password":
oauthFlows.Password = new OpenApiOAuthFlow
{
TokenUrl = new Uri(docOptions.ApiAuth.TokenUrl, UriKind.RelativeOrAbsolute),
Scopes = docOptions.ApiAuth.Scopes
};
haveOauth = true;
break;
}
}
@@ -160,27 +170,34 @@ namespace SimApi
public static IApplicationBuilder UseSimApi(this IApplicationBuilder builder)
{
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)
{
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<SimApiAuthMiddleware>();
}
if (options.EnableSimApiDoc)
{
SimApiUtil.Log("开始配置SimApiDoc...");
logger.LogInformation("开始配置SimApiDoc...");
var docOptions = options.SimApiDocOptions;
builder.UseSwagger(x => x.RouteTemplate = "/swagger/{documentName}.json").UseSwaggerUI(x =>
{
@@ -198,20 +215,20 @@ namespace SimApi
if (options.EnableSimApiException)
{
SimApiUtil.Log("开始配置SimApiException...");
logger.LogInformation("开始配置SimApiException...");
builder.UseMiddleware<SimApiExceptionMiddleware>();
}
//请求一下检测存储错误
if (options.EnableSimApiStorage)
{
SimApiUtil.Log("开始配置SimApiStorage...");
logger.LogInformation("开始配置SimApiStorage...");
builder.ApplicationServices.GetService<SimApiStorage>();
}
if (options.EnableLowerUrl)
{
SimApiUtil.Log("开始配置使用URL小写...");
logger.LogInformation("开始配置使用URL小写...");
}
return builder;