Allows to configure Log4net as Microsoft Extensions Logging handler on any ASP.NET Core application.
Thanks to @anuraj for this original blog post.
-
Install the package or reference the project into your asp.net core application.
-
Add the
AddLog4Net()
call into yourConfigure
method of theStartup
class.
using Microsoft.Extensions.Logging;
public class Startup
{
//...
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
//...
loggerFactory.AddLog4Net(); // << Add this line
app.UseMvc();
//...
}
}
- Add a
log4net.config
file with the content:
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<appender name="DebugAppender" type="log4net.Appender.DebugAppender" >
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<root>
<level value="ALL"/>
<appender-ref ref="DebugAppender" />
</root>
</log4net>
Associated issues #34 & #41
In order to be able to register Debug level messages in any of your configured log4net appenders, you should change the ASP .NET Core 2 configuration when you build your IWebHost
instance as follows.
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.ConfigureLogging((hostingContext, logging) =>
{
// The ILoggingBuilder minimum level determines the
// the lowest possible level for logging. The log4net
// level then sets the level that we actually log at.
logging.AddLog4Net();
logging.SetMinimumLevel(LogLevel.Debug);
})
.Build();
Sometimes we might want to modify the value of an appender, for example, the file name of our log. This might be interesting if we want to use a different name for each environment deployed. To do this, this package includes the possibility of overwriting the information of a node or the attributes of that node using the Net Core configuration system.
To do this, you will need to do the following:
"Log4net": [
{
"XPath": "/log4net/appender[@name='RollingFile']/file",
"Attributes": {
"Value": "overrided.log"
}
},
{
"XPath": "/log4net/appender[@name='RollingFile']/maximumFileSize",
"Attributes": {
"Value": "1024KB"
}
}
]
As you can see, the section is an array. For each element of the array, an XPath
key must be included, which will contain the XPath expression to find the node from which we want to overwrite its information.
The Attributes
key will contain a list of all the attributes you want to overwrite. In our case, we will almost always add the attribute value
, followed by the value we want that attribute to take.
The NodeContent
key will contain the text to be included inside the node, removing any information that was previously on the original node.
Add as the first parameter the name of your log4net
configuration file, and specify, as the second parameter, an IConfigurationSection object containing the configuration section you added to your AppSettings.json
file:
loggerFactory.AddLog4Net("log4net.config", Configuration.GetSection("Log4net"));
This way, the package will iterate for each XPath contained in the array, will check if there are any nodes within the XML file that match the expression, and will overwrite the attributes or content of that node, depending on what you have specified in the configuration section.
Thank you very much to all contributors & users by its collaboration, and specially to: