I'm trying to log SQL queries into a file using log4net and .NET Core (EF 3.1).
I tried the following configuration:
Startup.cs
loggerFactory.AddLog4Net();
DBContext.s
private readonly ILoggerFactory _loggerFactory;
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseLoggerFactory(_loggerFactory);
}
log4netConfig:
<appender name="logger" type="log4net.Appender.RollingFileAppender">
<file value="c:\\log.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maximumFileSize value="100MB" />
<maxSizeRollBackups value="3"/>
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="[%thread] ; %date ; %property{ExcutionTime} ; %message%newline" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="DEBUG" />
<levelMax value="DEBUG" />
</filter>
</appender>
I got an empty file as a result. What am I missing?
You can try the following within your DbContext class:
private readonly ILogging _logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
public MyContext(): base("<connectionstring>")
{
Database.Log = log => _logger.Debug(log);
}
There is already a quite similar answer that might help you further. Take a look at the following: How to log SQL generated by EF using log4net
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.