views:

17

answers:

2

I'm working on a project with some custom Sharepoint Workflow components which I'd like to add log4net to.

I'm really struggling to get log4net to output anything at all though!

Here's my current setup:

In the codebehind for my Workflow:

private ILog log;

public MessageQueueWorkflow()
{
    InitializeComponent();

    string filepath = ConfigurationManager.AppSettings["log4netConfigPath"];
    if (!string.IsNullOrEmpty(filepath))
    {
        log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo(filepath));
        log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    }

}

public Guid workflowId = default(System.Guid);
public SPWorkflowActivationProperties workflowProperties = new SPWorkflowActivationProperties();

private void onWorkflowActivated1_Invoked(object sender, ExternalDataEventArgs e)
{
    try
    {
        #region Logging
        if (log.IsDebugEnabled)
        {
            log.Debug(System.Reflection.MethodInfo.GetCurrentMethod().Name);
        }
        #endregion Logging

        // do some stuff
    }
    catch (Exception ex)
    {
        if (log.IsErrorEnabled)
        {
            log.Error("An error has occurred.", ex);
        }

        throw ex;
    }
}

In my web.config for the Sharepoint site:

<appSettings>
    <add key="log4netConfigPath" value="C:\Inetpub\wwwroot\wss\VirtualDirectories\80\log4net.config"/>
</appSettings>

In my log4net.config file:

<log4net debug="true">
  <appender name="EventLogAppender" type="log4net.Appender.EventLogAppender" >
    <applicationName value="MyApp" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%d %-5p %c - %m%n" />
    </layout>
  </appender>
</log4net>

Now, when I run this workflow, I'd expect to see some debug entries showing up in the EventViewer, but I'm getting nothing.

Any ideas what I'm doing wrong?

Thanks!

+1  A: 

You need to configure at least one logger. Usually you would configure the root logger. E.g.:

<log4net>
   ...
   <root>
       <level value="ALL" />
      <appender-ref ref="EventLogAppender" />     
   </root>
</log4net>

If this is not making it work yet, the I recommend that you configure a trace listener that will output the log4net internal debugging messages. (You already have internal debugging turned on.)

Stefan Egli
Awesome... Thanks Stefan. I'd messed about with the config file fairly extensively, and hadn't noticed that I'd knocked off the <root> node! The tracing was also a good suggestion, and showed me that whatever service ID Sharepoint is running under doesn't have access to use the EventLog. I've changed it to log to a file, and it's working correctly now.
NeilD
A: 

Also keep in mind that whenver workflow goes to sleep (serialized to database) and wakes up, it continues to run within OWSTIMER.EXE process and you will NO MORE see any log entries. This also applies for log entries written in event reciever or methods executed by stsadm or any other process than IIS worker process.

So i would recommend you to:

  • Put log4net.config in 12Hive/CONFIG directory
  • Put log4net.dll in GAC

Then i prefer putting this in my AssemblyInfo.cs:

[assembly: log4net.Config.XmlConfigurator(ConfigFile = 
    @"C:\Program Files\Common Files\Microsoft Shared\" + 
    @"Web Server Extensions\12\CONFIG\log4net.config", Watch = true)]

So whatever process will use my assembly, it will get logged (would it be event reciever, stsadm console or SharePoint Manager - it all gets logged).

See SharePoint and Log4Net question.

Janis Veinbergs