This post outlines the steps by which I was able to get a Log4Net RollingFileAppender working on this site, which runs on a GoDaddy ASP.NET hosting plan.
First create a Logs folder in the root of your web application.
Next, Download the latest version of log4net (1.10 or higher) and add log4net.dll to your application bin folder.
Add the log4net configSection to Web.config:
<configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" requirePermission="false"/> </configSections>
Add the configuration for the RollingFileAppender to Web.config:
<appender name="GeneralLog" type="log4net.Appender.RollingFileAppender"> <file value="Logs\\RollingFileLog.txt" /> <appendToFile value="true" /> <rollingStyle value="Composite" /> <datePattern value="yyyyMMdd" /> <maxSizeRollBackups value="10" /> <maximumFileSize value="1MB" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%d{dd MMM yyyy HH:mm:ss} [%p] %c - %m%n" /> </layout> </appender> <root> <level value="ALL" /> <appender-ref ref="GeneralLog" /> </root> </log4net>
Set the GoDaddy File Manager permissions on the Logs folder as shown below:
Open the GoDaddy IIS Manager and create a virtual directory in your web application root called Logs with permissions set as shown below (it can take a few minutes for GoDaddy to apply your changes):
Define a Global.asax ApplicationStart method to initialize Log4Net and create the log file as specified above in Web.config:
void Application_Start(object sender, EventArgs e) { // Code that runs on application startup log4net.Config.XmlConfigurator.Configure(); }
Create a page with code-behind to test things out. Here's an example snippet of the essential includes and Log4Net calls to generate a simple write to your log file:
using log4net; using log4net.Config; using log4net.Util; public partial class Test : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { ILog log = LogManager.GetLogger("test"); log.Debug("test log message");
Restart your web application so that your Global.asax method will be called. A quick way to do this is to add or remove a blank line from Web.config.
Now open a browser and load your test page. If everything worked, you will now have a RollingFileLog.txt in the Logs sub-folder of your web application that looks like this:
Kudos to Phil Haack for this post which describes in detail what's required to get the Log4Net XmlConfiguration and RollingFileAppender working in a medium trust web application.