0

I have a parallel ForEach that calls another method to so some processing, in the other method I use Enterprise Logging. Currently the log all goes to one file, I would like to have each thread create its own log file at least each thread that meets a certain criteria to have it own log file. is this possible with Enterprise logging. I am thinking I can define multiple switches in the configuration file and then use those switches in the logging, will that work

current Code

                Parallel.ForEach(
                listOfAccounts,
                new ParallelOptions { MaxDegreeOfParallelism = 10},
                accountId =>
                {
                    AccountProcess(accountId);
                });

This is the method called:

    private void AccountProcess(int accountId)
    {
        string msgPrefix = MethodBase.GetCurrentMethod().DeclaringType.Name + "." + MethodBase.GetCurrentMethod().Name;

        _logWriter.WriteLog(msgPrefix, string.Format(" Starting AccountProcess method with accountId: {0} on threadID: {1}", accountId, Thread.CurrentThread.ManagedThreadId),
            "Information", 0, 0, TraceEventType.Information);

        try
        {
            _processor = new AccountProcessor(_logWriter);
            bool result = _processor.AccountProcess(accountId);
            _logWriter.WriteLog(msgPrefix, string.Format(" Account Process result: {0} on threadID: {1}", result.ToString(), Thread.CurrentThread.ManagedThreadId),
                "Information", 0, 0, TraceEventType.Information);
        }
        catch(Exception ex)
        {
            _logWriter.WriteLog(msgPrefix,
                string.Format("Error: {0}", ex.Message), "Error", 1, 1, TraceEventType.Error);
            bool rethrow = ExceptionPolicy.HandleException(ex, "Fatal");
            if (rethrow)
            {
                throw;
            }
        }
        finally
        {
            _processor = null;
        }
        _logWriter.WriteLog(msgPrefix, string.Format(" Completed AccountProcess method with accountId: {0} on threadID: {1}", accountId, Thread.CurrentThread.ManagedThreadId),
            "Information", 0, 0, TraceEventType.Information);
    }

In the AccountProcess Method I want to log each thread to a new file. Do I need to create a new instance of LogWriter for each thread rather than use the singleton of Enterprise logging V6. The _logWriter is wrapper to the Singleton instance of LogWriter in version 6 of Enterprise logging OR do need to use multiple switches. I have three type of threads (each thread has a property based on the accountID. so based on the particular type I can use particular switch eg.

  <add switchValue="Type1" name="Information">
    <listeners>
      <add name="Informational Listener"/>
    </listeners>
  </add>

  <add switchValue="Type2" name="Information">
    <listeners>
      <add name="Informational Listener"/>
    </listeners>
  </add>

  <add switchValue="Type3" name="Information">
    <listeners>
      <add name="Informational Listener"/>
    </listeners>
  </add>

would this approach work?

Jawahar
  • 183
  • 4
  • 16
  • I avoid singletons when writing multi-threaded apps. You don't want threads competing with each other on a single instance. – Tyler Jennings Oct 27 '15 at 14:33
  • By "Enterprise logging", do you mean you are using Microsoft's Enterprise Library? If so, please add [tag:enterprise-logging] to your post's tags, so people know what API you're actually asking about. – Peter Duniho Oct 28 '15 at 04:04

0 Answers0