Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8691801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:11:50+00:00 2026-06-13T00:11:50+00:00

I perform some operations on files in a parallel.for Loop for which a log

  • 0

I perform some operations on files in a parallel.for Loop for which a log is created. But I need to know which operation created a particular log-entry. I tried to use the {ThreadID} in my nlog-config, but the threadID is reused by the parallel.for loop so that there seems to be no unique identifier.
Any Suggestions how to give every file a unique log-identifier?

Basic code:

Private Sub DoParallel
 Private Logger As NLog.Logger = NLog.LogManager.GetCurrentClassLogger 
 Dim ListOfFolder as New List(of String)
 ListOfFolder.add("C:\Test.txt")

 Parallel.ForEach(listOfFolders, Sub(elem As String)
     logger.warn("Doing stuff, need to know on which elem it is done")
  End Sub)
End Sub

Nlog Config:

 layout="${threadid} | ${time} | ${level:uppercase=true} | ${logger} | ${message} | ${exception}"
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T00:11:51+00:00Added an answer on June 13, 2026 at 12:11 am

    You could try using Trace.CorrelationManager.ActivityId. You can set a value and then use a custom NLog LayoutRenderer to render that value to the output log. You can find an example of a LayoutRenderer that does this here on SO. Look about in the middle of the top answer.

    Here is the code for the LayoutRender from that post (note that it was written against NLog 1.0, not NLog 2.0. It should not be hard to update).

    [LayoutRenderer("ActivityId")]
    class ActivityIdLayoutRenderer : LayoutRenderer
    {
      int estimatedSize = Guid.Empty.ToString().Length;
    
      protected override void Append(StringBuilder builder, LogEventInfo logEvent)
      {
        builder.Append(Trace.CorrelationManager.ActivityId);
      }
    
      protected override int GetEstimatedBufferSize(LogEventInfo logEvent)
      {
        return estimatedSize;
      }
    }
    

    Your NLog.config would look something like this:

    Tell NLog where your NLog extensions are located:

    <extensions>
        <add assembly="MyNLogExtensions"/>
    </extensions>
    
    layout="${threadid} | ${ActivityId} | ${time} | ${level:uppercase=true} | ${logger} | ${message} | ${exception}"
    

    Using your original sample code, you could log something like this:

    Private Sub DoParallel
     Private Logger As NLog.Logger = NLog.LogManager.GetCurrentClassLogger 
     Dim ListOfFolder as New List(of String)
     ListOfFolder.add("C:\Test.txt")
    
     Trace.CorrelationManager.ActivityId = Guid.Empty;
     Parallel.ForEach(listOfFolders, Sub(elem As String)
         Trace.CorrelationManager.ActivityId = Guid.NewGuid();
         logger.warn("Doing stuff, need to know on which elem it is done")
         Trace.CorrelationManager.ActivityId = Guid.Empty;
      End Sub)
    End Sub
    

    This should uniquely identify each logging statement and, if you had more lines of code in your action, all of the logging statements from a given execution of the action would be tagged with the same guid.

    If this approach works for you, you could easily wrap the ActivityId setting/unsetting in an IDisposable class and use the using statement to automate the process.

    Please forgive the C#:

    public class ActivityIdContext : IDisposable
    {
      Guid oldActivityId;
    
      public ActivityIdContext(Guid id)
      {
        oldActivityId = Trace.CorrelationManager.ActivityId;
        Trace.CorrelationManager.ActivityId = Guid.NewGuid();
      }
    
      public ActivityIdContext() : this(Guid.NewGuid()) { }
    
      public Dispose()
      {
        Trace.CorrelationManager.ActivityId = oldActivityId;
      }
    }
    

    Then, you could enclose the body of the Parallel.ForEach action to do something like this (again, in C#)

    Parallel.ForEach(listOfFolders, () =>
    {
      using(new ActivityIdContext())
      {
        logger.Warn("Hello!  I hope the ActivityId helps!");
      }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to perform some operations on all folders within a file share which
i need to perform some operations on files - rename, delete and etc. what
I have a table on which I want to perform some operations every hour.
I have to do a background process which will perform some operations.. while this
I need to perform some operations on a 2D array of values read from
I have two files, and I want to perform some line-wise operation across both
I need to perform some file operations on WinCE platform. The paths of file
We have to store approximately 13 million files and perform some standard operations on
I have a java class which performs some operations on files. Since the java
A winforms dialog is using BackgroundWorker to perform some asynchronous operations with significant success.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.