在我们的项目中实施serilog。我们已经实现了AspNetCoreRateLimit来解决对API的调用。该项目是一个.Net Core项目,但由于存在一些依赖性,因此构建了Net461。
在serilog之前,api很好。
现在安装了serilog,我们收到了错误。
错误从调用此错误起泡,之前一直有效,直到引入了serilog。
services.Configure<IpRateLimitOptions>(_configuration.GetSection("IpRateLimiting"));
Startup()有:
Log.Logger = new LoggerConfiguration()
.WriteTo.File("Logs/FSCPAPI-{Date}.log")
.CreateLogger();
以下是在Configure()
中loggerfactory.AddSerilog();
来自services.Configure&lt;&gt;()的错误是:
发生了System.TypeLoadException 的HResult = 0x80131522 Message =方法'get_Name',类型为'Microsoft.Extensions.Options.ConfigurationChangeTokenSource`1',来自程序集'Microsoft.Extensions.Options.ConfigurationExtensions,Version = 1.1.2.0,Culture = neutral,PublicKeyToken = adb9793829ddae60'没有实现。 来源= Microsoft.Extensions.Options.ConfigurationExtensions 堆栈跟踪: at Microsoft.Extensions.DependencyInjection.OptionsConfigurationServiceCollectionExtensions.Configure [TOptions](IServiceCollection services,IConfiguration config) 在C:\ Users \ saynort \ Documents \ Repos \ ngb.ifs.purchapp \ ngb.ifs.purchapp \ NGB.IFS.PurchApp.Services \ Startup中的NGB.IFS.PurchApp.Services.Startup.ConfigureServices(IServiceCollection服务)。 cs:第86行
我安装了serilog,serilog.extensions.logging和serilog.sinks.file Nuget软件包。
答案 0 :(得分:5)
来自Serilog.Extensions.Logging Github project:
ASP.NET Core 2.0 应用程序应该更喜欢Serilog.AspNetCore和
UseSerilog()
。
删除Serilog和Serilog.Extensions.Logging包。然后,使用以下命令安装Serilog.AspNetCore包
PM> Install-Package Serilog.AspNetCore -DependencyVersion Highest
答案 1 :(得分:0)
如果您需要使用AspNetCoreRateLimit实现serilog的示例,我可以重写并使其正常工作。
public static class RateLimitSerilogExtensions
{
public static IApplicationBuilder UseCustomIpRateLimiting(this IApplicationBuilder builder)
{
return builder.UseMiddleware<IpRateLimitMiddlewareCustom>();
}
}
public class IpRateLimitMiddlewareCustom : IpRateLimitMiddleware
{
private readonly Microsoft.Extensions.Logging.ILogger _logger;
private readonly IHostEnvironment _env;
private readonly ILogger _serilogger;
public IpRateLimitMiddlewareCustom(RequestDelegate next, IOptions<IpRateLimitOptions> options, IRateLimitCounterStore counterStore, IIpPolicyStore policyStore, Microsoft.Extensions.Logging.ILogger<IpRateLimitMiddleware> logger, IRateLimitConfiguration config,
IHostEnvironment env, ILogger serLog) : base(next, options, counterStore, policyStore, config, logger)
{
_logger = logger;
_serilogger = serLog;
_env = env;
}
protected override void LogBlockedRequest(HttpContext httpContext, ClientRequestIdentity identity, RateLimitCounter counter, RateLimitRule rule)
{
_serilogger
.ForContext("Blocked by rule", rule.Endpoint)
.ForContext("TraceIdentifier", httpContext.TraceIdentifier)
.ForContext("Quota", rule.Limit + "/" + rule.Period)
.ForContext("Exceeded By", counter.Count)
.Information("EService limit reached");
}
}
在启动时,必须先调用此方法,然后再使用nuget软件包随附的方法
app.UseCustomIpRateLimiting();
app.UseIpRateLimiting();