SharePoint and Log4Net

2019-02-04 21:11发布

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

4条回答
闹够了就滚
2楼-- · 2019-02-04 21:50

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

查看更多
女痞
3楼-- · 2019-02-04 21:53

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.

查看更多
趁早两清
4楼-- · 2019-02-04 21:54

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.

查看更多
不美不萌又怎样
5楼-- · 2019-02-04 22:09

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

查看更多
登录 后发表回答