Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use log4net in Asp.net core 2.0

I configure log4net in my asp.net core 2.0 application as mentioned in this article LINK

program.cs

public static void Main(string[] args) {     var logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());     XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));      BuildWebHost(args).Run(); } 

HomeController

public class HomeController : Controller {     private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(HomeController));      public IActionResult Error()     {         log.Info("Hello logging world!");         return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });     } } 

log4net.config

<?xml version="1.0" encoding="utf-8"?> <configuration>   <log4net>     <root>       <level value="ALL" />       <appender-ref ref="RollingFile" />     </root>     <appender name="RollingFile" type="log4net.Appender.FileAppender">       <file value="‪C:\Temp\app.log" />        <layout type="log4net.Layout.PatternLayout">         <conversionPattern value="%-5p %d{hh:mm:ss} %message%newline" />       </layout>     </appender>   </log4net> </configuration> 

Unlucky!, I didn't see any file generated in ‪C:\Temp\app.log directory. What would be the mistake? how to configure log4net for asp.net core 2.0?

like image 219
k11k2 Avatar asked Sep 12 '17 06:09

k11k2


People also ask

Can we use log4net in .NET core?

Now for the good stuff. Let's add log4net in an ASP.NET Core Web App project and save the logs in a rolling file. For this example, we'll set up a config to generate a new log file every 1 minute. In actual implementations, you can actually generate a new file every hour or day, or even based on the maximum file size.

How will you implement log4net in asp net core application?

Step 1: appsettings file Install the package in your Asp.net Core application and add Log4Net in the appsettings. json file. Add appsetting. json file in configuration builder method as below.

Does log4net work with .NET 5?

log4net works with almost any version of . NET (including . NET Core).


2 Answers

I am successfully able to log a file using the following code

public static void Main(string[] args) {     XmlDocument log4netConfig = new XmlDocument();     log4netConfig.Load(File.OpenRead("log4net.config"));     var repo = log4net.LogManager.CreateRepository(Assembly.GetEntryAssembly(),                typeof(log4net.Repository.Hierarchy.Hierarchy));     log4net.Config.XmlConfigurator.Configure(repo, log4netConfig["log4net"]);      BuildWebHost(args).Run(); } 

log4net.config in website root

<?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\" />     <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> 
like image 27
Irfan Ashraf Avatar answered Oct 12 '22 10:10

Irfan Ashraf


There is a third-party log4net adapter for the ASP.NET Core logging interface.

Only thing you need to do is pass the ILoggerFactory to your Startup class, then call

loggerFactory.AddLog4Net(); 

and have a config in place. So you don't have to write any boiler-plate code.

More info here

like image 166
Jan Muncinsky Avatar answered Oct 12 '22 12:10

Jan Muncinsky