The easiest and quickest way to get started using log4net is by installing it through the NuGet Package Manager. Assuming that you have created a console application project in Visual Studio, you can install log4net via NuGet Manager, by following these steps.
log4net works with almost any version of . NET (including . NET Core).
Just create a log4net. config file with a log file as an appender, then add two using statements and a single line of code to the new . NET 6 hosting model: //Program.
You can download the desired Log4net. dll from this path: http://logging.apache.org/log4net/download.html, Whilst I've also attached the required Log4net.
One gotcha for this type of thing is to make sure to add the XmlConfigurator
attribute to the assembly by placing the following line in your AssemblyInfo.cs
:
[assembly: log4net.Config.XmlConfigurator]
Otherwise log4net never activates.
I guess that either log4net is not logging at all, or the file is not ending up where you expect it.
Firstly, have you actually called
XmlConfigurator.Configure()
anywhere in your code? If the xml snippet above is in the application configuration file, this call will do the trick. If the xml snippet is in it's own file, you'll need to use the .Configure(string)
overload that takes the path to the file. Without this call (or apparently the assembly level attribute mentioned by Kirk Woll), then log4net won't be logging at all.
If you believe this is all done, and log4net should be logging, then maybe you should put a fully qualified path in for the log file while you debug further. That will let you be sure where the file should be.
There is another small gotcha, see here: http://logging.apache.org/log4net/release/manual/configuration.html#dot-config
the [assembly: log4net.Config.XmlConfigurator]
method doesn't work with app.config. If you configure log4net from app.config, you must use the log4net.Config.XmlConfigurator.Configure()
method.
Here is my checklist for when log4net is proving to be recalcitrant:
For an ASP.NET MVC project adding
log4net.Config.XmlConfigurator.Configure();
to the Global.asax.cs also helps:
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AuthConfig.RegisterAuth();
log4net.Config.XmlConfigurator.Configure();
}
}
These are the steps which eventually got my file logging working:
<appender name="MainLogger"...
<layout type="log4net.Layout.SimpleLayout"/>
<add key="log4net.Internal.Debug" value="true"/>
to your appSettings
.I've had experiences where logging systems fail silently without raising exceptions. I suppose this makes sense because if the logger is logging errors then how can it log an error that it's unable to perform logging?
So if the file isn't created on disk, start by looking into file system permissions to ensure the user your application is running under can write a new file to that disk location.
For testing purposes you might want to manually create the file on disk that should be written to and open up permissions for everybody to write to it. If the logger starts writing to it then you know it's permission based rather than configuration based.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With