描述:
B/S 我在前端发了一个Post请求,后端对应的Action 执行了两遍。
背景:
.net core 2.0.0 ,EF core 2.0.0 ,sdk 2.0.0 ,nuget 包对应的也是2.0.0 版本的。
相关代码:
Program.cs
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args)
{
return WebHost.CreateDefaultBuilder(args)
.ConfigureLogging((context, logging) =>
{
logging.AddSerilog();
})
.UseStartup<Startup>()
.Build();
}
Startup.cs
public Startup(IConfiguration configuration)
{
Configuration = configuration;
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(Configuration)
.WriteTo.RollingFile(Path.GetFullPath("logs/log-{Date}.txt"))
.CreateLogger();
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
//浏览器缓存
services.CacheServiceRegister();
services.MapperInit();
services.AddSingleton<IConfiguration>(Configuration);
//自己写的相关服务
services.ResiterService();
services.AddDbContextPool<DbContext>(option
=> option.UseSqlServer(Configuration.DbConnectionStr(),
o => o.UseRowNumberForPaging()));
//memcacheCore 缓存
services.AddEnyimMemcached(options => options.AddServer("memcached", 11211));
services.AddCnblogsAuthentication(Configuration);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env,
ILoggerFactory loggerFactory)
{
//http 重定向到https
var rewriteOptions = new RewriteOptions();
rewriteOptions.Rules.Add(new RedirectToProxiedHttpsRule());
app.UseRewriter(rewriteOptions);
app.UseEnyimMemcached();
app.UseCnblogsCookieAuthentication();
loggerFactory.AddSerilog();
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseCustomErrorPages();
}
app.UseStaticFiles();
///健康检查
app.UseHealthCheck();
//自定义Url相关代码
app.UrlRewrite();
}
出问题的Action
构造函数
public EnterpriseController( 其他的service
,ILogger<EnterpriseController> log) : base(context)
{
_log = log;
}
Action
[NeedLogin, HttpPost, ActionName("Save")]
public async Task<IActionResult> Save(EnterpriseInfoSaveModel enterpriseInfoSaveModel)
{
if(expr)
{
_log.LogError($"XXXXXXXX");
}
}
问题出现了: 日志中出现了两个错误记录(内容都是一样的):
2018-01-05 15:01:38.425 +09:00 [Error] XXXX
2018-01-05 15:01:38.430 +09:00 [Error] XXXX
看似一切都是正常的,就是不知道问题处在了哪里?
日志组件注册了两次。
两行代码注释掉就行了。
loggerFactory.AddSerilog();
loggerFactory.AddConsole(Configuration.GetSection("Logging"));