.NET CORE 3.1 MVC Log4net
阅读原文时间:2023年07月09日阅读:1

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
¨C26C ¨C27C ¨C28CBackups" value="100" /> ¨C29C ¨C30C ¨C31C ¨C32C ¨C33C ¨C34C ¨C35C
 
4  项目实际使用
public class HomeController : Controller { private readonly ILogger _logger;
public HomeController(ILogger logger) { _logger = logger; }
 
public IActionResult Privacy() { _logger.LogError("这里记录的是LogError: 您在" + DateTime.Now.ToString()+"查看了Privacy" );
return View(); }
}
5 效果展示