.NET CORE 3.1 MVC Log4net
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
<log4net>
<root>
<level value="ALL" ref="infoAppender" />
<appender-ref ref="infoAppender" />
<appender-ref ref="errorAppender" />
<appender-ref ref="debugAppender" />
</root>
<!-- 日志的等级,它们由高到底分别为: OFF > FATAL > ERROR > WARN > INFO > DEBUG > ALL -->
<!--信息日志配置-->
<!--<appender name="infoAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="../../../Logs/Info/info.log" />
<param name="AppendToFile" value="false" />
<param name="MaxFileSize" value="10240" />
<param name="MaxSizeRollBackups" value="100" />
<param name="PreserveLogFileNameExtension" value="true" />
<param name="StaticLogFileName" value="false" />
<param name="DatePattern" value="yyyyMMdd" />
<param name="RollingStyle" value="Date" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="OFF" />
<param name="LevelMax" value="OFF" />
</filter>
</appender>-->
<!--调试日志配置-->
<!--<appender name="debugAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="../../../Logs/Debug/debug.log" />
<param name="AppendToFile" value="false" />
<param name="MaxFileSize" value="10240" />
<param name="MaxSizeRollBackups" value="100" />
<param name="PreserveLogFileNameExtension" value="true" />
<param name="StaticLogFileName" value="false" />
<param name="DatePattern" value="yyyyMMdd" />
<param name="RollingStyle" value="Date" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="OFF" />
<param name="LevelMax" value="OFF" />
</filter>
</appender>-->
<!--错误日志配置-->
<appender name="errorAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="../../../Logs/Error/Err.log" />
<param name="AppendToFile" value="true" />
<param name="MaxFileSize" value="10240" />
Backups" value="100" />
<param name="PreserveLogFileNameExtension" value="true" />
<param name="StaticLogFileName" value="false" />
<param name="DatePattern" value="yyyyMMdd" />
<param name="RollingStyle" value="Date" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<param name="LevelMin" value="ERROR" />
<param name="LevelMax" value="ERROR" />
</filter>
</appender>
</log4net>
4 项目实际使用
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
public IActionResult Privacy()
{
_logger.LogError("这里记录的是LogError: 您在" + DateTime.Now.ToString()+"查看了Privacy" );
return View();
}
}
5 效果展示