在微軟發佈的ASP.NET Core 中給了一種默認的日誌配置選擇,那麼當咱們使用其餘的日誌中間件是如何配置呢?以下:ios
一、使用NutGet 獲取日誌庫:Microsoft.Extensions.Logging.Log4Net.AspNetCore 和 Log4Net庫web
二、配置Program.csapp
public class Program { public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }) .ConfigureLogging((hosttingContext,logging)=> { logging.AddFilter("System", LogLevel.Warning); logging.AddFilter("Microsoft", LogLevel.Warning); logging.AddLog4Net(); }) ; }
三、配置Startup.cs文件佈局
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory) { 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(); } loggerFactory.AddLog4Net(); app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); }); }
三、新建配置文件:ui
<?xml version="1.0" encoding="utf-8" ?> <log4net> <appender name="DebugAppender" type="log4net.Appender.DebugAppender" > <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" /> </layout> </appender> <!--指定日記記錄方式,以滾動文件的方式(文件記錄)--> <appender name="RollingFile" type="log4net.Appender.RollingFileAppender"> <!--日誌路徑--> <file value="Log/" /> <!--是不是向文件中追加日誌--> <appendToFile value="true" /> <!--log保留天數--> <param name= "MaxSizeRollBackups" value= "10"/> <!--每一個文件最大1M--> <param name="maximumFileSize" value="1MB" /> <!--日誌根據日期滾動--> <param name="RollingStyle" value="Date" /> <!--日誌文件名格式爲:logs_20080831.log--> <param name="DatePattern" value=""logs_"yyyyMMdd".log"" /> <!--日誌文件名是不是固定不變的--> <param name="StaticLogFileName" value="false" /> <!--佈局--> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date %5level %logger.%method [%line] - MESSAGE: %message%newline %exception" /> </layout> </appender> <root> <level value="ALL"/> <appender-ref ref="DebugAppender" /> <appender-ref ref="RollingFile" /> </root> </log4net>