SharePoint and Log4Net
Asked Answered
T

4

10

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
  • ...
Twinflower answered 20/10, 2008 at 19:55 Comment(2)
Have you thought of logging to the SharePoint logs?Twannatwattle
Yes. But I want to be able to have differents log files for each project, or to have a central database for logs with a simple webapp to see those logs withtout having to give access to ULS logs.Twinflower
C
9

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

Confab answered 19/11, 2009 at 23:8 Comment(3)
downvoted b/c your link doesn't actually go to codeproject.comPatrimony
@Patrimony ops link is fixedConfab
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.Solvency
E
1

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.

Empower answered 21/10, 2008 at 16:59 Comment(1)
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...Twinflower
T
0

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

Twannatwattle answered 20/10, 2008 at 20:6 Comment(0)
E
0

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.

Etruscan answered 8/2, 2010 at 18:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.