Log4Net Not Logging When Deployed

If you're using IIS, make sure the correct group has modify access to the Logs folder (usually IIS_USERS).


Sounds like a permissions issue to me. I almost always use a directory where I don't have to enable any special permissions for the applications to write the log files to.

Here is what I generally use with log4net:

<file type="log4net.Util.PatternString" value="${ALLUSERSPROFILE}/<Company Name>/Logs/<Program Name>/<Log file name>.txt" />

Of cource you'll need to substitute Company Name, Program Name and Log file name in the above with actual values.

This will write to the ProgramData folder where access is typically not restricted. You can navigate to this folder in File Explorer by typing %ProgramData% or %AllUsersProfile%

Another thing I like about this method is that it works on nearly every microsoft O/S. XP, Vista, 7, 8


If the directory and the file is not being created, then most likely, the configuration is not being read (and therefore used) at runtime.

I always forget to add the single line of code for Log4net that hooks up the configuration. This code usually appears in the bootstrap class in the application (e.g. Global.asax for an ASP.NET app).

XmlConfigurator.Configure(new System.IO.FileInfo(configFile));  // configFile being the path to the file.

Instead of the above in-line, you can add this attribute to the AssemblyInfo.cs file:

[assembly: log4net.Config.XmlConfigurator(Watch = true)]

Either way, this will wire up log4net. More information is found in the Manual Configuration section of the log4net docs.

Tags:

C#

.Net

Log4Net