views:

1778

answers:

5

I'm looking for best practices to integrate log4net to SharePoint for web request, feature activation and all timer stuff.

I have several subprojects in my farm, and I would like to have only one Log4Net.config file.

[Edit]
Not only I need to configure log4net for the web application, which is easy to do (I use global.asax, and a log4net.config file, so I can modify log settings withtout reloading the webapp), but I also need to log asynchronous events:

  • Event Handler (like ItemAdded)
  • Timer Jobs
  • ...
A: 

You could release the config file as part of the solution package(s) to the 12 hive (use STSDev) to create any packages). This would give you a set location for the config and any changes to it can be released in a controlled manner (i.e. no need for manual editm, just roll back and re-install the solution).

Nat
+1  A: 

Firstly, you will need to modify the web.config where your SharePoint virtual directory resides. This is because you'll need to add SafeControl entries to trust the log4net assembly. You can update the web.config programmatically using the SPWebConfigModification class in a feature receiver. As you have to modify web.config anyway, you may want to consider including your log4net config inside and not set up an external log4net config.

However, if you'd still like to do this, it may work if you add the following to the web.config file:

<configuration ...>
  ...
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
  </configSections>
  <log4net configSource="log4Net.config">
  ...
</configuration>

The log4net.config file should then be able to live alongside your web.config. As Nat says, you could deploy this file as a solution package.

Assuming you are attempting to run a minimal trust, you will need to update your Code Access Security file to include the log4net assemblies as well. All of your custom SharePoint code should then automatically use your log4net configuration.

Alex Angas
thanks, but this only help me for the "web" part of SharePoint. However, I also need to log everything happening asynchronously, like event handler, feature, jobs, etc...
Nico
+4  A: 

I implemented this recently and came up with a solution that worked for me.

Deploy your log4net config file to the 12 hive and the log4net dll into the GAC using a globally scoped solution. Then in your application code explicitly initialize log4net from the location of your global file. This allows you to log feature receiver, timer jobs and web application code.

[assembly: log4net.Config.XmlConfigurator(ConfigFile = 
    @"C:\Program Files\Common Files\Microsoft Shared\" + 
    @"Web Server Extensions\12\CONFIG\log4net.config", Watch = true)]

see here http://www.codeproject.com/KB/sharepoint/SharepointLog4Net.aspx

TheCodeKing
downvoted b/c your link doesn't actually go to codeproject.com
Chloraphil
@Chloraphil ops link is fixed
TheCodeKing
Just a minor enchancment: I would recommend using SPUtility.GetGenericSetupPath(@"CONFIG\log4net.config") to get SharePoint path. In this case it won't matter if SharePoint is on C drive or not.
Janis Veinbergs
A: 

I developed a log4net feature and packaged it in a wsp file. The feature receiver adds an httpmodule to the the web.config and the httpmodule loads the log4net.config from the layouts direcory when the application start event is raised in the http module.

Athens
A: 

What solution did you choose in the end? To handle the logging of timer jobs and events? I'm looking for a similar solution.

Maarten Louage