Sunday, February 24, 2008

log4net in sharepoint 2007

Phil Haack explains configuring log4net in asp.net 2.0 application. We can keep the configuration in the web.config, and we need to tell the log4net, that where we kept the configuration in the Application_Start() event using XmlConfiguration.Configure() menthod. In asp.net we can add a Global.asax and keep this code in Global.asax.cs.

But how can we do this in Sharepoint?

Every site collection we create in sharepoint, creates a globals.asax file and keeps in the webapp root directory. The asax files contains the following code.
<%@ Assembly Name="Microsoft.SharePoint"%>
<%@ Application Language="C#" Inherits="Microsoft.SharePoint.ApplicationRuntime.SPHttpApplication" %>
We can add a inline code in the global.asax file as follows,
<%@ Assembly Name="Microsoft.SharePoint"%>
<%@ Assembly Name="log4net, version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821" %>
<%@ Application Language="C#" Inherits="Microsoft.SharePoint.ApplicationRuntime.SPHttpApplication" %>
<%@ Import Namespace="log4net.Config" %>

<script runat="server">
void Application_Start(Object sender, EventArgs e)
{

// Configure log4Net, to take its configuration from the web.config file.
XmlConfigurator.Configure();
}
</script>
or we can derive a class from SPHttpApplication and add the Application_Start() method.

Note, as i explained here you need to keep the log4net.dll in the _app_bin directory of your sharepoint webapplication.

Cheers,
Murugan G.

1 comment:

kevin said...

Hi,do as you said,but some error occur.
log4net.dll path is
C:\Inetpub\wwwroot\wss\VirtualDirectories\80\_app_bin

log4net.config path is
C:\Inetpub\wwwroot\wss\VirtualDirectories\80


Parser Error
Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.

Parser Error Message: There can be only one 'application' directive.

Source Error:


Line 3: < Assembly Name="Microsoft.SharePoint">
Line 4: < Assembly Name="log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821" %>
Line 5: < Application Language="C#" Inherits="Microsoft.SharePoint.ApplicationRuntime.SPHttpApplication" %>
Line 6: < Import Namespace="log4net.Config" >
Line 7:


can you help