I'm developing a custom SharePoint solution which consist of a feature and a timer job (which is created by the featu开发者_Go百科re 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:
- Where to store log4net.config and how pass it to the job?
- Do I need to initialize log4net each time Execute() method is called?
Thanks!
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.
精彩评论