Serilog 实现破坏其他配置
Serilog implementaiton breaking other configuration
正在我们的项目中实施 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<>() 的错误是:
System.TypeLoadException occurred
HResult=0x80131522
Message=Method 'get_Name' in type 'Microsoft.Extensions.Options.ConfigurationChangeTokenSource`1' from assembly 'Microsoft.Extensions.Options.ConfigurationExtensions, Version=1.1.2.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' does not have an implementation.
Source=Microsoft.Extensions.Options.ConfigurationExtensions
StackTrace:
at Microsoft.Extensions.DependencyInjection.OptionsConfigurationServiceCollectionExtensions.Configure[TOptions](IServiceCollection services, IConfiguration config)
at NGB.IFS.PurchApp.Services.Startup.ConfigureServices(IServiceCollection services) in C:\Users\saynort\Documents\Repos\ngb.ifs.purchapp\ngb.ifs.purchapp\NGB.IFS.PurchApp.Services\Startup.cs:line 86
我安装了 serilog、serilog.extensions.logging 和 serilog.sinks.file Nuget 包。
来自Serilog.Extensions.Logging Github project:
ASP.NET Core 2.0 applications should prefer Serilog.AspNetCore and UseSerilog()
instead.
删除 Serilog 和 Serilog.Extensions.Logging 包。然后,使用
安装 Serilog.AspNetCore 包
PM> Install-Package Serilog.AspNetCore -DependencyVersion Highest
如果您需要使用 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();
正在我们的项目中实施 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<>() 的错误是:
System.TypeLoadException occurred HResult=0x80131522 Message=Method 'get_Name' in type 'Microsoft.Extensions.Options.ConfigurationChangeTokenSource`1' from assembly 'Microsoft.Extensions.Options.ConfigurationExtensions, Version=1.1.2.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' does not have an implementation. Source=Microsoft.Extensions.Options.ConfigurationExtensions StackTrace: at Microsoft.Extensions.DependencyInjection.OptionsConfigurationServiceCollectionExtensions.Configure[TOptions](IServiceCollection services, IConfiguration config) at NGB.IFS.PurchApp.Services.Startup.ConfigureServices(IServiceCollection services) in C:\Users\saynort\Documents\Repos\ngb.ifs.purchapp\ngb.ifs.purchapp\NGB.IFS.PurchApp.Services\Startup.cs:line 86
我安装了 serilog、serilog.extensions.logging 和 serilog.sinks.file Nuget 包。
来自Serilog.Extensions.Logging Github project:
ASP.NET Core 2.0 applications should prefer Serilog.AspNetCore and
UseSerilog()
instead.
删除 Serilog 和 Serilog.Extensions.Logging 包。然后,使用
安装 Serilog.AspNetCore 包PM> Install-Package Serilog.AspNetCore -DependencyVersion Highest
如果您需要使用 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();