Click here to Skip to main content
Licence CPOL
First Posted 3 Mar 2009
Views 12,827
Bookmarked 9 times

SharePoint 2007 - Log4net - User Controls - Smart Part

By , | 3 Mar 2009 | Article
Configure log4net with SharePoint 2007

Introduction

For those of you who have used Log4net in your .NET applications, you can do the same in SharePoint for custom user controls.

Before continuing below, you need some kind of wrapper for your user controls. You can either develop or download a webpart which will provide this functionality for you. 

We downloaded SmartPart to provide this functionality for us. Download and installation instructions can be found here.

The newest version of log4net can be found here.

Installing Log4Net for SharePoint

Step 1 

Add the log4net.dll to the bin directory of the SharePoint instance you are working on. Our site was installed in: C:\inetpub\wwwroot\wss\VirtualDirectories\443\bin.

Step 2 

Modify the web.config file for the SharePoint instance in question.

Add the following to the <configuration><configsections> section of the web.config file:

<section name="log4net" 
	type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>

Add the following just before the <system.web> section:

<log4net>
  <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
   <param name="File" value="C:\\logs\\RollingWebPart.log"/>
    <param name="AppendToFile" value="true"/> <param name="RollingStyle" value="Size"/>
    <param name="MaxSizeRollBackups" value="30"/>
    <param name="MaximumFileSize" value="100KB"/>
    <param name="StaticLogFileName" value="true"/>
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] 
		%-5level %logger [%property{NDC}] - %message%newline"/>
    </layout>
  </appender>
  <logger name="LogIT">
     <level value="DEBUG"/>
     <appender-ref ref="RollingFileAppender"/>
  </logger>
</log4net>

Step 3

Add the following to the global.aspx file which is found in the same virtualdirectory path that the web.confile is located.  In our install, it was found at:  c:\inetpub\wwwroot\wss\VirtualDirectories\443.

<%@ Import Namespace="log4net" %>
<%@ Import Namespace="log4net.config" %>

<script language="VB" runat="server">
Protected Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs) 
  log4net.Config.XmlConfigurator.Configure()
End Sub
</script>  

Step 4 

Now all of the configuration steps have been completed. The next part is to deploy your user control to the SharePoint environment. 

Please note: We were unable to deploy our user control with a code behind. All of our development has been within the main body of the ascx file. 

Given below is a sample of what a test user control could look like.

Note the imports for the log4net namespace and the Ilog object.

<%@ Control Language="vb" AutoEventWireup="false" %>
<%@ Import Namespace="log4net" %>
<%@ Import Namespace="log4net.Config" %>

<script runat="server">

  Private Shared ReadOnly log As ILog = LogManager.GetLogger("LogIT")
  Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)

     Label1.Text = TextBox1.Text

     log.Info(TextBox1.TeXT)
  End Sub
</script>

<asp:Button ID="Button1"
   runat="server"
   Text="Button"
   onclick="Button1_Click" />


<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

<br />

<asp:Label ID="Label1" runat="server"></asp:Label>

Deploy the user control to your SharePoint environment (the same virtualdirectory we've been discussing).

Create the logs folder you referenced in step 2:

<param name="File" value="C:\\logs\\RollingWebPart.log"/>

Do an iisreset on your SharePoint server and you should be in business!

Please feel free to post any suggestions as we are new to SharePoint custom development.

History

  • 3rd March, 2009: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Authors

markphalen



United States United States

Member



tdashnaw

Web Developer

United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralYou can have code behind files PinmemberAlan Zhang12:40 11 Mar '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 3 Mar 2009
Article Copyright 2009 by markphalen, tdashnaw
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid