You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying to implement a feature onto a .NET 6 durable function orchestration that has a service bus trigger. The feature is to add a correlationId to all logs and this correlationId will be coming from the message received in the service bus trigger. I am trying to find the best way to implement this feature. I believe I am on the right track but I am hitting some issues.
The most brute-force way would be to pass correlationId to every orchestration, activity, and method that requires this correlationId, and then use log scopes. I want to prevent this as much as possible and so I am looking for alternative solutions.
The solution that I have come up with for now works partially. I have broken the problem into two parts, first being adding the property to the logs, and the second being saving the correlationId across the orchestration.
First, when it comes to adding the property to all logs, I have taken the use of ITelemetryInitializer. This lets me intercept ever log and add the correlationId property.
Note: You will see in the next code block where CorrelationIdContext.CorrelationId.Value is coming from
usingSystem.Diagnostics.CodeAnalysis;usingMicrosoft.ApplicationInsights.Channel;usingMicrosoft.ApplicationInsights.DataContracts;usingMicrosoft.ApplicationInsights.Extensibility;namespaceMyProject.Telemetry{publicclassCorrelationIdAppender:ITelemetryInitializer{publicvoidInitialize(ITelemetrylog){if(logisISupportPropertieslogWithProperties){if(CorrelationIdContext.CorrelationId?.Valueis not null)logWithProperties.Properties["prop_CorrelationId"]=CorrelationIdContext.CorrelationId.Value;}}}
Second, which is the main issue I have, is how to actually hold this correlationId throughout the orchestration. At first I thought that I can just create a static class with a string property, but even with my limited understanding of concurrency, threading, etc. I had a feeling this would cause issues. I did some research and found this article : https://engineering.payoneer.com/how-to-use-asynclocal-for-cross-service-logging-5a6d75dfeb81 which mentioned that AsyncLocal can be used to hold the correlationId. I have implemented this in the following.
The problem with the approach I have taken is that it seems like the correlationId is only stored for logs that execute in the trigger. In the trigger which is where I receive the correlationId, I set the value in CorrelationIdContext by doing CorrelationIdContext.SetCorrelationId(queueItem.CorrelationId). But even though the class is static when it goes to the orchestrator this value disappears.
Based on this do you have any suggestions as to what I can do to persist the correltaionId across the orchestration?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I am trying to implement a feature onto a .NET 6 durable function orchestration that has a service bus trigger. The feature is to add a correlationId to all logs and this correlationId will be coming from the message received in the service bus trigger. I am trying to find the best way to implement this feature. I believe I am on the right track but I am hitting some issues.
The most brute-force way would be to pass correlationId to every orchestration, activity, and method that requires this correlationId, and then use log scopes. I want to prevent this as much as possible and so I am looking for alternative solutions.
The solution that I have come up with for now works partially. I have broken the problem into two parts, first being adding the property to the logs, and the second being saving the correlationId across the orchestration.
First, when it comes to adding the property to all logs, I have taken the use of ITelemetryInitializer. This lets me intercept ever log and add the correlationId property.
Note: You will see in the next code block where CorrelationIdContext.CorrelationId.Value is coming from
Second, which is the main issue I have, is how to actually hold this correlationId throughout the orchestration. At first I thought that I can just create a static class with a string property, but even with my limited understanding of concurrency, threading, etc. I had a feeling this would cause issues. I did some research and found this article : https://engineering.payoneer.com/how-to-use-asynclocal-for-cross-service-logging-5a6d75dfeb81 which mentioned that AsyncLocal can be used to hold the correlationId. I have implemented this in the following.
The problem with the approach I have taken is that it seems like the correlationId is only stored for logs that execute in the trigger. In the trigger which is where I receive the correlationId, I set the value in CorrelationIdContext by doing CorrelationIdContext.SetCorrelationId(queueItem.CorrelationId). But even though the class is static when it goes to the orchestrator this value disappears.
Based on this do you have any suggestions as to what I can do to persist the correltaionId across the orchestration?
Beta Was this translation helpful? Give feedback.
All reactions