I have a series of Action
that I want to execute in parallel using Parallel.Invoke
. But for each Action
I want to have a separate log file created by log4net. In my log4net configuration file I have added this:
<file type="log4net.Util.PatternString" value="3S2M3_%property{UniqueIdentifier}.log" />
If there is only one Action to execute this works fine: the log file has the correct file name.
But, if there is more than one Action
to be executed, all the log entries end up in the same log file.
The code for creating the Action
is this:
Dim lstActions(4) As Action
Dim iCount As Integer = 0
For iCount = 0 To 4
Dim sUniqueIdentifier As String = iCount.ToString("D4")
Dim aOrderTask As Action = Sub()
log4net.LogicalThreadContext.Properties("UniqueIdentifier") = sUniqueIdentifier
' Some process that takes some time to complete
End Sub
lstActions(iCount) = aOrderTask
Next
Parallel.Invoke(lstActions)
The log file that gets created is using one of the values assigned to the UniqueIdentifier
property. I cannot figure out how this value is picked: it is not always the first or the last. It he example above, only the file 3S2M3_0004.log
got created and it contained all the log entries.
I have tried both LogicalThreadContext and ThreadContext and it makes not difference.