问题
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?
回答1:
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>
回答2:
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
回答3:
Still looking for a solution? I got mine from this link .
All I had to do was add this two lines of code at the top of "public static void Main" method in the "program class".
var logRepo = LogManager.GetRepository(Assembly.GetEntryAssembly());
XmlConfigurator.Configure(logRepo, new FileInfo("log4net.config"));
Yes, you have to add:
- Microsoft.Extensions.Logging.Log4Net.AspNetCore using NuGet.
- A text file with the name of log4net.config and change the property(Copy to Output Directory) of the file to "Copy if Newer" or "Copy always".
You can also configure your asp.net core application in such a way that everything that is logged in the output console will be logged in the appender of your choice. You can also download this example code from github and see how i configured it.
回答4:
You need to install the Microsoft.Extensions.Logging.Log4Net.AspNetCore NuGet package and add a log4net.config-file to your application. Then this should work:
public class Program
{
private readonly ILogger<Program> logger;
public Program()
{
var services = new ServiceCollection()
.AddLogging(logBuilder => logBuilder.SetMinimumLevel(LogLevel.Debug))
.BuildServiceProvider();
logger = services.GetService<ILoggerFactory>()
.AddLog4Net()
.CreateLogger<Program>();
}
static void Main(string[] args)
{
Program program = new Program();
program.Run();
Console.WriteLine("\n\nPress any key to continue...");
Console.ReadKey();
}
private void Run()
{
logger.LogInformation("Logging is working");
}
}
回答5:
Following on Irfan's answer, I have the following XML configuration on OSX with .NET Core 2.1.300 which correctly logs and appends to a ./log
folder and also to the console. Note the log4net.config
must exist in the solution root (whereas in my case, my app root is a subfolder).
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %-5level %logger - %message%newline" />
</layout>
</appender>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
<file value="logs/" />
<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"/>
<appender-ref ref="ConsoleAppender"/>
</root>
</log4net>
Another note, the traditional way of setting the XML up within app.config
did not work:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net> ...
For some reason, the log4net node was not found when accessing the XMLDocument via log4netConfig["log4net"]
.
回答6:
I've figured out what the issue is the namespace is ambigious in the loggerFactory.AddLog4Net(). Here is a brief summary of how I added log4Net to my Asp.Net Core project.
- Add the nugget package Microsoft.Extensions.Logging.Log4Net.AspNetCore
Add the log4net.config file in your root application folder
Open the Startup.cs file and change the Configure method to add log4net support with this line loggerFactory.AddLog4Net
First you have to import the package using Microsoft.Extensions.Logging; using the using statement
Here is the entire method, you have to prefix the ILoggerFactory interface with the namespace
public void Configure(IApplicationBuilder app, IHostingEnvironment env, NorthwindContext context, Microsoft.Extensions.Logging.ILoggerFactory loggerFactory) { loggerFactory.AddLog4Net(); .... }
回答7:
Click here to learn how to implement log4net in .NET Core 2.2
The following steps are taken from the above link, and break down how to add log4net to a .NET Core 2.2 project.
First, run the following command in the Package-Manager console:
Install-Package Log4Net_Logging -Version 1.0.0
Then add a log4net.config with the following information (please edit it to match your set up):
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file value="logfile.log" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d [%t] %-5p - %m%n" />
</layout>
</appender>
<root>
<!--LogLevel: OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL -->
<level value="ALL" />
<appender-ref ref="FileAppender" />
</root>
</log4net>
</configuration>
Then, add the following code into a controller (this is an example, please edit it before adding it to your controller):
public ValuesController()
{
LogFourNet.SetUp(Assembly.GetEntryAssembly(), "log4net.config");
}
// GET api/values
[HttpGet]
public ActionResult<IEnumerable<string>> Get()
{
LogFourNet.Info(this, "This is Info logging");
LogFourNet.Debug(this, "This is Debug logging");
LogFourNet.Error(this, "This is Error logging");
return new string[] { "value1", "value2" };
}
Then call the relevant controller action (using the above example, call /Values/Get
with an HTTP GET), and you will receive the output matching the following:
2019-06-05 19:58:45,103 [9] INFO-[Log4NetLogging_Project.Controllers.ValuesController.Get:23] - This is Info logging
来源:https://stackoverflow.com/questions/46169606/how-to-use-log4net-in-asp-net-core-2-0