在我的ASP.NET Core 2.2应用程序中,我安装了Microsoft.Extensions.Logging.Log4Net.AspNetCore
在startup.cs
public class Startup
{
private readonly ILogger _logger;
public Startup(IConfiguration configuration, ILogger<Startup> logger)
{
Configuration = configuration;
_logger = logger;
}
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)
{
....
.....
_logger.LogInformation("Added TodoRepository to services");
.....
.....
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env,
UserManager<User> userManager, RoleManager<Role> roleManager,
ILoggerFactory loggerFactory)
{
....
loggerFactory.AddLog4Net();
....
}
在我的家庭控制器中
public IActionResult Index()
{
_logger.LogInformation("Home Index page");
_logger.LogWarning("Home Index page");
_logger.LogError("Home Index page");
_logger.LogDebug("Home Index page");
return View();
}
我在Visual Studio“输出”窗口中看到了日志,但这并未写入任何文件。
log4net.config
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
<file value="C:\Temp\WebAppFolder" />
<datePattern value="yyyy-MM-dd.'txt'"/>
<staticLogFileName value="false"/>
<appendToFile value="true"/>
<rollingStyle value="Date"/>
<maxSizeRollBackups value="100"/>
<maximumFileSize value="15MB"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level App %newline %message %newline %newline"/>
</layout>
</appender>
<root>
<level value="ALL"/>
<appender-ref ref="RollingLogFileAppender"/>
</root>
</log4net>
答案 0 :(得分:1)
您的datePattern
值似乎格式错误。那里不应该有.txt
。另外,您需要使用要登录的文本文件的完整路径填写file
值。