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:

filemgr2

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):

iismgr

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:

image 

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.

Bookmark and Share  Comments [0] | Permalink | log4net
 

This post describes how to configure a SharePoint application so that you can write to log4net from within custom code you are developing and deploying to the web application. When working with log4net in SharePoint or ASP.NET code that is running in Windows Authentication mode with impersonation enabled, you need to configure and write to log4net using the process execution context and not the context of the user accessing the web application. The reason is that unless the user accessing the site has the rights to read the log4net configuration and write to the disk on the server then you will find that your log is either not created or has no or seemingly random output.

Updated 2/23/2010: I've added an example download with complete source code and a sample web.config file for a SharePoint solution that deploys a web part and http module. The module is setup to perform the log4net initialization as described below. The web part displays a text button and logs whatever you type to the log4net logfile. If you run the setup.exe included in the zip it will install the solution to whatever site(s) you choose. Then you can add the web part and use that to generate test writes to your log file. However the logging won't work until you have performed the steps outlined below.

Click here to download the example and complete source code

The first step is to deploy the log4net DLL to the GAC (the example download does this automatically). Next step is to specify the log4net configuration. There are many options and examples documented on the log4net web site. For this example the log4net configuration will reside in Web.config and will write to a RollingFileAppender. First, add an entry to the Web.config configSections to register the new configuration block:

Next add the log4net configuration section to Web.config just prior to the system.web section. Note that in the configuration below the file is set to reside in App_Data\Logs. You will need to create these directories in the webroot of each SharePoint web application which needs to use log4net if they do not already exist. (An example of a webroot is the following for a site running on port 40062: C:\Inetpub\wwwroot\wss\VirtualDirectories\40062).


  
    
    
    
    
    
    
    
      
    
    
      
    
  
  
    
    
  

Within your custom code there needs to be an explicit API call so the first-time initialization will occur. Although log4net does provide an auto-watch capability to look for configuration settings in Web.config, I have found this alone does not work when your application is executing in Windows Authentication mode with impersonation enabled. The workaround is to explicitly initialize log4net by creating an HttpModule initialization method and calling the log4net XmlConfigurator.Configure() method. Here's an example module - note that you can just as easily add the required code to an HttpModule you may already have setup:

using System;
using System.Text;
using System.Web;

using log4net;
using log4net.Config;
using log4net.Util;

[assembly: log4net.Config.XmlConfigurator(Watch = true)]
namespace MikeKnowles
{
    public class InitLog4NetModule : IHttpModule
    {
        public void Init(HttpApplication app)
        {
            XmlConfigurator.Configure();
        }

        public void Dispose() { }
    }
}

HttpModules are registered in the Web.config httpModules section. The entry needs to point the ASP.NET runtime to the DLL which contains your module code.

    
 

After the log is configured and all assemblies deployed the application can write to log4net. The example web part source code included in the download just writes whatever text is entered by the user to the log at DEBUG scope (see goButton_click method below).

using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Utilities;

using log4net;

namespace MikeKnowles
{
    public class Log4netExampleWebPart : System.Web.UI.WebControls.WebParts.WebPart
    {
        Button goButton;
        TextBox logTextBox;

        public Log4netExampleWebPart()
        {
        }

        protected override void CreateChildControls()
        {
            // Create text box
            logTextBox = new TextBox();
            logTextBox.Width = 360;
            logTextBox.Text = "Enter log text";
            this.Controls.Add(logTextBox);

            // Create button
            goButton = new Button();
            goButton.Text = "Go";
            goButton.Click += new EventHandler(goButton_click);
            this.Controls.Add(goButton);
        }

        public void goButton_click(object sender, EventArgs e)
        {
            ILog log = LogManager.GetLogger("ExampleLogger");
            log.Debug(logTextBox.Text);
        }
    }
}
Bookmark and Share  Comments [0] | Permalink | log4net | SharePoint
Subscribe
Top 5 Posts
Add Google Analytics to a SharePoint Publishing Site
Integrating jQuery 1.3, ASP.NET 3.5 Visual Studio 2008
Add Custom Table Formats to SharePoint Content Editor
Configuring log4net for SharePoint Windows Authentication
Convert Visual Studio 2008 Class Library Project to Web Application Project
Tags
ASP.NET (5) dasBlog (5) GoogleAnalytics (2) jQuery (1) log4net (2) SharePoint (18) WebDesign (3)
 
 
 
Spreadfirefox Affiliate Button
 
Download Notepad++