I was able to respond with the following methods:
1-Install-Package log4net
2-Install-Package MicroKnights.Log4NetAdoNetAppender
3-Install-Package System.Data.SqlClient
First,I Create Database and Table with this Code:
CREATE DATABSE Log4netDb
CREATE TABLE [dbo].[Log] (
[Id] [int] IDENTITY (1, 1) NOT NULL,
[Date] [datetime] NOT NULL,
[Thread] [varchar] (255) NOT NULL,
[Level] [varchar] (50) NOT NULL,
[Logger] [varchar] (255) NOT NULL,
[Message] [varchar] (4000) NOT NULL,
[Exception] [varchar] (2000) NULL
)
Second,
I Create log4net.config File in program .
This is a simple configuration with no customization on the log message:
<?xml version="1.0" encoding="utf-8" ?>
<log4net debug="true">
<!-- definition of the RollingLogFileAppender goes here -->
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="Logs/WebApp.log" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<!-- Format is [date/time] [log level] [thread] message-->
<conversionPattern value="[%date] [%level] [%thread] %m%n" />
</layout>
</appender>
<appender name="AdoNetAppender" type="MicroKnights.Logging.AdoNetAppender, MicroKnights.Log4NetAdoNetAppender">
<bufferSize value="1" />
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data" />
<connectionStringName value="log4net" />
<connectionStringFile value="appsettings.json" />
<commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message]) VALUES (@log_date, @thread, @log_level, @logger, @message)" />
<parameter>
<parameterName value="@log_date" />
<dbType value="DateTime" />
<layout type="log4net.Layout.RawTimeStampLayout" />
</parameter>
<parameter>
<parameterName value="@thread" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%thread" />
</layout>
</parameter>
<parameter>
<parameterName value="@log_level" />
<dbType value="String" />
<size value="50" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%level" />
</layout>
</parameter>
<parameter>
<parameterName value="@logger" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%logger" />
</layout>
</parameter>
<parameter>
<parameterName value="@message" />
<dbType value="String" />
<size value="4000" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message" />
</layout>
</parameter>
<parameter>
<parameterName value="@exception" />
<dbType value="String" />
<size value="2000" />
<layout type="log4net.Layout.ExceptionLayout" />
</parameter>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="RollingLogFileAppender" />
<appender-ref ref="AdoNetAppender" />
</root>
</log4net>
Third,
Replace code below with 'IHostBuilder' :
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureLogging(logging =>
{
// clear default logging providers
logging.ClearProviders();
logging.AddConsole();
logging.AddDebug();
logging.AddEventLog();
// add more providers here
})
.UseStartup<Startup>();
Fourth,
in appsettings.json insert this code:
{
"connectionStrings": {
"log4net": "Server=MICKO-PC;Database=Log4netDb;Trusted_Connection=True;MultipleActiveResultSets=true"
}
}
At the end,
Use these commands to enjoy logging in
public class ValuesController : Controller
{
private static readonly ILog log = LogManager.GetLogger(typeof(ValuesController));
[HttpPost]
public async Task<IActionResult> Login(string userName, string password)
{
log.Info("Action start");
// More code here ...
log.Info("Action end");
}
// More code here...
}
Good luck.