1 引用包:Microsoft.Extensions.Logging.Log4Net.AspNetCore
2 ILoggerFactory loggerFactory 注册
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory) { loggerFactory.AddLog4Net(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Home/Error"); // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. app.UseHsts(); } app.UseHttpsRedirection(); app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); }); }
3 配置log4net.config
4 项目实际使用
public class HomeController : Controller { private readonly ILogger
public HomeController(ILogger
public IActionResult Privacy() { _logger.LogError("这里记录的是LogError: 您在" + DateTime.Now.ToString()+"查看了Privacy" );
return View(); }
}
5 效果展示
手机扫一扫
移动阅读更方便
你可能感兴趣的文章