views:

418

answers:

2

Hi mates

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  A: 

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.

Flo
Thanks, Flo! Very helpful. But how would you pass the path to log4net.config (let's say it is under 12 hives) to the job? I'm quite new to sharepoint but as far as I know the job might be run on a different machine
Anton Polyakov
Yes you're right you cannot define on which server the job will run, but it will definitely run on a front end server. By applying your configuration file as a feature via a SharePoint solution (.wsp file) it will be provisioned to every font end server of your farm. So the job could access the file no matter on which server it runs.
Flo
+1  A: 

I can propose you two solutions here. 1. Place all configurations in the web.config file of application and use them as explained here. 2. Use a custom configuration file and use the configuration in timer job code as explained here.

You should get it working in any of both solutions. Let me know, if you need any help.

Rare Solutions