Sharepoint Timer Job and log4net

二次信任 提交于 2019-12-24 06:45:20

问题


I'm developing a custom SharePoint solution which consist of a feature and a timer job (which is created by the feature in FeatureActivated). In my solution I want to use log4net for logging (I know about ULS).

Everything is fine with log4net in the feature itself (just placed log4net.config near web.config and all is fine), but I have completely no idea how to initialize log4net from timer job (taking into account it is run not by IIS but by OSWTIMER).

Can someone please help me finding answers for the questions:

  1. Where to store log4net.config and how pass it to the job?
  2. Do I need to initialize log4net each time Execute() method is called?

Thanks!


回答1:


It has been a while ago since I used log4net but if I remember right, you could store you configuration file where ever you want and let log4net load it from there during initialization of the logger.

1) A good place to store the configuration file would be the folder within the 12 hives. Perhaps the folder of your feature. As the path to this file won't change it should be easy to reference it with the logger.

2) I think you'll have to initialize the logger every time as the SPJobDefinition (timer job class) is stored within the database when it doesn't run. The functionality to store the timer job in the database comes from the SPJobDefinition's base class SPPersistedObject. Member variables of a class inherited from SPPersistedObject, that should be stored together with the class in the database, have to be marked with a [Persisted] attribute. But as far as I know persisted objects can only be elementary data types (int, long, string, ...).

So storing a log4net Logger object within the database won't work or at least is not supported. So every time your job is loaded from the database the Logger object has to be initialized again.



来源:https://stackoverflow.com/questions/1637349/sharepoint-timer-job-and-log4net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!